how to change color of a column in datagridview?

72,741

Solution 1

Try setting the DefaultCellStyle property for the selected columns.

Edit:

grid.Columns["NameOfColumn"].DefaultCellStyle.ForeColor = Color.Gray;

Solution 2

just change the style for the DataGridViewColumn object,

myGrid.Columns["myColumn"].DefaultCellStyle.BackColor = Color.Red;

Solution 3

You can specify the cell background colours for a column like so using the DefaultCellStyle property of a DataGridViewColumn.

DataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Gray;

Solution 4

        DataGridViewColumn firstColumn = dataGridView.Columns[0];
        DataGridViewCellStyle cellStyle = new DataGridViewCellStyle();
        cellStyle.BackColor = Color.Grey;

        firstColumn.DefaultCellStyle = cellStyle;
Share:
72,741
MAW74656
Author by

MAW74656

Updated on July 18, 2022

Comments

  • MAW74656
    MAW74656 almost 2 years

    I have a DataGridview, and I'm setting some of the columns to readonly for data entry purposes. When I do that, the column stays the normal white (although it does not allow entry). How can I color the column gray? I've seen lots of samples on how to color rows, but not columns.

    How can I make the readonly columns look gray?

  • MAW74656
    MAW74656 over 12 years
    -Gave you +1 for the right way to change it (and especially for using the columnname instead of the index, I hate using the index), but I wanted gray.... not red. :-)
  • MAW74656
    MAW74656 over 12 years
    -I see this would work, but way more code than the other solutions.
  • Narayan
    Narayan over 10 years
    its Color.Red not Colors.Red
  • Scott Uphus
    Scott Uphus about 2 years
    ForeColor changes the text color. BackColor changes the background color.