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

本文将介绍如何在 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 窗体设计器生成的代码 
 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等类似的控件。

posted @ 2006-06-22 09:06  wenanry  阅读(882)  评论(1编辑  收藏  举报