ComboBox in Datagrid view
-
Hi all, Basically i have 2 question in this message.I am using
DataGridViewComboBoxColumn
to insert a ComboBox into my datagridview.Code as below:dataGridView1.Columns.Remove("Seqence\_Number"); <----remove the original column first before add the combo box DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn(); dgvComboBox.DataPropertyName = "Seqence\_Number"; dgvComboBox.HeaderText = "Sequence (Stage)"; dgvComboBox.DropDownWidth = 100; dgvComboBox.Width = 100; dgvComboBox.MaxDropDownItems = 10; dgvComboBox.FlatStyle = FlatStyle.System; dsTemp = singleton.ProdetailsDataset.Copy(); <-----dataset will copy from my singleton class dgvComboBox.DataSource = dsTemp.Tables\[0\]; dgvComboBox.ValueMember = "Stage\_Name"; dgvComboBox.DisplayMember = "Stage\_Name"; dataGridView1.Columns.Insert(1,dgvComboBox);
The first question is, how can i display more than one value member in ComboBox? The second qeustion is, how can i do a selected event for that ComboBox in datagridview? what i wanna do is when user select the value in ComboBox, some event will fire to validate the value which selected by user. Any tips are welcome, Thanks in advance
-
Hi all, Basically i have 2 question in this message.I am using
DataGridViewComboBoxColumn
to insert a ComboBox into my datagridview.Code as below:dataGridView1.Columns.Remove("Seqence\_Number"); <----remove the original column first before add the combo box DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn(); dgvComboBox.DataPropertyName = "Seqence\_Number"; dgvComboBox.HeaderText = "Sequence (Stage)"; dgvComboBox.DropDownWidth = 100; dgvComboBox.Width = 100; dgvComboBox.MaxDropDownItems = 10; dgvComboBox.FlatStyle = FlatStyle.System; dsTemp = singleton.ProdetailsDataset.Copy(); <-----dataset will copy from my singleton class dgvComboBox.DataSource = dsTemp.Tables\[0\]; dgvComboBox.ValueMember = "Stage\_Name"; dgvComboBox.DisplayMember = "Stage\_Name"; dataGridView1.Columns.Insert(1,dgvComboBox);
The first question is, how can i display more than one value member in ComboBox? The second qeustion is, how can i do a selected event for that ComboBox in datagridview? what i wanna do is when user select the value in ComboBox, some event will fire to validate the value which selected by user. Any tips are welcome, Thanks in advance
Hi, 1) Concatnate the two column values (in a query) and assign as a value member. 2) You can use "EditingControlShowing" event to access the "SelectedIndexChanged" event for combobox cell. Here is the Code Snipet: private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing); } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox ctl = e.Control as ComboBox; if(ctl!=null) ctl.SelectedIndexChanged+=new EventHandler(ctl_SelectedIndexChanged); } private void ctl_SelectedIndexChanged(object sender, EventArgs args) { MessageBox.Show("Selected Index Changed Event"); } Thanks,
Gopal.S
-
Hi, 1) Concatnate the two column values (in a query) and assign as a value member. 2) You can use "EditingControlShowing" event to access the "SelectedIndexChanged" event for combobox cell. Here is the Code Snipet: private void Form1_Load(object sender, EventArgs e) { this.dataGridView1.EditingControlShowing += new System.Windows.Forms.DataGridViewEditingControlShowingEventHandler(this.dataGridView1_EditingControlShowing); } private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) { ComboBox ctl = e.Control as ComboBox; if(ctl!=null) ctl.SelectedIndexChanged+=new EventHandler(ctl_SelectedIndexChanged); } private void ctl_SelectedIndexChanged(object sender, EventArgs args) { MessageBox.Show("Selected Index Changed Event"); } Thanks,
Gopal.S
Hi Gopal.S Thanks for your suggestion. I am just figure out how to do it, but seem you have a better suggestion :) 1) I just simple Concatnate the columns after i get the dataset, something like below:
string s = dsTemp.Tables[0].Rows[1][2].ToString() + dsTemp.Tables[0].Rows[1][3].ToString();
2) I am usingCellEndEdit
,as below;this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit); private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { string cellvalue; if (e.ColumnIndex == 1) { cellvalue = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString(); if (cellvalue != null && cellvalue != "") { //some coding here } }
I will try you suggestion too, Thanks :laugh: