防止网格视图在验证失败时失去焦点



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

当我留下一行不完整,然后单击表单上其他地方的"保存"按钮时,就会出现问题。下面的 RowValidating 代码片段不起作用,因为一旦焦点从网格视图移动到"保存"按钮,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;
            }

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

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

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

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

相关内容

最新更新