前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >(四十七)c#Winform自定义控件-树表格(treeGrid)

(四十七)c#Winform自定义控件-树表格(treeGrid)

作者头像
冰封一夏
发布2019-09-09 17:13:48
1.3K0
发布2019-09-09 17:13:48
举报
文章被收录于专栏:c#Winform自定义控件系列

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

NuGet

代码语言:javascript
复制
Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

这个是在前面表格的基础上,扩展了自定义行实现的,当然也修改了一些列表控件以兼容

如果对前面的表格控件不了解,请移步查看

(三十二)c#Winform自定义控件-表格

开始

实现树表格的思路就是,在行控件中再添加一个无标题的表格控件,当需要显示子节点的时候,将子节点数据加载到行里的表格控件中,然后处理一下事件,让事件可以穿透到最顶层就行了。

另外我们前面表格中的行个数是根据高度大小自动计算的,这里就会出现问题,当出现子节点表格的时候,就会导致重算个数和高度,所有我们在表格列表控件增加一个属性来禁用这个自动计算。

添加一个用户控件,命名UCDataGridViewTreeRow,实现接口IDataGridViewRow

属性

代码语言:javascript
复制
 1  #region 属性
 2         public event DataGridViewEventHandler CheckBoxChangeEvent;
 3 
 4         public event DataGridViewEventHandler CellClick;
 5 
 6         public event DataGridViewEventHandler SourceChanged;
 7 
 8         public List<DataGridViewColumnEntity> Columns
 9         {
10             get;
11             set;
12         }
13 
14         public object DataSource
15         {
16             get;
17             set;
18         }
19 
20         public bool IsShowCheckBox
21         {
22             get;
23             set;
24         }
25         private bool m_isChecked;
26         public bool IsChecked
27         {
28             get
29             {
30                 return m_isChecked;
31             }
32 
33             set
34             {
35                 if (m_isChecked != value)
36                 {
37                     m_isChecked = value;
38                     (this.panCells.Controls.Find("check", false)[0] as UCCheckBox).Checked = value;
39                 }
40             }
41         }
42 
43         int m_rowHeight = 40;
44         public int RowHeight
45         {
46             get
47             {
48                 return m_rowHeight;
49             }
50             set
51             {
52                 m_rowHeight = value;
53                 this.Height = value;
54             }
55         }
56         #endregion

构造函数处理一写东西,注意 this.ucDGVChild.ItemClick ,这个是将子节点表格的点击事件向上传递

代码语言:javascript
复制
 1  public UCDataGridViewTreeRow()
 2         {
 3             InitializeComponent();
 4             this.ucDGVChild.RowType = this.GetType();
 5             this.ucDGVChild.IsAutoHeight = true;
 6             this.SizeChanged += UCDataGridViewTreeRow_SizeChanged;
 7             this.ucDGVChild.ItemClick += (a, b) =>
 8             {
 9                 if (CellClick != null)
10                 {
11                     CellClick(a, new DataGridViewEventArgs()
12                     {
13                         CellControl = (a as Control),
14                         CellIndex = (a as Control).Tag.ToInt()
15                     });
16                 }
17             };
18 
19         }

实现接口函数BindingCellData,主要处理一下数据绑定后将子节点数据向下传递

