DataGrid Exception clears data in changed row.

  • Thread starter Thread starter Henrik Skak Pedersen
  • Start date Start date
H

Henrik Skak Pedersen

I have a problem in the Windows DataGridView control.

I the user either add a row or changes an existing row and the leaves the
row. If an exception occours because of for example an data constraint an
'DataGridView Default Error Dialog' occours which is fine. The problem is
that it resets the changes and leaves the row. I would like the user to stay
on the row with the error until the error is fixed or the user press
escape(default behaviour). Is that possible.

Thanks

Henrik,.
 
Hi Henrik,

Thanks for your post!

DataGridView provided the RowValidating and CellValidating events, which we
can set DataGridViewCellCancelEventArgs.Cancel property or
DataGridViewCellValidatingEventArgs.Cancel property to true to cancel the
leaving operation.

Hope this helps!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,

Thank you very much for your reply!

Sorry but my problem is a little bit different.

The problem is when I leave a row in the grid, the datatable validates the
new row, I would like that to happen. If there is a problem a form with the
caption "DataGrid Default Error Dialog" is being shown, I would also like
that to happen.

Then problem is when I press ok on the dialog all my changes are being
removed. I would instead like to return focus to the row being edited.

Thanks

Henrik.
 
Hi Henrik ,

Yes, I see your requirement.

However, based on the test on my side, after I pressed the OK button on the
error dialog, the focus goes to the original cell and the error input data
is not removed.

My test project uses the code snippet below to construct the datasource:
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));
for (int i = 0; i < 20; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr);
}
this.dataGridView1.DataSource = dt;
}

"column1" requires int type of data to input. If I input a non-decimal
string, it will generate an error dialog. If I have misunderstood you, can
you provide a little sample reproduce project?

I look forward to hearing from you. Thanks!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top