本文将介绍如何在 system.windows.forms.datagrid中切入使用combobox控件,主要包括三方面的内容。

   1. 在datagrid中加入combobox列;

   2. 把在datagrid中的修改保存到对应的网格;

   3. 设置datagrid中网格的焦点。

   下面是整个源代码,一些功能可以看注释。

  1namespace datagridtest 
  2
  3  public class form1 : system.windows.forms.form 
  4  
  5   private system.windows.forms.datagrid dgdfunctionarea; 
  6   private datatable dtblfunctionalarea; 
  7   private system.windows.forms.button buttonfocus; 
  8   private system.componentmodel.container components = null; 
  9
 10   public form1() 
 11   
 12    initializecomponent(); 
 13    populategrid(); 
 14   } 
 15
 16   protected override void dispose( bool disposing ) 
 17   
 18    if( disposing ) 
 19    
 20     if (components != null) 
 21     
 22      components.dispose(); 
 23     } 
 24    } 
 25    base.dispose( disposing ); 
 26   } 
 27
 28   windows 窗体设计器生成的代码#region windows 窗体设计器生成的代码 
 29
 30   private void initializecomponent() 
 31   
 32    this.dgdfunctionarea = new system.windows.forms.datagrid(); 
 33    this.buttonfocus = new system.windows.forms.button(); 
 34   ((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).begininit(); 
 35    this.suspendlayout(); 
 36    / 
 37    / dgdfunctionarea 
 38    / 
 39    this.dgdfunctionarea.datamember = ""; 
 40    this.dgdfunctionarea.headerforecolor = system.drawing.systemcolors.controltext; 
 41
 42    this.dgdfunctionarea.location = new system.drawing.point(4, 8); 
 43    this.dgdfunctionarea.name = "dgdfunctionarea"; 
 44    this.dgdfunctionarea.size = new system.drawing.size(316, 168); 
 45    this.dgdfunctionarea.tabindex = 0; 
 46    / 
 47    / buttonfocus 
 48    / 
 49    this.buttonfocus.location = new system.drawing.point(232, 188); 
 50    this.buttonfocus.name = "buttonfocus"; 
 51    this.buttonfocus.size = new system.drawing.size(84, 23); 
 52    this.buttonfocus.tabindex = 1; 
 53    this.buttonfocus.text = "获取焦点"; 
 54    this.buttonfocus.click += new system.eventhandler(this.buttonfocus_click); 
 55    / 
 56    / form1 
 57    / 
 58    this.autoscalebasesize = new system.drawing.size(6, 14); 
 59    this.clientsize = new system.drawing.size(332, 217); 
 60    this.controls.add(this.buttonfocus); 
 61    this.controls.add(this.dgdfunctionarea); 
 62    this.name = "form1"; 
 63    this.text = "form1"; 
 64    ((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).endinit(); 
 65    this.resumelayout(false); 
 66
 67   } 
 68   #endregion 
 69   // <summary> 
 70   // 应用程序的主入口点。 
 71   // </summary> 
 72   [stathread] 
 73   static void main() 
 74   
 75    application.run(new form1()); 
 76   } 
 77   /初始化datagrid 
 78   private void populategrid() 
 79   
 80    /创建一个datatable对象,包括四列,前三列为string,最后一列为boolean。 
 81    dtblfunctionalarea = new datatable ("functionarea"); 
 82    string[] arrstrfunctionalarea = new string [3]{"functional area","min","max"}; 
 83    datacolumn dtcol = null; 
 84    /创建string列 
 85    for(int i=0; i< 3;i++) 
 86    
 87     dtcol = new datacolumn(arrstrfunctionalarea[i]); 
 88     dtcol.datatype = type.gettype("system.string"); 
 89     dtcol.defaultvalue = ""; 
 90     dtblfunctionalarea.columns.add(dtcol); 
 91    } 
 92
 93    /创建boolean列,用checkedbox来显示。 
 94    datacolumn dtccheck = new datacolumn("ismandatory"); 
 95    dtccheck.datatype = system.type.gettype("system.boolean"); 
 96    dtccheck.defaultvalue = false; 
 97    dtblfunctionalarea.columns.add(dtccheck); 
 98
 99    /把表绑定到datagrid 
