Validation for RadGridView being bound at Runtime

1 Answer 79 Views
GridView
Leenah
Top achievements
Rank 1
Iron
Leenah asked on 31 Mar 2022, 08:32 AM | edited on 31 Mar 2022, 09:45 PM

I am new at Telerik.

I would like to validate data that are in my radgridview,. If data are saved in the database they must be highlighted orange. The radgriview in my form does not have any columns before I run the application. The columns are developed programmatically and the radgridview bound at runtime.

 

How can I validate the columns?

I have tried the following code but it does not work.

 

Can you tell me what I am doing wrong?

 

Thank you in advance

 void RadGridView_CellValidating(object sender, Telerik.WinControls.UI.CellValidatingEventArgs e)
        { 
             var column = e.Column as GridViewDataColumn;

            if (e.Row is GridViewDataRowInfo && column != null && column.Name == "Name")
            {
                var Value = (string)e.Value;
                var Row = (GridViewDataRowInfo)e.Row;
                if (string.IsNullOrEmpty((string)e.Value) || ((string)e.Value).Trim() == string.Empty)
                {
                    e.Cancel = true;
                    ((GridViewDataRowInfo)e.Row).ErrorText = "Validation error!";
                }
                else
                {
                    ((GridViewDataRowInfo)e.Row).ErrorText = string.Empty;
                }
            }
            }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 01 Apr 2022, 08:24 AM
Hello, Leenah,

RadGridView provides a convenient way to perform validation before data is committed to the underlying data source. You can validate data by handling CellValidating event which is raised by RadGridView when the current cell changes or when the cell loses input focus (when pressing Enter key). Canceling this event prevents the user from exiting the cell until a valid editor value is entered or the edit process is canceled. However, please have in mind that this event is expected to be fired not only when the editor is active, but also when RadGridView is not in unbound mode. Additional information is available in the following help article:
https://docs.telerik.com/devtools/winforms/controls/gridview/editors/data-validation 

I would highly encourage you to have a look at the following example in our Demo application: GridView >> Manipulate data >> Indicate Errors. To access the Live Demo simply click on the Windows Start button and type WinForms Demo. If you are not able to find the Live Demos using that approach you can also download it directly from here.

In case you are still experiencing any further difficulties, I would recommend you to submit a support ticket from your Telerik account and provide a sample runnable project demonstrating the problem you are facing. Thus, we would be able to make an adequate analysis of the precise case and provide further assistance.

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
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.

Tags
GridView
Asked by
Leenah
Top achievements
Rank 1
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or