代码语言:javascript
复制
 1  public void BindingCellData()
 2         {
 3             for (int i = 0; i < Columns.Count; i++)
 4             {
 5                 DataGridViewColumnEntity com = Columns[i];
 6                 var cs = this.panCells.Controls.Find("lbl_" + com.DataField, false);
 7                 if (cs != null && cs.Length > 0)
 8                 {
 9                     var pro = DataSource.GetType().GetProperty(com.DataField);
10                     if (pro != null)
11                     {
12                         var value = pro.GetValue(DataSource, null);
13                         if (com.Format != null)
14                         {
15                             cs[0].Text = com.Format(value);
16                         }
17                         else
18                         {
19                             cs[0].Text = value.ToStringExt();
20                         }
21                     }
22                 }
23             }
24             panLeft.Tag = 0;
25             var proChildrens = DataSource.GetType().GetProperty("Childrens");
26             if (proChildrens != null)
27             {
28                 var value = proChildrens.GetValue(DataSource, null);
29                 if (value != null)
30                 {
31                     int intSourceCount = 0;
32                     if (value is DataTable)
33                     {
34                         intSourceCount = (value as DataTable).Rows.Count;
35                     }
36                     else if (typeof(IList).IsAssignableFrom(value.GetType()))
37                     {
38                         intSourceCount = (value as IList).Count;
39                     }
40                     if (intSourceCount > 0)
41                     {
42                         panLeft.BackgroundImage = Properties.Resources.caret_right;
43                         panLeft.Enabled = true;
44                         panChildGrid.Tag = value;
45                     }
46                     else
47                     {
48                         panLeft.BackgroundImage = null;
49                         panLeft.Enabled = false;
50                         panChildGrid.Tag = null;
51                     }
52                 }
53                 else
54                 {
55                     panLeft.BackgroundImage = null;
56                     panLeft.Enabled = false;
57                     panChildGrid.Tag = null;
58                 }
59             }
60             else
61             {
62                 panLeft.BackgroundImage = null;
63                 panLeft.Enabled = false;
64                 panChildGrid.Tag = null;
65             }
66         }

实现函数绑定列

代码语言:javascript
复制
 1  public void ReloadCells()
 2         {
 3             try
 4             {
 5                 ControlHelper.FreezeControl(this, true);
 6                 this.panCells.Controls.Clear();
 7                 this.panCells.ColumnStyles.Clear();
 8 
 9                 int intColumnsCount = Columns.Count();
10                 if (Columns != null && intColumnsCount > 0)
11                 {
12                     if (IsShowCheckBox)
13                     {
14                         intColumnsCount++;
15                     }
16                     this.panCells.ColumnCount = intColumnsCount;
17                     for (int i = 0; i < intColumnsCount; i++)
18                     {
19                         Control c = null;
20                         if (i == 0 && IsShowCheckBox)
21                         {
22                             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F));
23 
24                             UCCheckBox box = new UCCheckBox();
25                             box.Name = "check";
26                             box.TextValue = "";
27                             box.Size = new Size(30, 30);
28                             box.Dock = DockStyle.Fill;
29                             box.CheckedChangeEvent += (a, b) =>
30                             {
31                                 IsChecked = box.Checked;
32                                 this.ucDGVChild.Rows.ForEach(p => p.IsChecked = box.Checked);
33                                 if (CheckBoxChangeEvent != null)
34                                 {
35                                     CheckBoxChangeEvent(a, new DataGridViewEventArgs()
36                                     {
37                                         CellControl = box,
38                                         CellIndex = 0
39                                     });
40                                 }
41                             };
42                             c = box;
43                         }
44                         else
45                         {
46                             var item = Columns[i - (IsShowCheckBox ? 1 : 0)];
47                             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
48 
49                             Label lbl = new Label();
50                             lbl.Tag = i - (IsShowCheckBox ? 1 : 0);
51                             lbl.Name = "lbl_" + item.DataField;
52                             lbl.Font = new Font("微软雅黑", 12);
53                             lbl.ForeColor = Color.Black;
54                             lbl.AutoSize = false;
55                             lbl.Dock = DockStyle.Fill;
56                             lbl.TextAlign = ContentAlignment.MiddleCenter;
57                             lbl.MouseDown += (a, b) =>
58                             {
59                                 Item_MouseDown(a, b);
60                             };
61                             c = lbl;
62                         }
63                         this.panCells.Controls.Add(c, i, 0);
64                     }
65 
66                 }
67             }
68             finally
69             {
70                 ControlHelper.FreezeControl(this, false);
71             }
72         }

当点击展开图标的时候处理子节点的数据绑定以及高度计算

