c#中为DataGrid添加下拉列表框
转载自:http://www.west.cn/www/info/51641-1.htm
本文将介绍如何在 System.Windows.Forms.DataGrid中切入使用ComboBox控件,主要包括三方面的内容。
1. 在DataGrid中加入ComboBox列;
2. 把在DataGrid中的修改保存到对应的网格;
3. 设置DataGrid中网格的焦点。
下面是整个源代码,一些功能可以看注释。
1 using System;
2 using System.Drawing;
3 using System.Collections;
4 using System.ComponentModel;
5 using System.Windows.Forms;
6 using System.Data;
7
8 namespace DataGridTest
9 {
10 public class Form1 : System.Windows.Forms.Form
11 {
12 private System.Windows.Forms.DataGrid dgdFunctionArea;
13 private DataTable dtblFunctionalArea;
14 private System.Windows.Forms.Button buttonFocus;
15 private System.ComponentModel.Container components = null;
16
17 public Form1()
18 {
19 InitializeComponent();
20 PopulateGrid();
21 }
22
23 protected override void Dispose(bool disposing)
24 {
25 if (disposing)
26 {
27 if (components != null)
28 {
29 components.Dispose();
30 }
31 }
32 base.Dispose(disposing);
33 }
34
35 #region Windows 窗体设计器生成的代码
36
37 private void InitializeComponent()
38 {
39 this.dgdFunctionArea = new System.Windows.Forms.DataGrid();
40 this.buttonFocus = new System.Windows.Forms.Button();
41 ((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).BeginInit();
42 this.SuspendLayout();
43 //
44 // dgdFunctionArea
45 //
46 this.dgdFunctionArea.DataMember = "";
47 this.dgdFunctionArea.HeaderForeColor = System.Drawing.SystemColors.ControlText;
48
49 this.dgdFunctionArea.Location = new System.Drawing.Point(4, 8);
50 this.dgdFunctionArea.Name = "dgdFunctionArea";
51 this.dgdFunctionArea.Size = new System.Drawing.Size(316, 168);
52 this.dgdFunctionArea.TabIndex = 0;
53 //
54 // buttonFocus
55 //
56 this.buttonFocus.Location = new System.Drawing.Point(232, 188);
57 this.buttonFocus.Name = "buttonFocus";
58 this.buttonFocus.Size = new System.Drawing.Size(84, 23);
59 this.buttonFocus.TabIndex = 1;
60 this.buttonFocus.Text = "获取焦点";
61 this.buttonFocus.Click += new System.EventHandler(this.buttonFocus_Click);
62 //
63 // Form1
64 //
65 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
66 this.ClientSize = new System.Drawing.Size(332, 217);
67 this.Controls.Add(this.buttonFocus);
68 this.Controls.Add(this.dgdFunctionArea);
69 this.Name = "Form1";
70 this.Text = "Form1";
71 ((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).EndInit();
72 this.ResumeLayout(false);
73
74 }
75 #endregion
76 /// <summary>
77 /// 应用程序的主入口点。
78 /// </summary>
79 [STAThread]
80 static void Main()
81 {
82 Application.Run(new Form1());
83 }
84 //初始化DataGrid
85 private void PopulateGrid()
86 {
87 //创建一个DataTable对象,包括四列,前三列为String,最后一列为Boolean。
88 dtblFunctionalArea = new DataTable("FunctionArea");
89 string[] arrstrFunctionalArea = new string[3] { "Functional Area", "Min", "Max" };
90 DataColumn dtCol = null;
91 //创建String列
92 for (int i = 0; i < 3; i++)
93 {
94 dtCol = new DataColumn(arrstrFunctionalArea[i]);
95 dtCol.DataType = Type.GetType("System.String");
96 dtCol.DefaultValue = "";
97 dtblFunctionalArea.Columns.Add(dtCol);
98 }
99
100 //创建Boolean列,用CheckedBox来显示。
101 DataColumn dtcCheck = new DataColumn("IsMandatory");
102 dtcCheck.DataType = System.Type.GetType("System.Boolean");
103 dtcCheck.DefaultValue = false;
104 dtblFunctionalArea.Columns.Add(dtcCheck);
105
106 //把表绑定到DataGrid
107 dgdFunctionArea.DataSource = dtblFunctionalArea;
108
109 //为DataGrid加载DataGridTableStyle样式
110 if (!dgdFunctionArea.TableStyles.Contains("FunctionArea"))
111 {
112 DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
113 dgdtblStyle.MappingName = dtblFunctionalArea.TableName;
114 dgdFunctionArea.TableStyles.Add(dgdtblStyle);
115 dgdtblStyle.RowHeadersVisible = false;
116 dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
117 dgdtblStyle.AllowSorting = false;
118 dgdtblStyle.HeaderBackColor = Color.FromArgb(8, 36, 107);
119 dgdtblStyle.RowHeadersVisible = false;
120 dgdtblStyle.HeaderForeColor = Color.White;
121 dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F,
122 System.Drawing.FontStyle.Bold,
123 System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
124 dgdtblStyle.GridLineColor = Color.DarkGray;
125 dgdtblStyle.PreferredRowHeight = 22;
126 dgdFunctionArea.BackgroundColor = Color.White;
127
128 //设置列的宽度
129 GridColumnStylesCollection colStyle = dgdFunctionArea.TableStyles[0].GridColumnStyles;
130 colStyle[0].Width = 100;
131 colStyle[1].Width = 50;
132 colStyle[2].Width = 50;
133 colStyle[3].Width = 80;
134 }
135
136 DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[0];
137
138 ComboBox cmbFunctionArea = new ComboBox();
139 cmbFunctionArea.Items.AddRange(new object[] { "选项一", "选项二", "选项三" });
140 cmbFunctionArea.Cursor = Cursors.Arrow;
141 cmbFunctionArea.DropDownStyle = ComboBoxStyle.DropDownList;
142 cmbFunctionArea.Dock = DockStyle.Fill;
143
144 //在选定项发生更改并且提交了该更改后发生
145
146 cmbFunctionArea.SelectionChangeCommitted += new EventHandler(cmbFunctionArea_SelectionChangeCommitted);
147
148 //把ComboBox添加到DataGridTableStyle的第一列
149
150 dgtb.TextBox.Controls.Add(cmbFunctionArea);
151
152 }
153
154 //设置焦点模拟
155
156 private void GetFocus(int row, int col)
157 {
158 //先把焦点移动到DataGrid
159 this.dgdFunctionArea.Focus();
160 //把焦点移动到DataGridCell
161 DataGridCell dgc = new DataGridCell(row, col);
162 this.dgdFunctionArea.CurrentCell = dgc;
163 DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[col];
164
165 //设置焦点
166
167 dgtb.TextBox.Focus();
168 }
169
170 //把Combobox上修改的数据提交到当前的网格
171
172 private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
173 {
174 this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();
175
176 }
177
178 //设置新的焦点
179
180 private void buttonFocus_Click(object sender, System.EventArgs e)
181 {
182 //焦点模拟,这里设置第三行第一列
183 GetFocus(2, 0);
184 }
185 }
186 }
总结,这里是通过DataGridTextBoxColumn.TextBox.Controls.Add方法实现在列中添加ComboBox控件;对于数据的保存是使用ComboBox.SelectionChangeCommitted事件来完成;设置焦点是通过DataGridTextBoxColumn.TextBox.Focus方法来实现。另外通过这个方法也可以添加DateTimePicker等类似的控件。