Winform 如何在DataGrid中加入ComboBox控件
转载自:http://www.thinksaas.cn/group/topic/302254/
如何在DataGrid中加入ComboBox控件
C#类库中的DataGrid控件功能非常强大,基本上可以应对一般的表格处理,但是对于特殊的要求,比如在DataGrid中加入单选框(ChechBox)和多选下拉菜单(ComboBox)时就要自己写一些代码了,在Framework1.1中的DataGrid并没有提供这方面的功能。下面介绍一种在DataGrid中加入ComboBox的方法。
1.首先设置DataGrid的TableStyle
2.然后利用DataGridTextBoxColumn中的TextBox.Controls属性把ComBoBox加入
3.最后编写ComboBox的comboBox_SelectedIndexChanged事件,使修改可以保存回DataGrid的相应行。
如果对于TableStyle不太清楚,请参看我的前一篇文章
下面是程序的完整例子,在vs2003中测试通过,代码说明在程序中,关于TableStyle的说明请参看上一篇文章。添加ComboBox的代码主要在InitDataTable()函数中。
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 DataGridBackColor 9 { 10 ///<summary> 11 ///Form1的摘要描述。 12 ///</summary> 13 public class Form1 : System.Windows.Forms.Form 14 { 15 private System.Windows.Forms.DataGrid dataGrid; 16 private System.Windows.Forms.DataGridTableStyle TableStyle; 17 private System.Windows.Forms.DataGridBoolColumn Status; 18 private System.Windows.Forms.DataGridTextBoxColumn DataGridTextBox1; 19 private System.Windows.Forms.DataGridTextBoxColumn DataGridTextBox2; 20 private System.Windows.Forms.DataGridTextBoxColumn DataGridComboBox; 21 ///<summary> 22 ///设计工具所需的变数。 23 ///</summary> 24 private System.ComponentModel.Container components = null; 25 26 public Form1() 27 { 28 // 29 // Windows Form 设计工具支持的必要项 30 // 31 InitializeComponent(); 32 33 // 34 // TODO: 在InitializeComponent呼叫之后加入任何建构函式程序代码 35 // 36 } 37 38 ///<summary> 39 ///清除任何使用中的资源。 40 ///</summary> 41 protected override void Dispose(bool disposing) 42 { 43 if (disposing) 44 { 45 if (components != null) 46 { 47 components.Dispose(); 48 } 49 } 50 base.Dispose(disposing); 51 } 52 53 #region Windows Form 设计工具产生的程序代码 54 ///<summary> 55 ///此为设计工具支持所必须的方法-请勿使用程序代码编辑器修改 56 ///这个方法的内容。 57 ///</summary> 58 private void InitializeComponent() 59 { 60 System.Resources.ResourceManager resources = newSystem.Resources.ResourceManager(typeof(Form1)); 61 this.dataGrid = newSystem.Windows.Forms.DataGrid(); 62 this.TableStyle = newSystem.Windows.Forms.DataGridTableStyle(); 63 this.Status = newSystem.Windows.Forms.DataGridBoolColumn(); 64 this.DataGridTextBox1 = newSystem.Windows.Forms.DataGridTextBoxColumn(); 65 this.DataGridTextBox2 = newSystem.Windows.Forms.DataGridTextBoxColumn(); 66 this.DataGridComboBox = newSystem.Windows.Forms.DataGridTextBoxColumn(); 67 ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit(); 68 this.SuspendLayout(); 69 // 70 // dataGrid 71 // 72 this.dataGrid.CaptionText = "DataGrid Example"; 73 this.dataGrid.DataMember = ""; 74 this.dataGrid.Dock = System.Windows.Forms.DockStyle.Fill; 75 this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText; 76 this.dataGrid.Location = new System.Drawing.Point(0, 0); 77 this.dataGrid.Name = "dataGrid"; 78 this.dataGrid.Size = new System.Drawing.Size(408, 238); 79 this.dataGrid.TabIndex = 0; 80 this.dataGrid.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[] { 81 this.TableStyle}); 82 // 83 // TableStyle 84 // 85 this.TableStyle.AlternatingBackColor = System.Drawing.Color.Wheat; 86 this.TableStyle.BackColor = System.Drawing.Color.LightGray; 87 this.TableStyle.DataGrid = this.dataGrid; 88 this.TableStyle.ForeColor = System.Drawing.Color.MidnightBlue; 89 this.TableStyle.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[] { 90 this.Status, 91 this.DataGridTextBox1, 92 this.DataGridTextBox2, 93 this.DataGridComboBox}); 94 this.TableStyle.GridLineColor = System.Drawing.Color.Black; 95 this.TableStyle.HeaderBackColor = System.Drawing.Color.LightBlue; 96 this.TableStyle.HeaderForeColor = System.Drawing.SystemColors.ControlText; 97 this.TableStyle.MappingName = ""; 98 this.TableStyle.RowHeadersVisible = false; 99 this.TableStyle.SelectionBackColor = System.Drawing.Color.LightSkyBlue; 100 this.TableStyle.SelectionForeColor = System.Drawing.Color.WhiteSmoke; 101 // 102 // Status 103 // 104 this.Status.AllowNull = false; 105 this.Status.FalseValue = false; 106 this.Status.HeaderText = "Status"; 107 this.Status.MappingName = ""; 108 this.Status.NullText = ""; 109 this.Status.NullValue = ((object)(resources.GetObject("Status.NullValue"))); 110 this.Status.TrueValue = true; 111 this.Status.Width = 40; 112 // 113 // DataGridTextBox1 114 // 115 this.DataGridTextBox1.Format = ""; 116 this.DataGridTextBox1.FormatInfo = null; 117 this.DataGridTextBox1.HeaderText = "TextBox1"; 118 this.DataGridTextBox1.MappingName = ""; 119 this.DataGridTextBox1.NullText = ""; 120 this.DataGridTextBox1.ReadOnly = true; 121 this.DataGridTextBox1.Width = 75; 122 // 123 // DataGridTextBox2 124 // 125 this.DataGridTextBox2.Format = ""; 126 this.DataGridTextBox2.FormatInfo = null; 127 this.DataGridTextBox2.HeaderText = "TextBox2"; 128 this.DataGridTextBox2.MappingName = ""; 129 this.DataGridTextBox2.NullText = ""; 130 this.DataGridTextBox2.Width = 75; 131 // 132 // DataGridComboBox 133 // 134 this.DataGridComboBox.Format = ""; 135 this.DataGridComboBox.FormatInfo = null; 136 this.DataGridComboBox.HeaderText = "ComboBox"; 137 this.DataGridComboBox.MappingName = ""; 138 this.DataGridComboBox.NullText = ""; 139 this.DataGridComboBox.Width = 75; 140 // 141 // Form1 142 // 143 this.AutoScaleBaseSize = new System.Drawing.Size(5, 15); 144 this.ClientSize = new System.Drawing.Size(408, 238); 145 this.Controls.Add(this.dataGrid); 146 this.Name = "Form1"; 147 this.Text = "DataGrid Example"; 148 this.Load += newSystem.EventHandler(this.Form1_Load); 149 ((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit(); 150 this.ResumeLayout(false); 151 152 } 153 #endregion 154 155 ///<summary> 156 ///应用程序的主进入点。 157 ///</summary> 158 [STAThread] 159 static void Main() 160 { 161 Application.Run(newForm1()); 162 } 163 164 #region User Variable 165 private DataTable UserTable = null; 166 private DataView UserView = null; 167 #endregion 168 169 #region comboBox Event 170 private void Form1_Load(object sender, System.EventArgs e) 171 { 172 try 173 { 174 InitDataTable(); 175 InitDataView(); 176 InitDataGrid(); 177 InitData(); 178 } 179 catch (Exception ex) 180 { 181 MessageBox.Show(ex.Message); 182 } 183 } 184 185 private void comboBox_SelectedIndexChanged(object sender, System.EventArgs e) 186 { 187 //将修改写回dataGrid的相应行 188 if (dataGrid != null) 189 { 190 dataGrid[dataGrid.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString(); 191 } 192 } 193 #endregion 194 195 private void InitDataTable() 196 { 197 UserTable = newDataTable("UserTable"); 198 DataColumn[] d = newDataColumn[4]; 199 d[0] = newDataColumn("d0", typeof(bool)); 200 d[1] = newDataColumn("d1", typeof(string)); 201 d[2] = newDataColumn("d2", typeof(string)); 202 d[3] = newDataColumn("d3", typeof(string)); 203 UserTable.Columns.AddRange(d); 204 205 TableStyle.MappingName = "UserTable"; 206 Status.MappingName = "d0"; 207 DataGridTextBox1.MappingName = "d1"; 208 DataGridTextBox2.MappingName = "d2"; 209 DataGridComboBox.MappingName = "d3"; 210 211 //创建一个新的ComboBox控件并设置其基本属性 212 ComboBox comboBox = newComboBox(); 213 comboBox.Items.AddRange(new object[] { "Choose1", "Choose2", "Choose3" }); 214 comboBox.DropDownStyle = ComboBoxStyle.DropDownList; 215 comboBox.Dock = DockStyle.Fill; 216 DataGridComboBox.TextBox.Controls.Add(comboBox);//将comboBox加入DataGrid的相应行 217 218 //关联comboBox的SelectedIndexChanged事件 219 comboBox.SelectedIndexChanged += newEventHandler(comboBox_SelectedIndexChanged); 220 } 221 222 private void InitDataView() 223 { 224 UserView = newDataView(); 225 UserView.Table = UserTable; 226 UserView.AllowNew = false; 227 } 228 229 private void InitDataGrid() 230 { 231 dataGrid.DataSource = UserView; 232 } 233 234 private void InitData() 235 { 236 DataRow r = null; 237 for (int count = 1; count < 4; count++) 238 { 239 r = UserTable.NewRow(); 240 r[0] = true; 241 r[1] = "TextBox1" + count.ToString(); 242 r[2] = "TextBox2" + count.ToString(); 243 r[3] = "Choose" + count.ToString(); 244 UserTable.Rows.Add(r); 245 } 246 } 247 } 248 }
运行结果如下:
在有些时候,DataGrid中不同行的值要以不同的颜色显示出来,比如告警,不同告警的背景色要不同,在下一篇文章中,我将介绍一下如何修改DataGrid的背景色问题。