代码语言:javascript
复制
 1 private void panLeft_MouseDown(object sender, MouseEventArgs e)
 2         {
 3             try
 4             {
 5                 ControlHelper.FreezeControl(this.FindForm(), true);
 6                 if (panLeft.Tag.ToInt() == 0)
 7                 {
 8                     var value = panChildGrid.Tag;
 9                     if (value != null)
10                     {
11                         panLeft.BackgroundImage = Properties.Resources.caret_down;
12                         panLeft.Tag = 1;
13                         int intSourceCount = 0;
14                         if (value is DataTable)
15                         {
16                             intSourceCount = (value as DataTable).Rows.Count;
17                         }
18                         else if (typeof(IList).IsAssignableFrom(value.GetType()))
19                         {
20                             intSourceCount = (value as IList).Count;
21                         }
22                         this.panChildGrid.Height = RowHeight * intSourceCount;
23                         if (panChildGrid.Height > 0)
24                         {
25                             if (value != this.ucDGVChild.DataSource)
26                             {
27                                 this.ucDGVChild.Columns = Columns;
28                                 this.ucDGVChild.RowHeight = RowHeight;
29                                 this.ucDGVChild.HeadPadingLeft = this.panLeft.Width;
30                                 this.ucDGVChild.IsShowCheckBox = IsShowCheckBox;
31                                 this.ucDGVChild.DataSource = value;
32                                 this.ucDGVChild.Rows.ForEach(p => p.IsChecked =this.IsChecked);
33                             }
34                         }
35                     }
36 
37                 }
38                 else
39                 {
40                     panLeft.Tag = 0;
41                     panChildGrid.Height = 0;
42                     panLeft.BackgroundImage = Properties.Resources.caret_right;
43                 }
44             }
45             finally
46             {
47                 ControlHelper.FreezeControl(this.FindForm(), false);
48             }
49         }

承载子节点表格的panel 在大小改变的时候,处理父级表格的大小。来防止出现多个滚动条

代码语言:javascript
复制
1   void UCDataGridViewTreeRow_SizeChanged(object sender, EventArgs e)
2         {
3             if (this.Parent.Parent.Parent != null && this.Parent.Parent.Parent.Name == "panChildGrid" && this.panLeft.Tag.ToInt() == 1)
4             {
5                 int intHeight = this.Parent.Parent.Controls[0].Controls.ToArray().Sum(p => p.Height);
6                 if (this.Parent.Parent.Parent.Height != intHeight)
7                     this.Parent.Parent.Parent.Height = intHeight;
8             }
9         }

完整代码

