rowchanging : validating user input with exception

G

Guest

I have a question about validating user input, which the user can enter in a DataGrid. I use the rowchanging event of the DataTable to which the DataGrid is bound. In this rowchanging event-handler, I check the column data of the row, and if not correct, my own exception is thrown
I’m having problems handling this exception, when not handled in my code, the following error message occurs

Error when committing the row to the original data store

<My own text, explaining the error message, in Dutch

Do you want to correct the value

Yes / No button

After pressing Yes or No (does not matter which of the two), al the edits of the (validated) row have been rolled back (like a CancelEdit) ! (Stefano Toniolo posted a question describing this same problem in microsoft.public.dotnet.framework)
So, my question is twofold
First, why and where are the edits rolled back, and how can I make sure this does not occur
Second, I want to handle the exception in my own code, where and how can I, and where in the .NET code is the English text added to my error text ? Does the datagrid do this

Thanks in advanc

M. Lein
 
K

Kevin Yu [MSFT]

Hi M. Leine,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you have two questions. The first one
is where does the roll back in the DataGrid occur, the second is how to
handle the exception of the DataGrid with your own code. If there is any
misunderstanding, please feel free to let me know.

As far as I know, this bahavior of DataGrid is by design. The DataGrid will
catch all the exceptions thrown from its data source. When an exception is
thrown, it catches the exception, and displays a dialog box which contains
the error message and "Do you want to correct the value?". And since it's
by design, I don't think we can disable it in our code.

In this case, my suggestion is to use the DataRow.RowError property. In the
RowChanging event handler, you can set e.Row.RowError. The DataGrid will
display a small excalmatory mark on the first column of the row. When you
move the mouse point to that mark, a yellow tool-tip-tag will be displayed
which contains the error text you have set to the RowError property.

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

Michael Hampel

I think most of this behaviour you are experiencing is how the
datagrid is supposed to behave. Have you implemented any code on the
dataadaptor or datatable that could be affecting the update?

I would choose to validate the input using one of the datatable
events, ColumnChanged,ColumneChanging, RowChanged,RowChanging, rather
than the datagrid events. This means that if the table was to be bound
to other type of controls the validation would still apply.

You could check the value of e.proposed and handle the action as you
wished, ie row.rejectchanges().
 
G

Guest

Hi Kevin Yu,

Yes, you have interpretered my questions correctly.
Your mentioned solution does not really solve my problem. Setting the rowerror property of the datarow, as you suggested has no impact on the behavior of the datagrid : the rowchanged event gets fired and the changes (incorrect user values) get stored (in the dataset). Second, if a user started this proces by choosing another row, that row gets selected. I would have expected that the row with the error would remain the active, current row.
I solved the problem (workaround) by letting the system store the row (in the dataset) with the errors, and in the rowchanged event show an error message. After that, the system moves back to the previous row if it has errors.
Please let me know if the mentioned behavior of the winforms DataGrid will change in the future.
I suspect at the moment it does something like this:

try
{
datagrid.EndEdit();
}
except (Exception e)
{
datagrid.CancelEdit();
MessageBox.Show(e.Message + "/nDo you want to correct the value ?",
"Error when committing the row to the original data store",
MessageBoxButtons.YesNo, MessageBoxIcon.Error);
}

Why not

try
{
datagrid.EndEdit();
}
except (Exception e)
{
//call an event, which let the user / developer handle it, in this eventhandler the user can show an error message:
RowErrorEvent(e);
}

----- Kevin Yu [MSFT] wrote: -----

Hi M. Leine,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you have two questions. The first one
is where does the roll back in the DataGrid occur, the second is how to
handle the exception of the DataGrid with your own code. If there is any
misunderstanding, please feel free to let me know.

As far as I know, this bahavior of DataGrid is by design. The DataGrid will
catch all the exceptions thrown from its data source. When an exception is
thrown, it catches the exception, and displays a dialog box which contains
the error message and "Do you want to correct the value?". And since it's
by design, I don't think we can disable it in our code.

In this case, my suggestion is to use the DataRow.RowError property. In the
RowChanging event handler, you can set e.Row.RowError. The DataGrid will
display a small excalmatory mark on the first column of the row. When you
move the mouse point to that mark, a yellow tool-tip-tag will be displayed
which contains the error text you have set to the RowError property.

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
K

Kevin Yu [MSFT]

Hi M. Leine,

It was nice to know that you have got a solution to this problem.

Before the new version of DataGrid releases, we cannot promise anything,
such as the changing of the mentioned behavior. I will let know that it
changes in the future.

Also, if you have suggestions that could make our product better, please
send mails directly to (e-mail address removed), your ideas will be highly
appreciated. Thank you very much for your suggestions and feedback!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

mgwalm

M. Leine

What you say sounds correct, but am not sure on your exact code.
The following is a sample of my code and it works. I just get an error
message box containing my message and the column is not updated. (C#)

private void ColumnChanging(object sender, DataColumnChangeEventArgs
e)
{
string val = "",
msg = "";

switch (e.Column.ColumnName.ToUpper())
{
case "ANZIC":
if (e.ProposedValue != null) val = e.ProposedValue.ToString();
if (val.Length == 0)
{
msg = "The code is required!";
}
break;

case "DESCRIPTION":
if (e.ProposedValue != null) val = e.ProposedValue.ToString();
if (val.Length == 0)
{
msg = "The description is required!";
}
break;

}
if (msg.Length > 0)
{
e.Row.SetColumnError(e.Column, msg);
throw new ValidatonException(msg);
}
else
{
e.Row.ClearErrors();
}
}

Regards
Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top