【问题标题】:How to determine a DataGridViewRow from the DataRow it is bound to如何从绑定到的 DataRow 中确定 DataGridViewRow
【发布时间】:2011-11-04 02:03:39
【问题描述】:

我想在触发DataTable 事件时更改DataGridViewRow 的前景色(特别是DataColumnChangeEvent)。为此,我需要获取事件发生所在行的关联 DataGridViewRow

我有以下几点:

private void DataColumnChanged(object sender, DataColumnChangeEventArgs e)
     {
     DataColumn col = e.Column;
     DataRow row = e.Row;

     if (col != null && row != null)
        {
        if (col.ColumnName == "abc")
           {
           String str = col.ToString();

           if (str == "1")
              {
              DataGridViewRow dgvr = <somehow get row's associated DataGridViewRow> 
              ChangeRowForeColor(dgvr, "Purple");
              }
           }
        }

以下问题是相反的方向(从DataGridViewRow 获得DataRow),所以我知道至少可以通过一种方式找到它,但我似乎无法找到有关相反问题的信息。

How do I get a DataRow from a row in a DataGridView

How to determine which DataRow is bound to a DataGridViewRow

非常感谢如何从DataRow 中确定DataGridViewRow 的示例代码。

【问题讨论】:

  • 你的数据行有主键吗?

标签: c# datagridview datarow


【解决方案1】:

在行的主键上调用dataView.Find

编辑:要查找DataGridViewRowDataRowView,请致电grid.Rows[dataView.IndexOf(drv))

【讨论】:

  • 对不起,我不知道如何使用它。你能提供一个更完整/更长的例子吗?
  • 您需要获取绑定到网格的DataView。这可能是table.DefaultView 或网格的数据源,具体取决于网格和绑定。然后,拨打Find(row["PrimaryKeyColumn"])
  • 所以您的意思是“PrimaryKeyColumn”将是“Id”或“Name”之类的列名称,而不是“123”或“Fred”之类的实际值。
  • 是的;它必须是 DataTable 的主键。这将返回一个DataRowView
  • 我需要返回 DataGridViewRow,而不是 DataRowView
猜你喜欢
  • 1970-01-01
  • 2011-12-10
  • 2023-03-08
  • 1970-01-01
  • 2011-01-28
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-24
相关资源
最近更新 更多