代码语言:javascript
复制
  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 using System.Collections;
 10 
 11 namespace HZH_Controls.Controls
 12 {
 13     [ToolboxItem(false)]
 14     public partial class UCDataGridViewTreeRow : UserControl, IDataGridViewRow
 15     {
 16         #region 属性
 17         public event DataGridViewEventHandler CheckBoxChangeEvent;
 18 
 19         public event DataGridViewEventHandler CellClick;
 20 
 21         public event DataGridViewEventHandler SourceChanged;
 22 
 23         public List<DataGridViewColumnEntity> Columns
 24         {
 25             get;
 26             set;
 27         }
 28 
 29         public object DataSource
 30         {
 31             get;
 32             set;
 33         }
 34 
 35         public bool IsShowCheckBox
 36         {
 37             get;
 38             set;
 39         }
 40         private bool m_isChecked;
 41         public bool IsChecked
 42         {
 43             get
 44             {
 45                 return m_isChecked;
 46             }
 47 
 48             set
 49             {
 50                 if (m_isChecked != value)
 51                 {
 52                     m_isChecked = value;
 53                     (this.panCells.Controls.Find("check", false)[0] as UCCheckBox).Checked = value;
 54                 }
 55             }
 56         }
 57 
 58         int m_rowHeight = 40;
 59         public int RowHeight
 60         {
 61             get
 62             {
 63                 return m_rowHeight;
 64             }
 65             set
 66             {
 67                 m_rowHeight = value;
 68                 this.Height = value;
 69             }
 70         }
 71         #endregion
 72 
 73         public UCDataGridViewTreeRow()
 74         {
 75             InitializeComponent();
 76             this.ucDGVChild.RowType = this.GetType();
 77             this.ucDGVChild.IsAutoHeight = true;
 78             this.SizeChanged += UCDataGridViewTreeRow_SizeChanged;
 79             this.ucDGVChild.ItemClick += (a, b) =>
 80             {
 81                 if (CellClick != null)
 82                 {
 83                     CellClick(a, new DataGridViewEventArgs()
 84                     {
 85                         CellControl = (a as Control),
 86                         CellIndex = (a as Control).Tag.ToInt()
 87                     });
 88                 }
 89             };
 90 
 91         }
 92 
 93         void UCDataGridViewTreeRow_SizeChanged(object sender, EventArgs e)
 94         {
 95             if (this.Parent.Parent.Parent != null && this.Parent.Parent.Parent.Name == "panChildGrid" && this.panLeft.Tag.ToInt() == 1)
 96             {
 97                 int intHeight = this.Parent.Parent.Controls[0].Controls.ToArray().Sum(p => p.Height);
 98                 if (this.Parent.Parent.Parent.Height != intHeight)
 99                     this.Parent.Parent.Parent.Height = intHeight;
100             }
101         }
102 
103 
104         public void BindingCellData()
105         {
106             for (int i = 0; i < Columns.Count; i++)
107             {
108                 DataGridViewColumnEntity com = Columns[i];
109                 var cs = this.panCells.Controls.Find("lbl_" + com.DataField, false);
110                 if (cs != null && cs.Length > 0)
111                 {
112                     var pro = DataSource.GetType().GetProperty(com.DataField);
113                     if (pro != null)
114                     {
115                         var value = pro.GetValue(DataSource, null);
116                         if (com.Format != null)
117                         {
118                             cs[0].Text = com.Format(value);
119                         }
120                         else
121                         {
122                             cs[0].Text = value.ToStringExt();
123                         }
124                     }
125                 }
126             }
127             panLeft.Tag = 0;
128             var proChildrens = DataSource.GetType().GetProperty("Childrens");
129             if (proChildrens != null)
130             {
131                 var value = proChildrens.GetValue(DataSource, null);
132                 if (value != null)
133                 {
134                     int intSourceCount = 0;
135                     if (value is DataTable)
136                     {
137                         intSourceCount = (value as DataTable).Rows.Count;
138                     }
139                     else if (typeof(IList).IsAssignableFrom(value.GetType()))
140                     {
141                         intSourceCount = (value as IList).Count;
142                     }
143                     if (intSourceCount > 0)
144                     {
145                         panLeft.BackgroundImage = Properties.Resources.caret_right;
146                         panLeft.Enabled = true;
147                         panChildGrid.Tag = value;
148                     }
149                     else
150                     {
151                         panLeft.BackgroundImage = null;
152                         panLeft.Enabled = false;
153                         panChildGrid.Tag = null;
154                     }
155                 }
156                 else
157                 {
158                     panLeft.BackgroundImage = null;
159                     panLeft.Enabled = false;
160                     panChildGrid.Tag = null;
161                 }
162             }
163             else
164             {
165                 panLeft.BackgroundImage = null;
166                 panLeft.Enabled = false;
167                 panChildGrid.Tag = null;
168             }
169         }
170 
171         void Item_MouseDown(object sender, MouseEventArgs e)
172         {
173             if (CellClick != null)
174             {
175                 CellClick(this, new DataGridViewEventArgs()
176                 {
177                     CellControl = this,
178                     CellIndex = (sender as Control).Tag.ToInt()
179                 });
180             }
181         }
182 
183         public void SetSelect(bool blnSelected)
184         {
185             if (blnSelected)
186             {
187                 this.panMain.BackColor = Color.FromArgb(255, 247, 245);
188             }
189             else
190             {
191                 this.panMain.BackColor = Color.Transparent;
192             }
193         }
194 
195         public void ReloadCells()
196         {
197             try
198             {
199                 ControlHelper.FreezeControl(this, true);
200                 this.panCells.Controls.Clear();
201                 this.panCells.ColumnStyles.Clear();
202 
203                 int intColumnsCount = Columns.Count();
204                 if (Columns != null && intColumnsCount > 0)
205                 {
206                     if (IsShowCheckBox)
207                     {
208                         intColumnsCount++;
209                     }
210                     this.panCells.ColumnCount = intColumnsCount;
211                     for (int i = 0; i < intColumnsCount; i++)
212                     {
213                         Control c = null;
214                         if (i == 0 && IsShowCheckBox)
215                         {
216                             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(SizeType.Absolute, 30F));
217 
218                             UCCheckBox box = new UCCheckBox();
219                             box.Name = "check";
220                             box.TextValue = "";
221                             box.Size = new Size(30, 30);
222                             box.Dock = DockStyle.Fill;
223                             box.CheckedChangeEvent += (a, b) =>
224                             {
225                                 IsChecked = box.Checked;
226                                 this.ucDGVChild.Rows.ForEach(p => p.IsChecked = box.Checked);
227                                 if (CheckBoxChangeEvent != null)
228                                 {
229                                     CheckBoxChangeEvent(a, new DataGridViewEventArgs()
230                                     {
231                                         CellControl = box,
232                                         CellIndex = 0
233                                     });
234                                 }
235                             };
236                             c = box;
237                         }
238                         else
239                         {
240                             var item = Columns[i - (IsShowCheckBox ? 1 : 0)];
241                             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(item.WidthType, item.Width));
242 
243                             Label lbl = new Label();
244                             lbl.Tag = i - (IsShowCheckBox ? 1 : 0);
245                             lbl.Name = "lbl_" + item.DataField;
246                             lbl.Font = new Font("微软雅黑", 12);
247                             lbl.ForeColor = Color.Black;
248                             lbl.AutoSize = false;
249                             lbl.Dock = DockStyle.Fill;
250                             lbl.TextAlign = ContentAlignment.MiddleCenter;
251                             lbl.MouseDown += (a, b) =>
252                             {
253                                 Item_MouseDown(a, b);
254                             };
255                             c = lbl;
256                         }
257                         this.panCells.Controls.Add(c, i, 0);
258                     }
259 
260                 }
261             }
262             finally
263             {
264                 ControlHelper.FreezeControl(this, false);
265             }
266         }
267 
268         private void panChildGrid_SizeChanged(object sender, EventArgs e)
269         {
270             int intHeight = RowHeight + panChildGrid.Height;
271             if (panChildGrid.Height != 0)
272                 this.ucDGVChild.Height = panChildGrid.Height;
273             if (this.Height != intHeight)
274                 this.Height = intHeight;
275         }
276 
277         private void panLeft_MouseDown(object sender, MouseEventArgs e)
278         {
279             try
280             {
281                 ControlHelper.FreezeControl(this.FindForm(), true);
282                 if (panLeft.Tag.ToInt() == 0)
283                 {
284                     var value = panChildGrid.Tag;
285                     if (value != null)
286                     {
287                         panLeft.BackgroundImage = Properties.Resources.caret_down;
288                         panLeft.Tag = 1;
289                         int intSourceCount = 0;
290                         if (value is DataTable)
291                         {
292                             intSourceCount = (value as DataTable).Rows.Count;
293                         }
294                         else if (typeof(IList).IsAssignableFrom(value.GetType()))
295                         {
296                             intSourceCount = (value as IList).Count;
297                         }
298                         this.panChildGrid.Height = RowHeight * intSourceCount;
299                         if (panChildGrid.Height > 0)
300                         {
301                             if (value != this.ucDGVChild.DataSource)
302                             {
303                                 this.ucDGVChild.Columns = Columns;
304                                 this.ucDGVChild.RowHeight = RowHeight;
305                                 this.ucDGVChild.HeadPadingLeft = this.panLeft.Width;
306                                 this.ucDGVChild.IsShowCheckBox = IsShowCheckBox;
307                                 this.ucDGVChild.DataSource = value;
308                                 this.ucDGVChild.Rows.ForEach(p => p.IsChecked =this.IsChecked);
309                             }
310                         }
311                     }
312 
313                 }
314                 else
315                 {
316                     panLeft.Tag = 0;
317                     panChildGrid.Height = 0;
318                     panLeft.BackgroundImage = Properties.Resources.caret_right;
319                 }
320             }
321             finally
322             {
323                 ControlHelper.FreezeControl(this.FindForm(), false);
324             }
325         }
326 
327         private void ucDGVChild_SizeChanged(object sender, EventArgs e)
328         {
329 
330         }
331     }
332 }
代码语言:javascript
复制
  1 namespace HZH_Controls.Controls
  2 {
  3     partial class UCDataGridViewTreeRow
  4     {
  5         /// <summary> 
  6         /// 必需的设计器变量。
  7         /// </summary>
  8         private System.ComponentModel.IContainer components = null;
  9 
 10         /// <summary> 
 11         /// 清理所有正在使用的资源。
 12         /// </summary>
 13         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
 14         protected override void Dispose(bool disposing)
 15         {
 16             if (disposing && (components != null))
 17             {
 18                 components.Dispose();
 19             }
 20             base.Dispose(disposing);
 21         }
 22 
 23         #region 组件设计器生成的代码
 24 
 25         /// <summary> 
 26         /// 设计器支持所需的方法 - 不要
 27         /// 使用代码编辑器修改此方法的内容。
 28         /// </summary>
 29         private void InitializeComponent()
 30         {
 31             this.panCells = new System.Windows.Forms.TableLayoutPanel();
 32             this.panLeft = new System.Windows.Forms.Panel();
 33             this.panChildGrid = new System.Windows.Forms.Panel();
 34             this.panChildLeft = new System.Windows.Forms.Panel();
 35             this.panMain = new System.Windows.Forms.Panel();
 36             this.ucSplitLine_H1 = new HZH_Controls.Controls.UCSplitLine_H();
 37             this.ucDGVChild = new HZH_Controls.Controls.UCDataGridView();
 38             this.ucSplitLine_V1 = new HZH_Controls.Controls.UCSplitLine_V();
 39             this.panChildGrid.SuspendLayout();
 40             this.panMain.SuspendLayout();
 41             this.SuspendLayout();
 42             // 
 43             // panCells
 44             // 
 45             this.panCells.ColumnCount = 1;
 46             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
 47             this.panCells.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
 48             this.panCells.Dock = System.Windows.Forms.DockStyle.Fill;
 49             this.panCells.Location = new System.Drawing.Point(24, 0);
 50             this.panCells.Name = "panCells";
 51             this.panCells.RowCount = 1;
 52             this.panCells.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
 53             this.panCells.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 55F));
 54             this.panCells.Size = new System.Drawing.Size(637, 64);
 55             this.panCells.TabIndex = 2;
 56             // 
 57             // panLeft
 58             // 
 59             this.panLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
 60             this.panLeft.Dock = System.Windows.Forms.DockStyle.Left;
 61             this.panLeft.Location = new System.Drawing.Point(0, 0);
 62             this.panLeft.Name = "panLeft";
 63             this.panLeft.Size = new System.Drawing.Size(24, 64);
 64             this.panLeft.TabIndex = 0;
 65             this.panLeft.Tag = "0";
 66             this.panLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panLeft_MouseDown);
 67             // 
 68             // panChildGrid
 69             // 
 70             this.panChildGrid.Controls.Add(this.ucDGVChild);
 71             this.panChildGrid.Controls.Add(this.ucSplitLine_V1);
 72             this.panChildGrid.Controls.Add(this.panChildLeft);
 73             this.panChildGrid.Dock = System.Windows.Forms.DockStyle.Bottom;
 74             this.panChildGrid.Location = new System.Drawing.Point(0, 65);
 75             this.panChildGrid.Name = "panChildGrid";
 76             this.panChildGrid.Size = new System.Drawing.Size(661, 0);
 77             this.panChildGrid.TabIndex = 0;
 78             this.panChildGrid.SizeChanged += new System.EventHandler(this.panChildGrid_SizeChanged);
 79             // 
 80             // panChildLeft
 81             // 
 82             this.panChildLeft.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
 83             this.panChildLeft.Dock = System.Windows.Forms.DockStyle.Left;
 84             this.panChildLeft.Location = new System.Drawing.Point(0, 0);
 85             this.panChildLeft.Name = "panChildLeft";
 86             this.panChildLeft.Size = new System.Drawing.Size(24, 0);
 87             this.panChildLeft.TabIndex = 1;
 88             this.panChildLeft.Tag = "0";
 89             // 
 90             // panMain
 91             // 
 92             this.panMain.Controls.Add(this.panCells);
 93             this.panMain.Controls.Add(this.panLeft);
 94             this.panMain.Dock = System.Windows.Forms.DockStyle.Fill;
 95             this.panMain.Location = new System.Drawing.Point(0, 0);
 96             this.panMain.Name = "panMain";
 97             this.panMain.Size = new System.Drawing.Size(661, 64);
 98             this.panMain.TabIndex = 0;
 99             // 
