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

(四十九)c#Winform自定义控件-下拉框(表格)

程序员文章站 2022-07-02 13:19:38
前提 入行已经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自定义控件-下拉框(表格)

 1   list<datagridviewcolumnentity> lstculumns = new list<datagridviewcolumnentity>();
 2             lstculumns.add(new datagridviewcolumnentity() { datafield = "id", headtext = "编号", width = 70, widthtype = sizetype.absolute });
 3             lstculumns.add(new datagridviewcolumnentity() { datafield = "name", headtext = "姓名", width = 100, widthtype = sizetype.absolute });
 4             lstculumns.add(new datagridviewcolumnentity() { datafield = "age", headtext = "年龄", width = 100, widthtype = sizetype.absolute });
 5             lstculumns.add(new datagridviewcolumnentity() { datafield = "birthday", headtext = "生日", width = 120, widthtype = sizetype.absolute, format = (a) => { return ((datetime)a).tostring("yyyy-mm-dd"); } });
 6             lstculumns.add(new datagridviewcolumnentity() { datafield = "sex", headtext = "性别", width = 100, widthtype = sizetype.absolute, format = (a) => { return ((int)a) == 0 ? "女" : "男"; } });
 7             this.uccomboxgrid1.gridcolumns = lstculumns;
 8             list<object> lstsourcegrid = new list<object>();
 9             for (int i = 0; i < 100; i++)
10             {
11                 testmodel model = new testmodel()
12                 {
13                     id = i.tostring(),
14                     age = 3 * i,
15                     name = "姓名——" + i,
16                     birthday = datetime.now.addyears(-10),
17                     sex = i % 2
18                 };
19                 lstsourcegrid.add(model);
20             }
21             this.uccomboxgrid1.griddatasource = lstsourcegrid;

 

准备工作

此控件继承自uccombox,并且需要表格控件ucdatagridview,如果不了解请移步查看

(三十五)c#winform自定义控件-下拉框

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

如果你想显示树形结构,请移步查看

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

开始

我们首先需要一个弹出的面板,那么我们先添加一个用户控件uccomboxgridpanel,在这个用户控件上添加一个文本框进行搜索,添加一个表格展示数据

一些属性

 1  [description("项点击事件"), category("自定义")]
 2         public event datagridvieweventhandler itemclick;
 3         private type m_rowtype = typeof(ucdatagridviewrow);
 4 
 5         public type rowtype
 6         {
 7             get { return m_rowtype; }
 8             set
 9             {
10                 m_rowtype = value;
11                 this.ucdatagridview1.rowtype = m_rowtype;
12             }
13         }
14 
15         private list<datagridviewcolumnentity> m_columns = null;
16 
17         public list<datagridviewcolumnentity> columns
18         {
19             get { return m_columns; }
20             set
21             {
22                 m_columns = value;
23                 this.ucdatagridview1.columns = value;
24             }
25         }
26         private list<object> m_datasource = null;
27 
28         public list<object> datasource
29         {
30             get { return m_datasource; }
31             set { m_datasource = value; }
32         }
33 
34         private string strlastsearchtext = string.empty;
35         ucpagercontrol m_page = new ucpagercontrol();

一些事件,处理数据绑定

 1  void ucdatagridview1_itemclick(object sender, datagridvieweventargs e)
 2         {
 3             if (itemclick != null)
 4             {
 5                 itemclick((sender as idatagridviewrow).datasource, null);
 6             }
 7         }
 8 
 9         void txtinput_textchanged(object sender, eventargs e)
