پر کردن و به دست آوردن مقدار Combobox موجود در DataGridView
به کد زیر توجه کنید :
1: using System; 2: using System.Windows.Forms; 3: 4: namespace WindowsFormsApplication14 5: { 6: public partial class Form1 : Form 7: { 8: public Form1() 9: { 10: InitializeComponent(); 11: } 12: private void Form1_Load(object sender, EventArgs e) 13: { 14: this.dataGridView1.Rows.Add(); 15: 16: DataGridViewComboBoxCell datagridviewComboBoxCell1 = 17: (DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[0]; 18: 19: datagridviewComboBoxCell1.Items.Add("Value1"); 20: datagridviewComboBoxCell1.Items.Add("Value2"); 21: } 22: 23: private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) 24: { 25: if (this.dataGridView1.IsCurrentCellDirty) 26: { 27: this.dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); 28: } 29: } 30: 31: private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) 32: { 33: if (this.dataGridView1.Rows.Count > 0) 34: { 35: DataGridViewComboBoxCell datagridviewComboBoxCell1 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[0]; 36: if (datagridviewComboBoxCell1.Value != null) 37: { 38: MessageBox.Show(datagridviewComboBoxCell1.Value.ToString()); 39: } 40: } 41: } 42: } 43: }
خطوط 14 تا 20 مقادیری را به Combobox موجود در سطر اول Datagridview اضافه می کند.
خطوط 23 تا 41 تغییر آیتم Combobox موجود در Datagridview را بررسی می کند و با توجه به آیتم انتخابی پیغامی را نمایش می دهد .