100             // ucSplitLine_H1
101             // 
102             this.ucSplitLine_H1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
103             this.ucSplitLine_H1.Dock = System.Windows.Forms.DockStyle.Bottom;
104             this.ucSplitLine_H1.Location = new System.Drawing.Point(0, 64);
105             this.ucSplitLine_H1.Name = "ucSplitLine_H1";
106             this.ucSplitLine_H1.Size = new System.Drawing.Size(661, 1);
107             this.ucSplitLine_H1.TabIndex = 1;
108             this.ucSplitLine_H1.TabStop = false;
109             // 
110             // ucDGVChild
111             // 
112             this.ucDGVChild.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
113             | System.Windows.Forms.AnchorStyles.Right)));
114             this.ucDGVChild.BackColor = System.Drawing.Color.White;
115             this.ucDGVChild.HeadFont = new System.Drawing.Font("微软雅黑", 12F);
116             this.ucDGVChild.HeadHeight = 40;
117             this.ucDGVChild.HeadPadingLeft = 0;
118             this.ucDGVChild.HeadTextColor = System.Drawing.Color.Black;
119             this.ucDGVChild.IsAutoHeight = false;
120             this.ucDGVChild.IsShowCheckBox = false;
121             this.ucDGVChild.IsShowHead = false;
122             this.ucDGVChild.Location = new System.Drawing.Point(25, 0);
123             this.ucDGVChild.Name = "ucDGVChild";
124             this.ucDGVChild.Page = null;
125             this.ucDGVChild.RowHeight = 40;
126             this.ucDGVChild.RowType = typeof(HZH_Controls.Controls.UCDataGridViewRow);
127             this.ucDGVChild.Size = new System.Drawing.Size(636, 100);
128             this.ucDGVChild.TabIndex = 0;
129             this.ucDGVChild.SizeChanged += new System.EventHandler(this.ucDGVChild_SizeChanged);
130             // 
131             // ucSplitLine_V1
132             // 
133             this.ucSplitLine_V1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
134             this.ucSplitLine_V1.Dock = System.Windows.Forms.DockStyle.Left;
135             this.ucSplitLine_V1.Location = new System.Drawing.Point(24, 0);
136             this.ucSplitLine_V1.Name = "ucSplitLine_V1";
137             this.ucSplitLine_V1.Size = new System.Drawing.Size(1, 0);
138             this.ucSplitLine_V1.TabIndex = 0;
139             this.ucSplitLine_V1.TabStop = false;
140             // 
141             // UCDataGridViewTreeRow
142             // 
143             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
144             this.BackColor = System.Drawing.Color.White;
145             this.Controls.Add(this.panMain);
146             this.Controls.Add(this.ucSplitLine_H1);
147             this.Controls.Add(this.panChildGrid);
148             this.Name = "UCDataGridViewTreeRow";
149             this.Size = new System.Drawing.Size(661, 65);
150             this.panChildGrid.ResumeLayout(false);
151             this.panMain.ResumeLayout(false);
152             this.ResumeLayout(false);
153 
154         }
155 
156         #endregion
157 
158         private System.Windows.Forms.TableLayoutPanel panCells;
159         private UCSplitLine_H ucSplitLine_H1;
160         private System.Windows.Forms.Panel panLeft;
161         private System.Windows.Forms.Panel panChildGrid;
162         private UCDataGridView ucDGVChild;
163         private System.Windows.Forms.Panel panChildLeft;
164         private System.Windows.Forms.Panel panMain;
165         private UCSplitLine_V ucSplitLine_V1;
166     }
167 }