10         {
11             timer1.enabled = false;
12             timer1.enabled = true;
13         }
14 
15         private void uccomboxgridpanel_load(object sender, eventargs e)
16         {
17             m_page.datasource = m_datasource;
18             this.ucdatagridview1.datasource = m_page.getcurrentsource();
19         }
20 
21         private void timer1_tick(object sender, eventargs e)
22         {
23             if (strlastsearchtext == txtsearch.inputtext)
24             {
25                 timer1.enabled = false;
26             }
27             else
28             {
29                 strlastsearchtext = txtsearch.inputtext;
30                 search(txtsearch.inputtext);
31             }
32         }
33 
34         private void search(string strtext)
35         {
36             m_page.startindex = 0;
37             if (!string.isnullorempty(strtext))
38             {
39                 strtext = strtext.tolower();
40                 list<object> lst = m_datasource.findall(p => m_columns.any(c => (c.format == null ? (p.gettype().getproperty(c.datafield).getvalue(p, null).tostringext()) : c.format(p.gettype().getproperty(c.datafield).getvalue(p, null))).tolower().contains(strtext)));
41                 m_page.datasource = lst;
42             }
43             else
44             {
45                 m_page.datasource = m_datasource;
46             }
47             m_page.reload();
48         }

完整代码

  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 
 10 namespace hzh_controls.controls.combobox
 11 {
 12     [toolboxitem(false)]
 13     public partial class uccomboxgridpanel : usercontrol
 14     {
 15         [description("项点击事件"), category("自定义")]
 16         public event datagridvieweventhandler itemclick;
 17         private type m_rowtype = typeof(ucdatagridviewrow);
 18 
 19         public type rowtype
 20         {
 21             get { return m_rowtype; }
 22             set
 23             {
 24                 m_rowtype = value;
 25                 this.ucdatagridview1.rowtype = m_rowtype;
 26             }
 27         }
 28 
 29         private list<datagridviewcolumnentity> m_columns = null;
 30 
 31         public list<datagridviewcolumnentity> columns
 32         {
 33             get { return m_columns; }
 34             set
 35             {
 36                 m_columns = value;
 37                 this.ucdatagridview1.columns = value;
 38             }
 39         }
 40         private list<object> m_datasource = null;
 41 
 42         public list<object> datasource
 43         {
 44             get { return m_datasource; }
 45             set { m_datasource = value; }
 46         }
 47 
 48         private string strlastsearchtext = string.empty;
 49         ucpagercontrol m_page = new ucpagercontrol();
 50 
 51         public uccomboxgridpanel()
 52         {
 53             initializecomponent();
 54             this.ucdatagridview1.page = m_page;
 55             this.ucdatagridview1.isautoheight = false;
 56             this.txtsearch.txtinput.textchanged += txtinput_textchanged;
 57             this.ucdatagridview1.itemclick += ucdatagridview1_itemclick;
 58         }
 59 
 60         void ucdatagridview1_itemclick(object sender, datagridvieweventargs e)
 61         {
 62             if (itemclick != null)
 63             {
 64                 itemclick((sender as idatagridviewrow).datasource, null);
 65             }
 66         }
 67 
 68         void txtinput_textchanged(object sender, eventargs e)
 69         {
 70             timer1.enabled = false;
 71             timer1.enabled = true;
 72         }
 73 
 74         private void uccomboxgridpanel_load(object sender, eventargs e)
 75         {
 76             m_page.datasource = m_datasource;
 77             this.ucdatagridview1.datasource = m_page.getcurrentsource();
 78         }
 79 
 80         private void timer1_tick(object sender, eventargs e)
 81         {
 82             if (strlastsearchtext == txtsearch.inputtext)
 83             {
 84                 timer1.enabled = false;
 85             }
 86             else
 87             {
 88                 strlastsearchtext = txtsearch.inputtext;
 89                 search(txtsearch.inputtext);
 90             }
 91         }
 92 
 93         private void search(string strtext)
 94         {
 95             m_page.startindex = 0;
 96             if (!string.isnullorempty(strtext))
 97             {
 98                 strtext = strtext.tolower();
 99                 list<object> lst = m_datasource.findall(p => m_columns.any(c => (c.format == null ? (p.gettype().getproperty(c.datafield).getvalue(p, null).tostringext()) : c.format(p.gettype().getproperty(c.datafield).getvalue(p, null))).tolower().contains(strtext)));
100                 m_page.datasource = lst;
101             }
102             else
103             {
104                 m_page.datasource = m_datasource;
105             }
106             m_page.reload();
107         }
108     }
109 }
  1 namespace hzh_controls.controls.combobox
  2 {
  3     partial class uccomboxgridpanel
  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.components = new system.componentmodel.container();
 32             this.panel1 = new system.windows.forms.panel();
 33             this.panel2 = new system.windows.forms.panel();
 34             this.timer1 = new system.windows.forms.timer(this.components);
 35             this.uccontrolbase1 = new hzh_controls.controls.uccontrolbase();
 36             this.ucdatagridview1 = new hzh_controls.controls.ucdatagridview();
 37             this.txtsearch = new hzh_controls.controls.uctextboxex();
 38             this.ucsplitline_v2 = new hzh_controls.controls.ucsplitline_v();
 39             this.ucsplitline_v1 = new hzh_controls.controls.ucsplitline_v();
 40             this.ucsplitline_h2 = new hzh_controls.controls.ucsplitline_h();
 41             this.ucsplitline_h1 = new hzh_controls.controls.ucsplitline_h();
 42             this.panel1.suspendlayout();
 43             this.uccontrolbase1.suspendlayout();
 44             this.suspendlayout();
 45             // 
 46             // panel1
 47             // 
 48             this.panel1.controls.add(this.uccontrolbase1);
 49             this.panel1.controls.add(this.panel2);
 50             this.panel1.controls.add(this.txtsearch);
 51             this.panel1.dock = system.windows.forms.dockstyle.fill;
 52             this.panel1.location = new system.drawing.point(1, 1);
 53             this.panel1.name = "panel1";
 54             this.panel1.padding = new system.windows.forms.padding(5);
 55             this.panel1.size = new system.drawing.size(447, 333);
 56             this.panel1.tabindex = 4;
 57             // 
 58             // panel2
 59             // 
 60             this.panel2.dock = system.windows.forms.dockstyle.top;
 61             this.panel2.location = new system.drawing.point(5, 47);
 62             this.panel2.name = "panel2";
 63             this.panel2.size = new system.drawing.size(437, 15);
 64             this.panel2.tabindex = 1;
 65             // 
 66             // timer1
 67             // 
 68             this.timer1.interval = 1000;
 69             this.timer1.tick += new system.eventhandler(this.timer1_tick);
 70             // 
 71             // uccontrolbase1
 72             // 
 73             this.uccontrolbase1.conerradius = 5;
 74             this.uccontrolbase1.controls.add(this.ucdatagridview1);
 75             this.uccontrolbase1.dock = system.windows.forms.dockstyle.fill;
 76             this.uccontrolbase1.fillcolor = system.drawing.color.transparent;
 77             this.uccontrolbase1.font = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
 78             this.uccontrolbase1.isradius = false;
 79             this.uccontrolbase1.isshowrect = true;
 80             this.uccontrolbase1.location = new system.drawing.point(5, 62);
 81             this.uccontrolbase1.margin = new system.windows.forms.padding(4, 5, 4, 5);
 82             this.uccontrolbase1.name = "uccontrolbase1";
 83             this.uccontrolbase1.padding = new system.windows.forms.padding(5);
 84             this.uccontrolbase1.rectcolor = system.drawing.color.fromargb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220)))));
 85             this.uccontrolbase1.rectwidth = 1;
 86             this.uccontrolbase1.size = new system.drawing.size(437, 266);
 87             this.uccontrolbase1.tabindex = 2;
 88             this.uccontrolbase1.tabstop = false;
 89             // 
 90             // ucdatagridview1
 91             // 
 92             this.ucdatagridview1.backcolor = system.drawing.color.white;
 93             this.ucdatagridview1.columns = null;
 94             this.ucdatagridview1.datasource = null;
 95             this.ucdatagridview1.dock = system.windows.forms.dockstyle.fill;
 96             this.ucdatagridview1.headfont = new system.drawing.font("微软雅黑", 12f);
 97             this.ucdatagridview1.headheight = 40;
 98             this.ucdatagridview1.headpadingleft = 0;
 99             this.ucdatagridview1.headtextcolor = system.drawing.color.black;
