c#中为datagrid添加下拉列表框

本文介绍在system.windows.forms.datagrid中嵌入combobox控件的方法,包括添加combobox列、保存修改数据及设置焦点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

   1. 在datagrid中加入combobox列;

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

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



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

 

 

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


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值