100    dgdfunctionarea.datasource = dtblfunctionalarea; 
101
102    /为datagrid加载datagridtablestyle样式 
103    if(!dgdfunctionarea.tablestyles.contains("functionarea")) 
104    
105     datagridtablestyle dgdtblstyle = new datagridtablestyle(); 
106     dgdtblstyle.mappingname = dtblfunctionalarea.tablename; 
107     dgdfunctionarea.tablestyles.add(dgdtblstyle); 
108     dgdtblstyle.rowheadersvisible = false; 
109     dgdtblstyle.headerbackcolor = color.lightsteelblue; 
110     dgdtblstyle.allowsorting = false; 
111     dgdtblstyle.headerbackcolor = color.fromargb(8,36,107); 
112     dgdtblstyle.rowheadersvisible = false; 
113     dgdtblstyle.headerforecolor = color.white; 
114     dgdtblstyle.headerfont = new system.drawing.font("microsoft sans serif", 9f, 
115     system.drawing.fontstyle.bold, 
116     system.drawing.graphicsunit.point, ((system.byte)(0))); 
117     dgdtblstyle.gridlinecolor = color.darkgray; 
118     dgdtblstyle.preferredrowheight = 22; 
119     dgdfunctionarea.backgroundcolor = color.white; 
120
121     /设置列的宽度 
122     gridcolumnstylescollection colstyle = dgdfunctionarea.tablestyles[0].gridcolumnstyles; 
123     colstyle[0].width = 100; 
124     colstyle[1].width = 50; 
125     colstyle[2].width = 50; 
126     colstyle[3].width = 80; 
127    } 
128
129    datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[0]; 
130
131    combobox cmbfunctionarea = new combobox(); 
132    cmbfunctionarea.items.addrange(new object[]{"选项一","选项二","选项三"}); 
133    cmbfunctionarea.cursor = cursors.arrow; 
134    cmbfunctionarea.dropdownstyle= comboboxstyle.dropdownlist; 
135    cmbfunctionarea.dock = dockstyle.fill; 
136
137    /在选定项发生更改并且提交了该更改后发生 
138
139    cmbfunctionarea.selectionchangecommitted += new  eventhandler(cmbfunctionarea_selectionchangecommitted); 
140
141    /把combobox添加到datagridtablestyle的第一列 
142
143    dgtb.textbox.controls.add(cmbfunctionarea); 
144
145   } 
146
147   /设置焦点模拟 
148
149   private void getfocus(int row,int col) 
150   
151    /先把焦点移动到datagrid 
152    this.dgdfunctionarea.focus(); 
153    /把焦点移动到datagridcell 
154    datagridcell dgc = new datagridcell(row,col); 
155    this.dgdfunctionarea.currentcell = dgc; 
156    datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[col]; 
157
158    /设置焦点 
159
160    dgtb.textbox.focus(); 
161   } 
162
163   /把combobox上修改的数据提交到当前的网格 
164
165  private void cmbfunctionarea_selectionchangecommitted(object sender, eventargs e) 
166  
167   this.dgdfunctionarea[this.dgdfunctionarea.currentcell] = ((combobox)sender).selecteditem.tostring(); 
168
169  } 
170
171  /设置新的焦点 
172
173  private void buttonfocus_click(object sender, system.eventargs e) 
174  
175   /焦点模拟,这里设置第三行第一列 
176   getfocus(2,0); 
177  } 
178
179
180
181

   总结,这里是通过datagridtextboxcolumn.textbox.controls.add方法实现在列中添加combobox控件;对于数据的保存是使用combobox.selectionchangecommitted事件来完成;设置焦点是通过datagridtextboxcolumn.textbox.focus方法来实现。另外通过这个方法也可以添加datetimepicker等类似的控件。