100             this.ucdatagridview1.isautoheight = false;
101             this.ucdatagridview1.isshowcheckbox = false;
102             this.ucdatagridview1.isshowhead = true;
103             this.ucdatagridview1.location = new system.drawing.point(5, 5);
104             this.ucdatagridview1.name = "ucdatagridview1";
105             this.ucdatagridview1.page = null;
106             this.ucdatagridview1.rowheight = 30;
107             this.ucdatagridview1.rowtype = typeof(hzh_controls.controls.ucdatagridviewrow);
108             this.ucdatagridview1.size = new system.drawing.size(427, 256);
109             this.ucdatagridview1.tabindex = 0;
110             this.ucdatagridview1.tabstop = false;
111             // 
112             // txtsearch
113             // 
114             this.txtsearch.backcolor = system.drawing.color.transparent;
115             this.txtsearch.conerradius = 5;
116             this.txtsearch.cursor = system.windows.forms.cursors.ibeam;
117             this.txtsearch.declength = 2;
118             this.txtsearch.dock = system.windows.forms.dockstyle.top;
119             this.txtsearch.fillcolor = system.drawing.color.empty;
120             this.txtsearch.font = new system.drawing.font("微软雅黑", 18f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
121             this.txtsearch.inputtext = "";
122             this.txtsearch.inputtype = hzh_controls.textinputtype.notcontrol;
123             this.txtsearch.isfocuscolor = true;
124             this.txtsearch.isradius = true;
125             this.txtsearch.isshowclearbtn = true;
126             this.txtsearch.isshowkeyboard = false;
127             this.txtsearch.isshowrect = true;
128             this.txtsearch.isshowsearchbtn = false;
129             this.txtsearch.keyboardtype = hzh_controls.controls.keyboardtype.uckeyborderall_en;
130             this.txtsearch.location = new system.drawing.point(5, 5);
131             this.txtsearch.margin = new system.windows.forms.padding(4, 5, 4, 5);
132             this.txtsearch.maxvalue = new decimal(new int[] {
133             1000000,
134             0,
135             0,
136             0});
137             this.txtsearch.minvalue = new decimal(new int[] {
138             1000000,
139             0,
140             0,
141             -2147483648});
142             this.txtsearch.name = "txtsearch";
143             this.txtsearch.padding = new system.windows.forms.padding(5);
144             this.txtsearch.promptcolor = system.drawing.color.gray;
145             this.txtsearch.promptfont = new system.drawing.font("微软雅黑", 15f, system.drawing.fontstyle.regular, system.drawing.graphicsunit.pixel);
146             this.txtsearch.prompttext = "输入内容模糊搜索";
147             this.txtsearch.rectcolor = system.drawing.color.fromargb(((int)(((byte)(220)))), ((int)(((byte)(220)))), ((int)(((byte)(220)))));
148             this.txtsearch.rectwidth = 1;
149             this.txtsearch.regexpattern = "";
150             this.txtsearch.size = new system.drawing.size(437, 42);
151             this.txtsearch.tabindex = 0;
152             // 
153             // ucsplitline_v2
154             // 
155             this.ucsplitline_v2.backcolor = system.drawing.color.fromargb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
156             this.ucsplitline_v2.dock = system.windows.forms.dockstyle.right;
157             this.ucsplitline_v2.location = new system.drawing.point(448, 1);
158             this.ucsplitline_v2.name = "ucsplitline_v2";
159             this.ucsplitline_v2.size = new system.drawing.size(1, 333);
160             this.ucsplitline_v2.tabindex = 3;
161             this.ucsplitline_v2.tabstop = false;
162             // 
163             // ucsplitline_v1
164             // 
165             this.ucsplitline_v1.backcolor = system.drawing.color.fromargb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
166             this.ucsplitline_v1.dock = system.windows.forms.dockstyle.left;
167             this.ucsplitline_v1.location = new system.drawing.point(0, 1);
168             this.ucsplitline_v1.name = "ucsplitline_v1";
169             this.ucsplitline_v1.size = new system.drawing.size(1, 333);
170             this.ucsplitline_v1.tabindex = 2;
171             this.ucsplitline_v1.tabstop = false;
172             // 
173             // ucsplitline_h2
174             // 
175             this.ucsplitline_h2.backcolor = system.drawing.color.fromargb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
176             this.ucsplitline_h2.dock = system.windows.forms.dockstyle.bottom;
177             this.ucsplitline_h2.location = new system.drawing.point(0, 334);
178             this.ucsplitline_h2.name = "ucsplitline_h2";
179             this.ucsplitline_h2.size = new system.drawing.size(449, 1);
180             this.ucsplitline_h2.tabindex = 1;
181             this.ucsplitline_h2.tabstop = false;
182             // 
183             // ucsplitline_h1
184             // 
185             this.ucsplitline_h1.backcolor = system.drawing.color.fromargb(((int)(((byte)(232)))), ((int)(((byte)(232)))), ((int)(((byte)(232)))));
186             this.ucsplitline_h1.dock = system.windows.forms.dockstyle.top;
187             this.ucsplitline_h1.location = new system.drawing.point(0, 0);
188             this.ucsplitline_h1.name = "ucsplitline_h1";
189             this.ucsplitline_h1.size = new system.drawing.size(449, 1);
190             this.ucsplitline_h1.tabindex = 0;
191             this.ucsplitline_h1.tabstop = false;
192             // 
193             // uccomboxgridpanel
194             // 
195             this.autoscalemode = system.windows.forms.autoscalemode.none;
196             this.backcolor = system.drawing.color.white;
197             this.controls.add(this.panel1);
198             this.controls.add(this.ucsplitline_v2);
199             this.controls.add(this.ucsplitline_v1);
200             this.controls.add(this.ucsplitline_h2);
201             this.controls.add(this.ucsplitline_h1);
202             this.name = "uccomboxgridpanel";
203             this.size = new system.drawing.size(449, 335);
204             this.load += new system.eventhandler(this.uccomboxgridpanel_load);
205             this.panel1.resumelayout(false);
206             this.uccontrolbase1.resumelayout(false);
207             this.resumelayout(false);
208 
209         }
210 
211         #endregion
212 
213         private ucsplitline_h ucsplitline_h1;
214         private ucsplitline_h ucsplitline_h2;
215         private ucsplitline_v ucsplitline_v1;
216         private ucsplitline_v ucsplitline_v2;
217         private system.windows.forms.panel panel1;
218         private uccontrolbase uccontrolbase1;
219         private ucdatagridview ucdatagridview1;
220         private system.windows.forms.panel panel2;
221         private system.windows.forms.timer timer1;
222         private uctextboxex txtsearch;
223     }
224 }