我这里写死了,如果要使用树表格的话,子数据就要用属性Childrens,比如下面的实体就是了

代码语言:javascript
复制
public class TestModel
    {
        public string ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime Birthday { get; set; }
        public int Sex { get; set; }
        public List<TestModel> Childrens { get; set; }
    }

使用实例如下

代码语言:javascript
复制
 1 this.ucDataGridView1.RowType = typeof(UCDataGridViewTreeRow);
 2             this.ucDataGridView1.IsAutoHeight = true;
 3 
 4             List<DataGridViewColumnEntity> lstCulumns = new List<DataGridViewColumnEntity>();
 5             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "ID", HeadText = "编号", Width = 70, WidthType = SizeType.Absolute });
 6             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Name", HeadText = "姓名", Width = 50, WidthType = SizeType.Percent });
 7             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Age", HeadText = "年龄", Width = 50, WidthType = SizeType.Percent });
 8             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Birthday", HeadText = "生日", Width = 50, WidthType = SizeType.Percent, Format = (a) => { return ((DateTime)a).ToString("yyyy-MM-dd"); } });
 9             lstCulumns.Add(new DataGridViewColumnEntity() { DataField = "Sex", HeadText = "性别", Width = 50, WidthType = SizeType.Percent, Format = (a) => { return ((int)a) == 0 ? "女" : "男"; } });
