允许用户取消datagridview中的编辑

时间:2014-06-14 18:19:56

标签: c# datagridview

我有一个处理CellValidating和RowValidating事件的datagridview。我的问题是当我点击它然后我改变主意并想要取消当前行的编辑时这些验证方法不会让我这样做,如果它是第一行,它似乎工作当已经存在一些有效的行时,但是当它第一次胜出时,我就不会将焦点改为其他内容。

所以我的问题是如何同时验证和取消编辑datagridview行的能力?如果在取消编辑时数据网格视图中的当前行丢失,我不介意。

编辑: 这是我的验证方法:

private void Neighbours_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex == 2)
            {
                int i = 0;
                if (!int.TryParse(e.FormattedValue.ToString(), out i) || i <= 0)
                {
                    e.Cancel = true;
                    Neighbours.Rows[e.RowIndex].ErrorText = "error";
                }
            }
        }

private void Neighbours_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            DataGridViewRow r = ((DataGridView)sender).Rows[e.RowIndex];
            int dist = 0;
            if (r.Cells["NeighboursStop1"].Value == null || r.Cells["NeighboursStop1"].Value.ToString() == "" || r.Cells["NeighboursStop2"].Value == null || r.Cells["NeighboursStop2"].Value.ToString() == "")
            {
                e.Cancel = true;
                r.ErrorText = "error";
            }
            else if (r.Cells["NeighboursStop1"].Value.ToString() == r.Cells["NeighboursStop2"].Value.ToString())
            {
                e.Cancel = true;
                r.ErrorText = "error";
            }
            else if(!int.TryParse(r.Cells["NeighboursDistance"].Value.ToString(), out dist))
            {
                e.Cancel = true;
                r.ErrorText = "error";
            }
            else if (dist <= 0)
            {
                e.Cancel = true;
                r.ErrorText = "error";
            }
            else
            {
                r.ErrorText = "";
            }
        }

以下是它看起来如何发生,如果我点击Escape键,错误消息消失,但当我尝试点击其他内容时,RowValidation再次发生,我又回到了这个状态:

error

EDIT2:我使用datatable作为此数据网格视图的数据源。

1 个答案:

答案 0 :(得分:2)

我无法测试代码,但您可以尝试以下操作吗?

   private void Neighbours_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
    {

        //Include this, check to see if the row is dirty
        if (Neighbours.Rows[e.RowIndex] != null && !Neighbours.Rows[e.RowIndex].IsNewRow && Neighbours.IsCurrentRowDirty)
        {



        DataGridViewRow r = ((DataGridView)sender).Rows[e.RowIndex];
        int dist = 0;
        if (r.Cells["NeighboursStop1"].Value == null || r.Cells["NeighboursStop1"].Value.ToString() == "" || r.Cells["NeighboursStop2"].Value == null || r.Cells["NeighboursStop2"].Value.ToString() == "")
        {
            e.Cancel = true;
            r.ErrorText = "error";
        }
        else if (r.Cells["NeighboursStop1"].Value.ToString() == r.Cells["NeighboursStop2"].Value.ToString())
        {
            e.Cancel = true;
            r.ErrorText = "error";
        }
        else if(!int.TryParse(r.Cells["NeighboursDistance"].Value.ToString(), out dist))
        {
            e.Cancel = true;
            r.ErrorText = "error";
        }
        else if (dist <= 0)
        {
            e.Cancel = true;
            r.ErrorText = "error";
        }
        else
        {
            r.ErrorText = "";
        }
    }

 }