以上,弹出面板完成,下面就是下拉控件了

添加一个用户控件uccomboxgrid,继承uccombox

一些属性

 1  private type m_rowtype = typeof(ucdatagridviewrow);
 2 
 3         [description("表格行类型"), category("自定义")]
 4         public type gridrowtype
 5         {
 6             get { return m_rowtype; }
 7             set
 8             {
 9                 m_rowtype = value;
10             }
11         }
12         int intwidth = 0;
13 
14         private list<datagridviewcolumnentity> m_columns = null;
15 
16         [description("表格列"), category("自定义")]
17         public list<datagridviewcolumnentity> gridcolumns
18         {
19             get { return m_columns; }
20             set
21             {
22                 m_columns = value;
23                 if (value != null)
24                     intwidth = value.sum(p => p.widthtype == sizetype.absolute ? p.width : (p.width < 80 ? 80 : p.width));
25             }
26         }
27         private list<object> m_datasource = null;
28 
29         [description("表格数据源"), category("自定义")]
30         public list<object> griddatasource
31         {
32             get { return m_datasource; }
33             set { m_datasource = value; }
34         }
35 
36         private string m_textfield;
37 
38         [description("显示值字段名称"), category("自定义")]
39         public string textfield
40         {
41             get { return m_textfield; }
42             set
43             {
44                 m_textfield = value;
45                 settext();
46             }
47         }
48         [obsolete("不再可用的属性")]
49         [browsable(false), editorbrowsable(editorbrowsablestate.never)]
50         private new comboboxstyle boxstyle
51         {
52             get;
53             set;
54         }
55         private object selectsource = null;
56 
57         [description("选中的数据源"), category("自定义")]
58         public object selectsource
59         {
60             get { return selectsource; }
61             set
62             {
63                 selectsource = value;
64                 settext();
65                 if (selectedchangedevent != null)
66                 {
67                     selectedchangedevent(value, null);
68                 }
69             }
70         }
71 
72  [description("选中数据源改变事件"), category("自定义")]
73         public new event eventhandler selectedchangedevent;

