Howdy
I have a DataGridView containing 2 ComboBox columns with a parent child relationship. One lists Clients, the other Client Projects.
Each ComboBox is bound to its own binding source,
bsClients
and
bsClientProjects
, with
bsClientProjects
being refreshed upon the
bsClients_CurrentChanged
event.
This works fine when collecting data for the first time, but breaks with a
DataGridViewComboBoxCell value is not valid
error when loading the data back into the DataGridView.
How can I have the the Client Project ComboBox refresh its list during the DataGridView binding process?
Cheers
Craig
P.S. The binding sources are populated using linq executed against typed datasets.
((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(
value
);
Thanks
Khalid
just go to the DataError event of ur datagridview control
and write the following code in it
if
(e.Exception.Message ==
"
DataGridViewComboBoxCell value is not valid."
)
object
value
= dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if
(!((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Contains(
value
))
((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(
value
);
e.ThrowException =
false
;
Set a null value to the cell:
var combobox = (DataGridViewComboBoxCell)dataGridView.CurrentRow.Cells[NAME];
combobox.DataSource = null;
combobox.Items.Clear();
combobox.ValueType = typeof(string)
dataGridView.CurrentRow.Cells[NAME].Value = null;
Just want to put this out there because I ran into this and here is what my problem was. Hope it helps someone
One of my data elements didn't match a potential options for the combobox
IE for the combobox in code you have the value options of
1, 2, 3, 4
but in your data there is a 5, so when it tries to set the combo box value to 5 it errors out because there is no 5 as an option.
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
<pre lang="cs">DataGridView dgv = (DataGridView)sender;
if (e.Exception is ArgumentException)
{
object value = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (!((DataGridViewComboBoxColumn)dgv.Columns[e.ColumnIndex]).Items.Contains(value))
{
((DataGridViewComboBoxCell)dgv[e.ColumnIndex, e.RowIndex]).Value = DBNull.Value;
//((DataGridViewComboBoxColumn)dgv.Columns[e.ColumnIndex]).Items.Add(value);
e.ThrowException = false;
}
}
else
{
core.ShowErrorMessage(e.Exception.Message);
e.Cancel = true;
}</pre>