0

In my example i have got:

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

now I would like to "pick" some DataRows from DataTable and highlight it in 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;    
}

Any idee how to do this? Is it possible anyway?

Adam Filipek
  • 213
  • 1
  • 2
  • 5
  • can you tell me is this datatable is diffrent from the source od the grid why i am asking that is becuase if this is same than you not need to do the matching . You can use cellformating event and acheive this – Yashveer Singh Jan 18 '17 at 09:13
  • This DataTable is a source of the grid. – Adam Filipek Jan 20 '17 at 08:49
  • This DataTable is a source of the grid. I dont want cellformating. Im not sure, but cellformating will format each cell that fulfills terms of formating. I want to get to particular DataGridViewRow that is "connected" with my particular DataRow in DataTable. Lets assume that I would like to make "search" functionality to my application. User clicks button first time - firts row with searched value is highlighted, user clicks button next time - next row is highlighted etc. – Adam Filipek Jan 20 '17 at 08:55

1 Answers1

0

You may use this way

foreach(DataGridViewRow row in dgv.Rows)
{
    if(row.Cells[someColumn].Value.ToString().Equals(someTerm))
    {
        dgvRow.DefaultCellStyle.BackColor = Color.Red;
    }
}
kgzdev
  • 2,770
  • 2
  • 18
  • 35
  • Yes, I know i can search through all DataGridView rows, however I was hoping that there is a way to do it without looping through all rows. DataRow dataRow = ((DataRowView)dgv.Rows[x].DataBoundItem).Row; this way I can find DataGridViewRow in DataTable, maybe there is a way to do it contrariwise. – Adam Filipek Jan 18 '17 at 09:14
  • You can use dgv_CellFormatting or dgv_DataBindingComplete events instead loop – kgzdev Jan 18 '17 at 09:29