【问题标题】:How to find DataGridViewRow in DataGridView that is "attached" to DataRow in DataTable?如何在 DataGridView 中找到“附加”到 DataTable 中的 DataRow 的 DataGridViewRow?
【发布时间】:2017-01-18 08:58:42
【问题描述】:

在我的例子中,我得到了:

DataTable dt = SomeMethodThatFillsDataTable();
DataGridView dgv = new DataGridView;
dgv.DataSource = dt;

现在我想从 DataTable 中“挑选”一些 DataRows 并在 DataGridView 中突出显示它

DataRow[] foundRows = dt.Select("someColumn = someTerm");

foreach (DataRow row in foundRows)    
{      
    DataGridViewRow dgvRow = // here i would like to get acces to DataGridViewRow "attached" to row    
    dgvRow.DefaultCellStyle.BackColor = Color.Red;    
}

任何想法如何做到这一点? 到底有没有可能?

【问题讨论】:

  • 你能告诉我这个数据表是否与网格的源不同,为什么我问这是因为如果这与你不需要进行匹配相同。您可以使用 cellformating 事件并实现这一目标
  • 这个DataTable是网格的来源。
  • 此 DataTable 是网格的来源。我不想要单元格格式。我不确定,但单元格格式化将格式化每个满足格式化条款的单元格。我想获得与 DataTable 中我的特定 DataRow “连接”的特定 DataGridViewRow。让我们假设我想为我的应用程序添加“搜索”功能。用户第一次点击按钮 - 搜索值的第一行突出显示,用户下次点击按钮 - 下一行突出显示等。

标签: c# datagridview datatable datarow datagridviewrow


【解决方案1】:

你可以这样用

foreach(DataGridViewRow row in dgv.Rows)
{
    if(row.Cells[someColumn].Value.ToString().Equals(someTerm))
    {
        dgvRow.DefaultCellStyle.BackColor = Color.Red;
    }
}

【讨论】:

  • 是的,我知道我可以搜索所有 DataGridView 行,但是我希望有一种方法可以在不遍历所有行的情况下进行搜索。 DataRow dataRow = ((DataRowView)dgv.Rows[x].DataBoundItem).Row;这样我就可以在DataTable中找到DataGridViewRow了,也许有相反的方法。
  • 您可以使用 dgv_CellFormatting 或 dgv_DataBindingComplete 事件来代替循环
猜你喜欢
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-03
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多