Angel Koh
Angel Koh

Reputation: 13525

how to map datatable's Index to datagridview's rowId?

i am writing a viewer using datagridview to load a text file and display an m row x n column table (the table is editable by the user).

the main aim of this viewer is to change the backcolor of the max in each column to red, and the min of each column to green, and also cells that exceeds a certain threshold to pink. (i use a button to trigger the checkMinMax() method, which iterates through each column of the dataTable to extract lists of rows for min/max/exceed )

i am able to do this when i first load the data into the datatable. however, when i sort the datagridview and call the checkMinMax() again, the color is still fixed at the old position.

public void setCellBackColor (List<int> rows, int col, Color color)
{
    foreach (int row in rows)
    { 
        dataGridView_data.Rows[row].Cells[col].Style.BackColor = color;  
    }
}

i understand that i am using the index of the datatable rather than the rowId of the datagridview. so my question is if there is a way to map my datatable's index to the rowId?

or is there a better way to do what i want to do ?

Upvotes: 1

Views: 656

Answers (1)

Mathieu
Mathieu

Reputation: 4520

Just recall your setCellBackColumn after sorting your data. Row highlighting is not dynamic, so you have to recall it. You may do so by using the Sorted event of the grid. Don't forget to specifically set the default color as white so it doesn't keep the outdated colors.

Upvotes: 1

Related Questions