重写点击事件来处理弹出

 1  protected override void click_mousedown(object sender, mouseeventargs e)
 2         {
 3             if (m_columns == null || m_columns.count <= 0)
 4                 return;
 5             if (m_ucpanel == null)
 6             {
 7                 var p = this.parent.pointtoscreen(this.location);
 8                 int intscreenheight = screen.primaryscreen.bounds.height;
 9                 int intheight = math.max(p.y, intscreenheight - p.y - this.height);
10                 intheight -= 100;
11                 m_ucpanel = new uccomboxgridpanel();
12                 m_ucpanel.itemclick += m_ucpanel_itemclick;
13                 m_ucpanel.height = intheight;
14                 m_ucpanel.width = intwidth;
15                 m_ucpanel.columns = m_columns;
16                 m_ucpanel.rowtype = m_rowtype;
17                 if (m_datasource != null && m_datasource.count > 0)
18                 {
19                     int _intheight = math.min(110 + m_datasource.count * 36, m_ucpanel.height);
20                     m_ucpanel.height = _intheight;
21                 }
22             }
23             m_ucpanel.datasource = m_datasource;
24             if (_frmanchor == null || _frmanchor.isdisposed || _frmanchor.visible == false)
25             {
26                 _frmanchor = new forms.frmanchor(this, m_ucpanel);
27                 _frmanchor.show(this.findform());
28             }
29 
30         }
31 
32         void m_ucpanel_itemclick(object sender, datagridvieweventargs e)
33         {
34             _frmanchor.hide();
35             selectsource = sender;
36         }
37 
38         private void settext()
39         {
40             if (!string.isnullorempty(m_textfield) && selectsource != null)
41             {
42                 var pro = selectsource.gettype().getproperty(m_textfield);
43                 if (pro != null)
44                 {
45                     textvalue = pro.getvalue(selectsource, null).tostringext();
46                 }
47             }
48         }

