4

I would like to change the forecolor of a DataGridViewRow when a DataTable event is fired (specifically the DataColumnChangeEvent). In order to do so I need to get the associated DataGridViewRow of the row the event occurred on.

I have the following:

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

The following questions are the reverse direction (getting a DataRow from a DataGridViewRow), so I know that it can be found one way at least, but I can't seem to find info on the reverse problem.

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

How to determine which DataRow is bound to a DataGridViewRow

Example code of how to determine a DataGridViewRow from a DataRow would be greatly appreciated.

Community
  • 1
  • 1
developer
  • 7,252
  • 14
  • 49
  • 57

1 Answers1

4

Call dataView.Find on the row's primary key.

EDIT: To find the DataGridViewRow for a DataRowView, call grid.Rows[dataView.IndexOf(drv)).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Sorry, I'm not sure how to use that. Could you provide a more complete/longer example? – developer Aug 25 '11 at 22:00
  • You need to get the `DataView` that is bound to the grid. This may be `table.DefaultView` or the grid's DataSource, depending on the grid and the binding. Then, call `Find(row["PrimaryKeyColumn"])`. – SLaks Aug 25 '11 at 22:02
  • so you mean "PrimaryKeyColumn" would be the column name like "Id" or "Name" instead of an actual value like "123" or "Fred". – developer Aug 25 '11 at 22:24
  • Yes; it must be the DataTable's primary key. This returns a `DataRowView`. – SLaks Aug 25 '11 at 23:32
  • I'm needing a `DataGridViewRow` returned though, not a `DataRowView`. – developer Aug 26 '11 at 15:59
  • Thank you, this looks to be what I want.. except I found out from an exception that there is "no Sort order is specified." :( I found this on MSDN: "If you do not explicitly specify sort criteria for DataView, the DataRowView objects in DataView are sorted based on the index of its corresponding DataRow in the DataTable.Rows DataRowCollection." So, even though it's sorted by this index, there was still no sort order specified, so it wouldn't help me would it? – developer Aug 26 '11 at 19:24
  • @dev: If there is no sort order, you can just get the index of the DataRow in its table (`table.IndexOf(row)`) – SLaks Aug 26 '11 at 19:26
  • @developer: Sorry; `table.Rows.IndexOf`. – SLaks Aug 26 '11 at 19:44
  • I think you meant table.Rows.IndexOf(row)? Thank you I believe this will work for me finally! :D – developer Aug 26 '11 at 20:13