欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-21 17:55:55
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:https://gitee.com/kwwwvagaa/net_winform_custom_contr ......

前提

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

github:https://github.com/kwwwvagaa/netwinformcontrol

码云:

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

欢迎前来交流探讨: 企鹅群568015492 

麻烦博客下方点个【推荐】,谢谢

nuget

install-package hzh_controls

目录

用处及效果

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

准备工作

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

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

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

开始

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

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

 

添加一个用户控件,命名ucdatagridviewtreerow,实现接口idatagridviewrow

属性

 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 ,这个是将子节点表格的点击事件向上传递

 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,主要处理一下数据绑定后将子节点数据向下传递

 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         }

实现函数绑定列

 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         }

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

 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 在大小改变的时候,处理父级表格的大小。来防止出现多个滚动条

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         }

完整代码

  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 }
  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,比如下面的实体就是了

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; }
    }

使用实例如下

 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         }

 

最后的话

如果你喜欢的话,请到  点个星星吧