完整代码

  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 hzh_controls.controls;
 10 
 11 namespace hzh_controls.controls.combobox
 12 {
 13     public partial class uccomboxgrid : uccombox
 14     {
 15 
 16         private type m_rowtype = typeof(ucdatagridviewrow);
 17 
 18         [description("表格行类型"), category("自定义")]
 19         public type gridrowtype
 20         {
 21             get { return m_rowtype; }
 22             set
 23             {
 24                 m_rowtype = value;
 25             }
 26         }
 27         int intwidth = 0;
 28 
 29         private list<datagridviewcolumnentity> m_columns = null;
 30 
 31         [description("表格列"), category("自定义")]
 32         public list<datagridviewcolumnentity> gridcolumns
 33         {
 34             get { return m_columns; }
 35             set
 36             {
 37                 m_columns = value;
 38                 if (value != null)
 39                     intwidth = value.sum(p => p.widthtype == sizetype.absolute ? p.width : (p.width < 80 ? 80 : p.width));
 40             }
 41         }
 42         private list<object> m_datasource = null;
 43 
 44         [description("表格数据源"), category("自定义")]
 45         public list<object> griddatasource
 46         {
 47             get { return m_datasource; }
 48             set { m_datasource = value; }
 49         }
 50 
 51         private string m_textfield;
 52 
 53         [description("显示值字段名称"), category("自定义")]
 54         public string textfield
 55         {
 56             get { return m_textfield; }
 57             set
 58             {
 59                 m_textfield = value;
 60                 settext();
 61             }
 62         }
 63         [obsolete("不再可用的属性")]
 64         [browsable(false), editorbrowsable(editorbrowsablestate.never)]
 65         private new comboboxstyle boxstyle
 66         {
 67             get;
 68             set;
 69         }
 70         private object selectsource = null;
 71 
 72         [description("选中的数据源"), category("自定义")]
 73         public object selectsource
 74         {
 75             get { return selectsource; }
 76             set
 77             {
 78                 selectsource = value;
 79                 settext();
 80                 if (selectedchangedevent != null)
 81                 {
 82                     selectedchangedevent(value, null);
 83                 }
 84             }
 85         }
 86 
 87       
 88 
 89         [description("选中数据源改变事件"), category("自定义")]
 90         public new event eventhandler selectedchangedevent;
 91         public uccomboxgrid()
 92         {
 93             initializecomponent();
 94         }
 95         uccomboxgridpanel m_ucpanel = null;
 96         forms.frmanchor _frmanchor;
 97         protected override void click_mousedown(object sender, mouseeventargs e)
 98         {
 99             if (m_columns == null || m_columns.count <= 0)
100                 return;
101             if (m_ucpanel == null)
102             {
103                 var p = this.parent.pointtoscreen(this.location);
104                 int intscreenheight = screen.primaryscreen.bounds.height;
105                 int intheight = math.max(p.y, intscreenheight - p.y - this.height);
106                 intheight -= 100;
107                 m_ucpanel = new uccomboxgridpanel();
108                 m_ucpanel.itemclick += m_ucpanel_itemclick;
109                 m_ucpanel.height = intheight;
110                 m_ucpanel.width = intwidth;
111                 m_ucpanel.columns = m_columns;
112                 m_ucpanel.rowtype = m_rowtype;
113                 if (m_datasource != null && m_datasource.count > 0)
114                 {
115                     int _intheight = math.min(110 + m_datasource.count * 36, m_ucpanel.height);
116                     m_ucpanel.height = _intheight;
117                 }
118             }
119             m_ucpanel.datasource = m_datasource;
120             if (_frmanchor == null || _frmanchor.isdisposed || _frmanchor.visible == false)
121             {
122                 _frmanchor = new forms.frmanchor(this, m_ucpanel);
123                 _frmanchor.show(this.findform());
124             }
125 
126         }
127 
128         void m_ucpanel_itemclick(object sender, datagridvieweventargs e)
129         {
130             _frmanchor.hide();
131             selectsource = sender;
132         }
133 
134         private void settext()
135         {
136             if (!string.isnullorempty(m_textfield) && selectsource != null)
137             {
138                 var pro = selectsource.gettype().getproperty(m_textfield);
139                 if (pro != null)
140                 {
141                     textvalue = pro.getvalue(selectsource, null).tostringext();
142                 }
143             }
144         }
145     }
146 }
 1 namespace hzh_controls.controls.combobox
 2 {
 3     partial class uccomboxgrid
 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.suspendlayout();
32             // 
33             // txtinput
34             // 
35             this.txtinput.backcolor = system.drawing.color.fromargb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
36             // 
37             // uccomboxgrid
38             // 
39             this.autoscalemode = system.windows.forms.autoscalemode.none;
40             this.backcolor = system.drawing.color.transparent;
41             this.boxstyle = system.windows.forms.comboboxstyle.dropdownlist;
42             this.fillcolor = system.drawing.color.fromargb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
43             this.name = "uccomboxgrid";
44             this.rectcolor = system.drawing.color.fromargb(((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
45             this.resumelayout(false);
46             this.performlayout();
47 
48         }
49 
50         #endregion
51 
52     }
53 }

以上就是全部东西了

最后的话

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