DataGridView ComboBox: Allowing user to type in a value NOT in the Items collection.
-
This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.
-
This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.
I found a little more info on this problem searching MS. Here's a comment:
The DataGridViewComboBox must hold all possible values that it will display
in it's items collection. For example, when binding to the Customers table
and having the CompanyName field a combobox the combobox must contain all
possible values. If the grid attempts to set the cell's value where the
combobox cell doesn't have the value then we throw the dataError event.To workaround this, handle the DataError event of the DataGridView. For
example:void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs
e)
{
// ignore the data error occurring under the territoryComboBoxColumn
if (e.ColumnIndex != territoryComboBoxColumn.Index)
{
MessageBox.Show("Data error occurs:" + e.Exception.Message);
}
}This tells us how to avoid the Exception. However, it doesn't address allowing the user's typed input to become the value for that cell. Any thoughts?
-
I found a little more info on this problem searching MS. Here's a comment:
The DataGridViewComboBox must hold all possible values that it will display
in it's items collection. For example, when binding to the Customers table
and having the CompanyName field a combobox the combobox must contain all
possible values. If the grid attempts to set the cell's value where the
combobox cell doesn't have the value then we throw the dataError event.To workaround this, handle the DataError event of the DataGridView. For
example:void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs
e)
{
// ignore the data error occurring under the territoryComboBoxColumn
if (e.ColumnIndex != territoryComboBoxColumn.Index)
{
MessageBox.Show("Data error occurs:" + e.Exception.Message);
}
}This tells us how to avoid the Exception. However, it doesn't address allowing the user's typed input to become the value for that cell. Any thoughts?
Did you try this?
if (e.ColumnIndex != territoryComboBoxColumn.Index)
{
MessageBox.Show("Data error occurs:" + e.Exception.Message);
//Add user's typed input into ComboBox here
}When you're alone in the Dark, Fear will protect you...
-
This question has been asked, in one way or another, a few times and I've yet to see a solid answer. I've searched the articles and messages but don't see a solution. Here's my desire: A normal ComboBox, when the DropDownStyle is set to ComboBox rather than ComboBoxList, will allow the user to select an item from the Items list OR type in their own value. Near as I can tell, the DataGridView ComboBox performs validation to see if the Value matches an item in the list. If that Value is not found in the list, you get the error message: "System.ArgumentException: DataGridViewComboBoxCell value is not valid" Basically making the DataGridViewComboBoxCell function like a ComboBoxList -- in essence NOT allowing the user to type in a value that isn't in the list. Is there a way around this? Is there a way for a DataGridViewComboBoxCell to allow the user to EITHER select a Value from the list OR type in a new value that is NOT already in the list? I am definitely NOT the only one out here with this question / problem, so if you have any ideas on a way around it, PLEASE let us all know. Thanks a bunch.
The best (and pretty much only) way I have ever found to address this kind of problem is to hook into the EditingControlShowing event of the DataGridView. When you do, you can detect that it is a ComboBoxCell being shown, and that the control is a DataGridViewComboBoxEditingControl (or something to that effect). This control is a very simple child class of the normal ComboBox control with a few extra properties for databinding in a data grid. In the EditingControlShowing event, you can hook into the ComboBox as you normally would, and detect when its data changes and when it is closing. When it is closing, you can edit the data being bound to the ComboBox's available items list. One caveat, I've never tried this. It may be impossible - I don't know exactly what order the DataGridView would carry out this operation (control closes->you add new item to bound data list->DataGridView validates the input against this new list). It may be that the data is validated before it will change the list of data, in which case you're up a creek unless you can find some way to add new items as the user is typing. At any rate, if this is possible at all, you'll probably find some way to do it using this event. There may be another way, and I'm all ears if there is - these are the kinds of problems I love solving with the DGV :)