This is a migrated thread and some comments may be shown as answers.

Cell Validating Event

1 Answer 446 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Claude
Top achievements
Rank 1
Iron
Veteran
Claude asked on 28 Nov 2020, 09:13 PM

I have a gridview with 3 columns and I want to make sure that all cells in the row have a value in them.  If col0 is null and the user goes back and enterd data, the event does not detect the changes and just gets stuck in the col0 cell.   As you can see, I tried a couple of different ways.  Below is my code:

 

private void dgvCatagories_CellValidating(object sender, CellValidatingEventArgs e)
        {
            try
            {
                GridViewDataColumn column = e.Column as GridViewDataColumn;
                // if (string.IsNullOrEmpty((string)e.Row.Cells["Col0"].Value) || ((string)e.Row.Cells["Col0"].Value).Trim() == string.Empty ||
                //        string.IsNullOrEmpty((string)e.Row.Cells["Col1"].Value) || ((string)e.Row.Cells["Col1"].Value).Trim() == string.Empty ||
                //        string.IsNullOrEmpty((string)e.Row.Cells["Col2"].Value) || ((string)e.Row.Cells["Col2"].Value).Trim() == string.Empty)
                if (e.Row is GridViewDataRowInfo && column != null && column.Name == "Col0" ||
                e.Row is GridViewDataRowInfo && column != null && column.Name == "Col1" ||
                e.Row is GridViewDataRowInfo && column != null && column.Name == "Col2")             
                {

                    e.Cancel = true;
                    ((GridViewDataRowInfo)e.Row).ErrorText = "Cell Can Not Be Blank";

                }
                else
                {
                    ((GridViewDataRowInfo)e.Row).ErrorText = string.Empty;
                }

            }
            catch (Exception ex)
            {
                LogFile(ex.Message, ex.ToString(), "dgvCatagories_CellValidating", this.FindForm().Name);
            }
        }

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 30 Nov 2020, 10:38 AM
Hello, Claude,    

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. 

Please have in mind that when you have an active editor in the grid, its value is not committed to the cell yet. Hence, the extracted value from the GridViewDataRowInfo is different than the one in the editor. In this case, feel free to use the CellValidatingEventArgs.Value property. Thus, you won't be stuck if the entered value is valid. A sample approach is demonstrated here: https://docs.telerik.com/devtools/winforms/controls/gridview/editors/data-validation 

In case you are still experiencing any further difficulties, it would be greatly appreciated if you can provide a sample project demonstrating the undesired behavior that you are facing. Thus, we would be able to investigate the precise case and think about a suitable solution.

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

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

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