Color the rows and column in c# datagridview

587 views Asked by At

I'm, trying to color the rows and columns after importing the excel sheet into datagridview.

  1. Rows (A1, A2, A3, A4, A5, A6) should be highlighted in red. (maximum row count is 35)

  2. Columns (Fields, Record 1, Record 2, Record 3, Record 4, Record 5, Record 6, Record 7) should be highlighted in orange. (columns count is not fixed, will be different each time).

     dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Orange;
    dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Red;
    

But above code color the unwanted area. How to resolve this.

(colored area in the attached picture should be in white and arrow pointed area should consider the colorings.)

enter image description here

2

There are 2 answers

0
Justin On

You need to set the default row color and alternate row color styles.

https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-set-alternating-row-styles-for-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8

this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
    Color.Beige;
0
Jack J Jun On

You can try the following code to set the first row's color and the first column's color.

dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Green;
dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Red;

Result: enter image description here