Click here to Skip to main content
15,889,574 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 28-Dec-21 4:29am
Comments
[no name] 29-Aug-12 8:05am    
How did you refreshed the column.
Share your code.
kraeg75 29-Aug-12 22:46pm    
Hi Meysam

I can't really remember the context now. It's nearly two years ago and I have a terrible memory. Not even sure what app I was working on at the time.

Sorry
Craig

Nice answer
This helped me alot
C#
((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(value);


Thanks
Khalid
 
Share this answer
 
just go to the DataError event of ur datagridview control
and write the following code in it

C#
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;
               }
           }
 
Share this answer
 
Comments
kraeg75 29-Aug-12 22:58pm    
Thanks for your reply Sasanka. I'm not sure what I ended up doing in the end, but I must have resolved this or come up with a work around.
kamil shaikh 15-Dec-12 0:40am    
Thnq U verymuch Sasanka Ur Solution work for me
Joaquín Lopez B 25-May-15 17:56pm    
This work for me, when datagridviewcomboboxcell is linked to a bindingsource:

<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>
ChenXiaoXi 7-Jan-22 0:39am    
Thank you so much Sir sasanka sekhar panda, your solution really worked for me.
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;
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900