GridView - DataError event and parsing error

1 Answer 20 Views
GridView
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Marian asked on 04 Jul 2024, 06:05 PM

Hello,

I would like to ask about DataError event once more (we discussed it a little in some previous thread). I have tested behavior and program crashes in case of parsing error. First, if I do some validation in underlying data object and throw exception in object property setter, GridView handles it corectly and fires DataError event. But in case of parsing error, for example when I have textbox column, integer property and enter some non-numeric text, program crashes with unhandled exception:

I know, I should use GridViewDecimalColumn, but generally, why GridView doesn't handle parsing error and doesn't fire DataError event also in this case? I have attached also the test project, TelerikTestDataError in solution.

Unhandled exception:
System.FormatException: Input string was not in a correct format.
   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   at System.ComponentModel.BaseNumberConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)
   at Telerik.WinControls.UI.RadDataConverter.ParseCore(Object value, Type targetType, Type sourceType, TypeConverter dataTypeConverter, IDataConversionInfoProvider dataColumn, Boolean checkFormattedNullValue, CultureInfo cultureInfo)
   at Telerik.WinControls.UI.RadDataConverter.Parse(IDataConversionInfoProvider converstionInfoProvider, Object value, CultureInfo cultureInfo)
   at Telerik.WinControls.UI.GridDataCellElement.set_Value(Object value)
   at Telerik.WinControls.UI.GridViewEditManager.EndEditCore(Boolean validate, Boolean cancel)
   at Telerik.WinControls.UI.GridViewEditManager.EndEdit()
   at Telerik.WinControls.UI.GridRowBehavior.ProcessEnterKey(KeyEventArgs keys)
   at Telerik.WinControls.UI.GridRowBehavior.ProcessKey(KeyEventArgs keys)
   at Telerik.WinControls.UI.BaseGridBehavior.ProcessKey(KeyEventArgs keys)
   at Telerik.WinControls.UI.BaseGridBehavior.ProcessKeyDown(KeyEventArgs keys)
   at Telerik.WinControls.UI.BaseGridEditor.OnKeyDown(KeyEventArgs keyEventArgs)
   at Telerik.WinControls.UI.RadTextBoxEditor.OnKeyDown(KeyEventArgs e)
   at Telerik.WinControls.UI.RadTextBoxEditor.TextBoxItem_KeyDown(Object sender, KeyEventArgs e)
   at System.Windows.Forms.KeyEventHandler.Invoke(Object sender, KeyEventArgs e)
   at Telerik.WinControls.RadItem.OnKeyDown(KeyEventArgs e)
   at Telerik.WinControls.UI.RadTextBoxItem.TextBoxControl_KeyDown(Object sender, KeyEventArgs e)
   at System.Windows.Forms.Control.OnKeyDown(KeyEventArgs e)
   at System.Windows.Forms.Control.ProcessKeyEventArgs(Message& m)
   at System.Windows.Forms.Control.ProcessKeyMessage(Message& m)
   at System.Windows.Forms.Control.WmKeyChar(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.TextBoxBase.WndProc(Message& m)
   at System.Windows.Forms.TextBox.WndProc(Message& m)
   at Telerik.WinControls.UI.HostedTextBoxBase.WndProc(Message& message)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

1 Answer, 1 is accepted

Sort by
0
Nadya | Tech Support Engineer
Telerik team
answered on 09 Jul 2024, 08:06 AM

Hello, Marian,

Thank you for the provided project.

When you have a GridViewTextBoxColumn where the DataType property is specified with Integer type it is normal behavior to obtain such an error indicating that "Input string was not in a correct format." This means that the editor value cannot be parsed to a valid number. This can be also seen in the provided stack trace. GridViewDecimalColumn works with numeric values and handles internally all the validation to stop users from entering non-numeric values. On the other hand, GridViewTextBoxColumn can accept any text values also numbers represented as strings.

However, if you decide to use GridViewTextBoxColumn with an integer type property, it is up to you to validate the user input upon typing into the cell. In this case you can handle the CellValidating event which can be canceled if the value is not valid. More information is available here: Data validation - WinForms GridView Control - Telerik UI for WinForms

I hope this information makes it clearer. If you have any other questions, please let me know. 

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 09 Jul 2024, 09:37 AM

Hello,
yes, I know, but I would expect error "Input string was not in a correct format" in DataError event as in the standard DataGridView, not that the program crashes with unhandled exception. Yes, I can use CellValidating, but I think GridView should handle parsing errors, why not? And yes, of course, I have used GridViewDecimalColumn in real application, there's no reason why to insist on using GridViewTextBoxColumn, this was the first test when I didn't know about this behavior, now I tested it once more and ask a question, because it's strange in my opinion. Now I can't imagine other situation, where there can be problem with parsing, but it seems to be unnecessary for me handle CellValidating for testing parsing error, if GridView could handle parsing error with DataError event as in DataGridView. But maybe there's some reason, why it works like this.
Nadya | Tech Support Engineer
Telerik team
commented on 11 Jul 2024, 09:10 AM

Hello, Marian,

I understand your insights about this error and also, I confirm that the MS DataGrid behaves different in this particular case. But I would like to explain that it is in the core implementation of RadGridView that according to the provided property type a respective column gets created. As you already noticed the GridViewDecimalColumn works with numbers and this error is handled internally. However, by providing our clients the opportunity to specify AutoGenerateColumns=false and thus manually setup your columns according to your requirements, it is up to the developer to handle such cases. In addition, we provide the CellValidating event that triggers for every cell which value is about to change. This is a very powerful mechanism since you have control over the whole validation process. 

I have also discussed this case with the team, and they are on the same opinion. The CellValidating event is intended for handling such scenarios, as it is part of our grid control's core design. Any changes in its core functionality would be a breaking change that will impact other clients which we want to avoid. Moreover, we do not consider this as a bug since we have introduced a mechanism to handle such cases.

The "Input string was not in a correct format" error in this case occurs because the format is not correct. You should use the CellValidating event and for example cancel the event, if the format conversion of value is not correct, or you can also display a message, or something else. Thus, the cell wouldn't end edit mode until a valid number is entered. As far, as I can test the same scenario with MS DataGridView it behaves in the same way.

Here is the code snippet which you can use to validate the entered value in cell for the Num1 column:

private void RadGridView1_CellValidating(object sender, CellValidatingEventArgs e)
{
    if (e.ColumnIndex == 0 && int.TryParse(e.Value.ToString(), out int r))
    {
        e.Cancel = false;
    }
    else
    {
        e.Cancel = true;
    }

}

Thus, if you enter let's say "ff" in the cell, you wouldn't be allowed to exit edit mode, until you enter any number, let's say "200". Then, the DataError will triggers to execute the validation you had in the property field. In this way RadGridView control behaves exactly as MS DataGridView. 

I believe this information useful. If you have any further concerns or encounter difficulties regarding this case, please share more details with us. We should do our best to assist you moving forward. 

Marian
Top achievements
Rank 2
Iron
Iron
Iron
commented on 11 Jul 2024, 09:19 AM

Ok, thanks for opinion. You didn't have to describe it so widely, I understand it's your design decision and of course I don't consider it as bug in this case. Also I know how to write CellValidating and other circumstances you have mentioned, but thanks for the effort.
Tags
GridView
Asked by
Marian
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or