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

Prevent gridview from losing focus if grid validation fails

2 Answers 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 19 Feb 2014, 11:17 AM
I have a radGridView on a form which has cell and row validation implemented. No problem with that, works well.

The problem arises when I leave a row incomplete and then click on the Save button elsewhere on the form. The RowValidating code snippet below does not work, because once the focus moves away from the GridView to the Save button, the 'if (row != null)' check always returns false, and the RowValidating code is never executed.

private void radGridView1_RowValidating(object sender, RowValidatingEventArgs e)
    {
        var row = e.Row as GridViewDataRowInfo;
 
        if (row != null)
        {
            var value = row.Cells["cboUnit"].Value.ToString();
            if (string.IsNullOrEmpty(value))
            {
                e.Cancel = true;
                row.ErrorText = "Unit Number is a required field";
            }
 
            else
            {
                row.ErrorText = string.Empty;
            }
     }
}


How can I retain focus in the grid to ensure that the entire grid validates before allowing the user to leave the grid?

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Feb 2014, 03:39 PM
Hello Peter,

Thank you for contacting us.

The RowValidating event is always firing on my end, no matter whether I am clicking on another row or clicking a button. However, in row validating mechanism if you cancel the validation, the current row is kept, but the editor is closed. If you want to keep the editor active, the appropriate event is CellValidating. The cell editor will be kept active until the user enters a valid value. Please refer to the attached sample video (drag and drop over the browser to play), demonstrating better my idea.

I hope this information helps. Should you have further questions, I would be glad to help.

Regards,
Desislava
Telerik
0
Peter
Top achievements
Rank 1
answered on 21 Feb 2014, 08:13 PM
Awesome!! Thanks Desislava, you just solved a major problem for me!!

Telerik support is absolutely the gold standard in software support!! Great!!
Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Peter
Top achievements
Rank 1
Share this question
or