在DataGrid中简单使用下拉列表框
2024-07-21 02:23:38
供稿:网友
 
在datagrid中简单使用下拉列表框
作者:tushar ameta
翻译:秋枫
在datagrid中使用下拉列表问题。这篇文章讲了如何在 system.windows.forms.datagrid中切入使用combobox控件。不过原文不全,无法调试,在这里为了说清楚点,对原文作了一些修改,整篇文章主要包括三方面的内容。
1. 在datagrid中加入combobox列;
2. 把在datagrid中的修改保存到对应的网格;
3. 设置datagrid中网格的焦点。
 
下面是整个源代码,一些功能可以看注释。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
 
namespace datagridtest
{
 public class form1 : system.windows.forms.form
 {
 private system.windows.forms.datagrid dgdfunctionarea;
 private datatable dtblfunctionalarea;
 private system.windows.forms.button buttonfocus;
 private system.componentmodel.container components = null;
 
 public form1()
 {
 initializecomponent();
 populategrid();
 }
 
 protected override void dispose( bool disposing )
 {
 if( disposing )
 {
 if (components != null) 
 {
 components.dispose();
 }
 }
 base.dispose( disposing );
 }
 
 #region windows 窗体设计器生成的代码
 private void initializecomponent()
 {
 this.dgdfunctionarea = new system.windows.forms.datagrid();
 this.buttonfocus = new system.windows.forms.button();
 ((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).begininit();
 this.suspendlayout();
 // 
 // dgdfunctionarea
 // 
 this.dgdfunctionarea.datamember = "";
 this.dgdfunctionarea.headerforecolor = system.drawing.systemcolors.controltext;
 this.dgdfunctionarea.location = new system.drawing.point(4, 8);
 this.dgdfunctionarea.name = "dgdfunctionarea";
 this.dgdfunctionarea.size = new system.drawing.size(316, 168);
 this.dgdfunctionarea.tabindex = 0;
 // 
 // buttonfocus
 // 
 this.buttonfocus.location = new system.drawing.point(232, 188);
 this.buttonfocus.name = "buttonfocus";
 this.buttonfocus.size = new system.drawing.size(84, 23);
 this.buttonfocus.tabindex = 1;
 this.buttonfocus.text = "获取焦点";
 this.buttonfocus.click += new system.eventhandler(this.buttonfocus_click);
 // 
 // form1
 // 
 this.autoscalebasesize = new system.drawing.size(6, 14);
 this.clientsize = new system.drawing.size(332, 217);
 this.controls.add(this.buttonfocus);
 this.controls.add(this.dgdfunctionarea);
 this.name = "form1";
 this.text = "form1";
 ((system.componentmodel.isupportinitialize)(this.dgdfunctionarea)).endinit();
 this.resumelayout(false);
 
 }
 #endregion
 
 /// <summary>
 /// 应用程序的主入口点。
 /// </summary>
 [stathread]
 static void main() 
 {
 application.run(new form1());
 }
 //初始化datagrid
 private void populategrid()
 {
 //创建一个datatable对象,包括四列,前三列为string,最后一列为boolean。
 dtblfunctionalarea = new datatable ("functionarea");
 string[] arrstrfunctionalarea = new string [3]{"functional area","min","max"};
 datacolumn dtcol = null;
 //创建string列 
 for(int i=0; i< 3;i++)
 { 
 dtcol = new datacolumn(arrstrfunctionalarea[i]);
 dtcol.datatype = type.gettype("system.string");
 dtcol.defaultvalue = "";
 dtblfunctionalarea.columns.add(dtcol); 
 } 
 
 //创建boolean列,用checkedbox来显示。 
 datacolumn dtccheck = new datacolumn("ismandatory");
 dtccheck.datatype = system.type.gettype("system.boolean");
 dtccheck.defaultvalue = false;
 dtblfunctionalarea.columns.add(dtccheck);
 
 //把表绑定到datagrid
 dgdfunctionarea.datasource = dtblfunctionalarea; 
 
 //为datagrid加载datagridtablestyle样式
 if(!dgdfunctionarea.tablestyles.contains("functionarea"))
 {
 datagridtablestyle dgdtblstyle = new datagridtablestyle();
 dgdtblstyle.mappingname = dtblfunctionalarea.tablename;
 dgdfunctionarea.tablestyles.add(dgdtblstyle);
 dgdtblstyle.rowheadersvisible = false;
 dgdtblstyle.headerbackcolor = color.lightsteelblue;
 dgdtblstyle.allowsorting = false;
 dgdtblstyle.headerbackcolor = color.fromargb(8,36,107);
 dgdtblstyle.rowheadersvisible = false;
 dgdtblstyle.headerforecolor = color.white;
 dgdtblstyle.headerfont = new system.drawing.font("microsoft sans serif", 9f, 
 system.drawing.fontstyle.bold, 
 system.drawing.graphicsunit.point, ((system.byte)(0)));
 dgdtblstyle.gridlinecolor = color.darkgray;
 dgdtblstyle.preferredrowheight = 22;
 dgdfunctionarea.backgroundcolor = color.white; 
 
 //设置列的宽度 
 gridcolumnstylescollection colstyle = dgdfunctionarea.tablestyles[0].gridcolumnstyles;
 colstyle[0].width = 100;
 colstyle[1].width = 50;
 colstyle[2].width = 50;
 colstyle[3].width = 80;
 }
 
 datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[0]; 
 combobox cmbfunctionarea = new combobox();
 cmbfunctionarea.items.addrange(new object[]{"选项一","选项二","选项三"});
 cmbfunctionarea.cursor = cursors.arrow;
 cmbfunctionarea.dropdownstyle= comboboxstyle.dropdownlist;
 cmbfunctionarea.dock = dockstyle.fill;
 //在选定项发生更改并且提交了该更改后发生
 cmbfunctionarea.selectionchangecommitted += new eventhandler(cmbfunctionarea_selectionchangecommitted); 
 //把combobox添加到datagridtablestyle的第一列
 dgtb.textbox.controls.add(cmbfunctionarea); 
 
 }
 //设置焦点模拟
 private void getfocus(int row,int col)
 {
 //先把焦点移动到datagrid
 this.dgdfunctionarea.focus(); 
 //把焦点移动到datagridcell
 datagridcell dgc = new datagridcell(row,col); 
 this.dgdfunctionarea.currentcell = dgc; 
 datagridtextboxcolumn dgtb = (datagridtextboxcolumn)dgdfunctionarea.tablestyles[0].gridcolumnstyles[col]; 
 //设置焦点
 dgtb.textbox.focus();
 
 } 
 //把combobox上修改的数据提交到当前的网格
 private void cmbfunctionarea_selectionchangecommitted(object sender, eventargs e)
 {
 this.dgdfunctionarea[this.dgdfunctionarea.currentcell] = ((combobox)sender).selecteditem.tostring();
 } 
 //设置新的焦点
 private void buttonfocus_click(object sender, system.eventargs e)
 {
 //焦点模拟,这里设置第三行第一列
 getfocus(2,0);
 }
 }
}
下面是测试界面:
 
 总结,这里是通过datagridtextboxcolumn.textbox.controls.add方法实现在列中添加combobox控件;对于数据的保存是使用combobox.selectionchangecommitted事件来完成;设置焦点是通过datagridtextboxcolumn.textbox.focus方法来实现。另外通过这个方法也可以添加datetimepicker等类似的控件。