10             this.ucDataGridView1.Columns = lstCulumns;
11             this.ucDataGridView1.IsShowCheckBox = true;
12             List<object> lstSource = new List<object>();
13             for (int i = 0; i < 200; i++)
14             {
15                 TestModel model = new TestModel()
16                 {
17                     ID = i.ToString(),
18                     Age = 3 * i,
19                     Name = "姓名——" + i,
20                     Birthday = DateTime.Now.AddYears(-10),
21                     Sex = i % 2
22                 };
23                 lstSource.Add(model);
24                 AddChilds(model, 5);
25             }
26 
27             var page = new UCPagerControl2();
28             page.DataSource = lstSource;
29             this.ucDataGridView1.Page = page;
30             this.ucDataGridView1.First();
31 
32 
33 
34 
35   private void AddChilds(TestModel tm, int intCount)
36         {
37             if (intCount <= 0)
38                 return;
39             tm.Childrens = new List<TestModel>();
40             for (int i = 0; i < 5; i++)
41             {
42                 TestModel model = new TestModel()
43                 {
44                     ID = i.ToString(),
45                     Age = 3 * i,
46                     Name = intCount + "——" + i,
47                     Birthday = DateTime.Now.AddYears(-10),
48                     Sex = i % 2
49                 };
50                 tm.Childrens.Add(model);
51                 AddChilds(model, intCount - 1);
52             }
53         }

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-08-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前提
  • NuGet
  • 目录
  • 用处及效果
  • 准备工作
  • 开始
  • 最后的话
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档