none
Using Datatable can I find cooresponding DataGridView Row? RRS feed

  • Question

  • I want to update color of row in datagridview based off the row I found in datatable.

    foreach (DataRow dr in clsHeartbeat.dtHeartbeat.Rows)
                    {
                        DateTime dtNow = DateTime.Now;
                        DateTime dtLastSeen = Convert.ToDateTime(dr["Last Seen"]);
                        double minutes = (dtNow - dtLastSeen).TotalMinutes;
                        if (minutes >= 2)
                        {
                            Console.WriteLine($"  > 2 Minutes  {dr["Machine Name"]}  Last Seen({dr["Last Seen"]}");
                            // Find DataGridView Row and change background color.
                        }
                    }


    FordIT

    Monday, December 16, 2019 3:18 PM

All replies

  • This pattern should work for you.

    foreach (DataGridViewRow row in vendorsDataGridView.Rows) 
         if (Convert.ToInt32(row.Cells[7].Value) < Convert.ToInt32(row.Cells[10].Value)) 
         {
             row.DefaultCellStyle.BackColor = Color.Red; 
         }
    https://stackoverflow.com/questions/2189376/how-to-change-row-color-in-datagridview


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, December 16, 2019 3:30 PM
    Moderator
  • Instead of looking row by row in DataGridView, I wanted to take the row I found in the underlying datatable and somehow use that index into DataGridView so that I can update color.


    FordIT

    Tuesday, December 17, 2019 3:26 PM
  • Instead of looking row by row in DataGridView, I wanted to take the row I found in the underlying datatable and somehow use that index into DataGridView so that I can update color.


    FordIT

    In that case the DataGridView should be set to have it's DataSource set to a BindingSource and the DataSource of the BindingSource is a DataTable which includes a primary key where the DataColumn for the primary key ColumnMapping is set to Hidden.

    To find a row in the DataGridView use Find method of the BindingSource which returns -1 if not found or 0 or above the row in the DataGridView you want so assign a variable to the Find operations if over -1 set the current row in the DataGridView via Position property of the BindingSource and if you only want to example the data cast the BindingSource to a DataTable, access the row e.g. Row[pos] where pos is the value returned from the Find operation.

    So rather than a code sample I've simply given you the plan to get what you want.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, December 17, 2019 3:58 PM
    Moderator
  • Instead of looking row by row in DataGridView, I wanted to take the row I found in the underlying datatable and somehow use that index into DataGridView so that I can update color.


    FordIT

    The datatable rows 0 based index should match yhe grid rows that is 0 based indexed. So you should be should  be able to do a forloop  using an index on the datatable the to access a row in the grid based on the index used from the datatable.

    You could use Linq and query the datatable, find a column item in a datatable row, have Linq return the index of the row and use the retuned value of the index supplied by the Linq query to access a row in the grid by its index.

    Tuesday, December 17, 2019 11:40 PM