Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am writing simple application that uses DataGridView control with ComboBoxColumn.
I have used this method to change list of items inside one cell of ComboBoxColumn :

C#
void Fill_Location()
{
    DataGridViewComboBoxCell Location = (DataGridViewComboBoxCell) DGV.Rows[0].Cells[1];

    Location.Items.Clear();

    string a ="hello";

    Location.Items.Add(a);
}


When executed this method raises DataGridView Default Error Dialog with message:

The following exception occurred in the DataGridView
System.ArgumentException : DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.


So I have assigned to DGV this DataError event handler :

C#
void DGV_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

        MessageBox.Show(e.Context.ToString());
}

After that when executed method raises DataGridView DataError event handler with following message : Formatting, PreferredSize.

Since that I do not know how to resolve above error,
I have changed code for DataError event handler to :

C#
void DGV_DataError(object sender, DataGridViewDataErrorEventArgs e)
{

    if(e.Context== DataGridViewDataErrorContexts.Formatting)
        e.ThrowException = false;

}

Now when executing method Fill_Location() list of items inside ComboBoxCell is changed as expected, and there is no any error message.


Is this correct use of DataError event handler for resolving this kind of problem,
or there can be some other hidden problems that currently are not evident.

Program is written for .Net 4.0

All the best,
Željko Perić

What I have tried:

I have searched on the Internet for similar problems with DataGridViewComboBox and changing list of items inside only one cell. There are reports of same error messages but there is no adequate solution.
Posted
Updated 24-Jun-19 6:17am
v2

1 solution

Truly, i don't get idea why do you want to replace DataGridViewComboBoxCell items. Nevertheless...

Quote:
Is this correct use of DataError event handler for resolving this kind of problem,
or there can be some other hidden problems that currently are not evident.


Probably this is not the best solution, because you have disabled other errors...

I would resolve that by using one of the following methods:
method #1
C#
bool disableHandler = false;

void Fill_Location()
{
    disableHandler = true;
    DataGridViewComboBoxCell Location = (DataGridViewComboBoxCell) DGV.Rows[0].Cells[1];
    Location.Items.Clear();
    string a ="hello";
    Location.Items.Add(a);
    disableHandler = false;
}

void DGV_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
    if(disableHandler)
        e.ThrowException = false;
    else
        MessageBox.Show(e.Context.ToString());
}


method #2
C#
void Fill_Location()
{
    DGV.DataError -= new EventHandler(DGV_DataError);
    DataGridViewComboBoxCell Location = (DataGridViewComboBoxCell) DGV.Rows[0].Cells[1];
    Location.Items.Clear();
    string a ="hello";
    Location.Items.Add(a);
    DGV.DataError += new EventHandler(DGV_DataError);
}


For handling data errors, please see: DataGridView.DataError Event (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
Comments
Perić Željko 24-Jun-19 15:58pm    
Thanks for solution.
Maciej Los 24-Jun-19 16:01pm    
You're very welcome.
If it was helpful, please accept it (green button).

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