Hi,
The value will not save to the database unless you update the changes to the database. If you bind a datagridview control to a datatable which data is got from database, you have got the datagridview bind to the datatable instance. Any Changes of the datatable will not impact the database.
If currency ComboBoxColumn is databound and set both DisplayMember and ValueMember property, you can get the display text by DataGridViewCurrentCell.EditedFormattedValue property, valuemember by DataGridViewCurrentCell.Value property.
Set the cell value by changing the cell.value property. If this combobxcolumn is databound, you need to make sure the value is contained in the datasource, otherwise, there will throw an error.
If you need implement other action base on user selection on the comboboxcell, you need to handle ComboboxCell.SelectedValueChanged Event. Please refer to the following FAQ.
17. How do I handle the SelectedIndexChanged event?
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq17
I also suggest you to check the currentCell.ColumnIndex and remove the selectedIndexChanged event handler at cellEndEdit event. This will avoid cells in other datagridviewComboBoxColumn invoke the selectedIndexChanged event.
ComboBox cb;
void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
cb = e.Control as ComboBox;
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
if (cb != null)
{
// first remove event handler to keep from attaching multiple:
cb.SelectedIndexChanged -= new
EventHandler(cb_SelectedIndexChanged);
// now attach the event handler
cb.SelectedIndexChanged += new
EventHandler(cb_SelectedIndexChanged);
}
}
}
void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
cb.SelectedIndexChanged -= new
EventHandler(cb_SelectedIndexChanged);
}
More information:
DataGridViewCell.Value Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.value.aspx
DataGridViewCell.EditedFormattedValue Property
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.editedformattedvalue.aspx
27. How do I have a combo box column display a sub set of data based upon the value of a different combo box column?
http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/a44622c0-74e1-463b-97b9-27b87513747e#faq27
If anything unclear, please feel free to tell me.
Best regards,
Ling Wang
Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.