如何在DataGridView中找到“附加”到DataTable中的DataRow的DataGridViewRow?

时间:2017-01-18 08:58:43

标签: c# datagridview datatable datarow datagridviewrow

在我的例子中,我有:

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;    
}

任何想法如何做到这一点? 反正有可能吗?

1 个答案:

答案 0 :(得分:0)

你可以这样使用

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