比较数据网格视图值

本文关键字:视图 网格 数据网 数据 比较 | 更新日期: 2023-09-27 17:56:44

我有一个 C# 中的 WinForms 应用程序,它在 DataGridView 中注册支持票证,每个票证都有一个状态,可能是挂起或已完成。我想实现一种方法来验证票证是否完成,则无法修改此记录。我尝试的是将 DataGridView 的"状态"列的每一行中的单元格值与字符串进行比较,如果它们相等,则禁用更新注册表值的按钮。我知道它一定有其他方式,但我是一个菜鸟。

foreach (DataGridViewRow rows in DataGridView1.Rows)
        {
            if (rows.Cells[10].Value.ToString().Equals("Completed"))
            {
                btnUpdate.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = true;
            }
        }

这是我尝试过的,但什么也没做。你能帮我这个吗?或者给我一些关于如何完成我需要的建议。

比较数据网格视图值

使用 RowEnter 事件:

private void DataGridView1_RowEnter(object sender,DataGridViewCellEventArgs e)
{
  btnUpdate.Enabled=!DataGridView1.Rows[e.RowIndex].Cells[10].Value.ToString().Equals("Completed") ;  
}

也许,在填充网格视图后需要以下内容(我不记得在初始化时是否引发 RowEnter 事件)。

btnUpdate.Enabled=DataGridView1.CurrentRow!=null &&
  !DataGridView1.CurrentRow.Cells[10].Value.ToString().Equals("Completed")