C# 允许用户在datagridview中取消编辑
我有一个处理CellValidating和RowValidating事件的datagridview。我的问题是,当我点击它,然后我改变主意,想取消对当前行的编辑时,这些验证方法不允许我这样做。如果它是第一行,当已经有一些有效行时,它似乎可以工作,但当它是第一行时,它不允许我将焦点更改为其他行 所以我的问题是,我如何既能进行验证,又能取消对datagridview行的编辑?我不介意在取消编辑时datagridview中的当前行是否丢失 编辑: 以下是我的验证方法:C# 允许用户在datagridview中取消编辑,c#,datagridview,C#,Datagridview,我有一个处理CellValidating和RowValidating事件的datagridview。我的问题是,当我点击它,然后我改变主意,想取消对当前行的编辑时,这些验证方法不允许我这样做。如果它是第一行,当已经有一些有效行时,它似乎可以工作,但当它是第一行时,它不允许我将焦点更改为其他行 所以我的问题是,我如何既能进行验证,又能取消对datagridview行的编辑?我不介意在取消编辑时datagridview中的当前行是否丢失 编辑: 以下是我的验证方法: private void Nei
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 = "";
}
}
private\u单元验证(对象发送方,DataGridViewCellValidatingEventArgs e)
{
如果(e.ColumnIndex==2)
{
int i=0;
如果(!int.TryParse(e.FormattedValue.ToString(),out i)| | i我无法测试代码,但您可以尝试以下操作吗
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 = "";
}
}
}
private void neights\u行验证(对象发送方,DataGridViewCellCancelEventArgs e)
{
//包括此项,检查行是否脏
if(neights.Rows[e.RowIndex]!=null&&&!neights.Rows[e.RowIndex].IsNewRow&&neights.IsCurrentRowDirty)
{
DataGridViewRow r=((DataGridView)sender).Rows[e.RowIndex];
int-dist=0;
if(r.Cells[“NeightursStop1”].Value==null | | r.Cells[“NeightursStop1”].Value.ToString()==“r.Cells[“NeightursStop2”]”。Value==null | | r.Cells[“NeightursStop2”]。Value.ToString()=“”)
{
e、 取消=真;
r、 ErrorText=“error”;
}
else if(r.Cells[“NeighboursStop1”].Value.ToString()==r.Cells[“NeighboursStop2”].Value.ToString())
{
e、 取消=真;
r、 ErrorText=“error”;
}
else如果(!int.TryParse(r.Cells[“NeighboursDistance”].Value.ToString(),out dist))
{
e、 取消=真;
r、 ErrorText=“error”;
}
else if(dist)提供一些代码和示例。您使用什么作为数据源?我使用datatable作为数据源