c#中为datagrid添加下拉列表框相关推荐

  1. 给DataGrid添加确定删除的功能

    给DataGrid添加确定删除的功能 DataGrid的功能我想大家是知道的,我在实际的应用中遇到如下的问题,客户要求在删除之前做一次提示.类 似于windows.首先我们都知道DataGrid支持删 ...

  2. ASP.NET中利用DataGrid的自定义分页功能和存储过程结合实现高效分页

    关键字:DataGrid.存储过程.分页 出自: http://blog.csdn.net/yzx110/archive/2004/08/18/78525.aspx 摘要:在最进的一个项目中因为一个管 ...

  3. 将数据追加到html 表格中,将数据添加到数据表中

    将数据添加到数据表中 03/30/2017 本文内容 在创建 DataTable 并使用列和约束定义其结构之后,您可以将新的数据行添加到表中. 要添加新行,可将一个新变量声明为 DataRow 类型. ...

  4. html图片滚动红点_HTML中更换或添加网站背景图片的代码怎么写?(示例)

    本篇文章主要介绍了HTML代码中如何更换或添加网站背景图片?对于小白来说,最简单的方法就是,如果是更换背景图片的话. 我们可以在网页上点击鼠标右键查看网站源代码,然后找到css里面的背景图这一段代码, ...

  5. Springboot中给图片添加文字水印

    Springboot中给图片添加文字水印 工作中遇到给图片添加文字水印的需求,记录下来方便之后查阅 需求内容: 给一张图片添加指定文字水印,使一张图片上有多个水印内容,并且设定一个水印开关,可指定是否 ...

  6. python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别

    python list的extend (会将被插入的列表的每个元素从列表中拿出添加到列表中)与append方法(若被插入为列表,会将列表插入到源列表中)区别 Python--list的extend() ...

  7. vue中如何加入横线_在word文档中如何快速添加页眉横线和删除页眉横线?

    你还在烦恼怎么给word文档页眉添加横线和删除页眉横线吗?不用烦恼!今天小白就教您快速添加页眉横线和快速删除页眉横线的方法.赶快一起来学习一下!在word文档中如何快速添加页眉横线 1.打开word文 ...

  8. [收藏]为DataGrid添加CheckBox控件

    作者:孟宪会 出自:[孟宪会之精彩世界] 发布日期:2003年5月23日 8点26分11秒 为DataGrid添加CheckBox控件,并实现"全选"功能.这里是实现的例子 VB. ...

  9. 在Service中通过WindowManger添加View的方式来把UI界面显示出来

    整体方案 在Service中通过WindowManger添加View的方式来把UI界面显示出来 业务场景 具体场景 IQOO手机,游戏辅助 这种场景能否使用Activity方式来做 使用activit ...

最新文章

  1. 天道酬勤,付出总有回报。
  2. 学习flask的网址
  3. 官方数据:5次SDN大会的背后
  4. UA MATH574M 统计学习II 高维数据的二元分类
  5. 1031 Hello World for U (20 分)【难度: 一般 / 知识点: 找规律】
  6. ImportError: No module named _tkinter, please install the python-tk package ubuntu运行tkinter错误
  7. lesson7 集合set
  8. 计算机专业女生的就业方向参考
  9. MyBatis Demo 编写(1)基础功能搭建
  10. bind()的实现(持续更新中)
  11. 3. 机器学习中为什么需要梯度下降?梯度下降算法缺点?_一起学习西瓜书2
  12. python能做什么-学了Python都能做什么
  13. Go编程笔记(28)
  14. yolov3-tiny
  15. linux下可以输入中文曲,Ubuntu 14.04终端模式下中文输入听歌
  16. 全球顶级的5个数据可视化案例
  17. 标签、画像设计与模型落地
  18. html ul动态添加li,javaScript动态添加Li元素
  19. 谁吃土的时候没有受过委屈?
  20. arc64,x86等架构linux,windows系统openjdk下载

热门文章

  1. web前端黑客技术揭秘 10.关于防御
  2. 2016-08-29
  3. xml c libxml类库使用
  4. 第十三届计算机语言学大会,第十三届全国语音学学术会议(PCC 2018) 会议通知第3号...
  5. c语言输入身高计算标准体重_女人身高165cm标准体重是多少?
  6. 电镀面积计算机公式,电镀面积计算法.pdf
  7. 哲学家就餐 java_java模拟哲学家就餐问题
  8. java预编译啥意思_java预编译 java jdbc 预编译语句和普通语句的区别
  9. python sys.exit_Python程序退出方式(sys.exit() os._exit() os.kill() os.popen(...))
  10. python以读写方式打开文件_python读写文件操作详细介绍【传智播客】