如果验证失败,则防止gridview失去焦点

时间:2014-02-18 23:00:29

标签: c# winforms gridview telerik

我在表单上有一个Telerik radGridView,它实现了单元格和行验证。没问题,效果很好。

如果我将行保留不完整,然后单击表单上其他位置的“保存”按钮,则会出现问题。下面的RowValidating代码片段不起作用,因为一旦焦点从gridview移动到Save按钮,if(row!= null)始终为false,并且永远不会执行RowValidating代码。

 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;
            }

如何在网格中保留焦点以确保整个网格在允许用户离开网格并保存之前进行验证?

1 个答案:

答案 0 :(得分:0)

请尝试同时给出这两个条件。

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

        else
        {
            row.ErrorText = string.Empty;
        }

我没有尝试过这些代码,但希望这有帮助!