0
votes

I have a DataGridView which I need to run a CellValidating event to ensure that only valid values are selected from a ComboBox. This is needed as the ComboBox contains dummy rows used to display the category, with the fields the user can select listed underneath each category.

Whilst I have the validation code working fine, there is an unwelcome side-effect that all values are being wiped from the row being validated. I have stripped the code in the Event handler down to this, and the issue still occurs:

private void dgvInformation_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
    DataGridView dgv = this.dgvInformation;    
    DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex];
}

If I remove the

DataGridViewCell cell = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex]; 

line then the issue does not occur.

The DGV is unbound which I believe is causing the issue. As a test I have made a simple form and populated the DGV values unbound, and each time the CellValidating event fires that row is wiped out, but when I create a List<> and use that as the DataSource the values are not wiped out. Could this be a bug with unbound DGVs?

Many thanks

1

1 Answers

0
votes

I have problems with the sentense: "to ensure that only valid values are selected from a ComboBox." Using a combobox should actually prevent wrong values beeing tipped in controls like textbox, why dont you just show valid values in the combobox, or validate after all sellections have been made if you need to validate a combination of comboboxes sellection, therefor you may need somthing like a Submit button to run the validation routine.

If you still think you have to validate the combobox after each sellection then you ahve to run the validation somehow on a SelectionChanged event of the combobox.