how to change color of a column in datagridview?

不羁岁月 提交于 2019-12-21 03:38:08

问题


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?


回答1:


Try setting the DefaultCellStyle property for the selected columns.

Edit:

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



回答2:


just change the style for the DataGridViewColumn object,

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



回答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;



回答4:


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

        firstColumn.DefaultCellStyle = cellStyle;


来源:https://stackoverflow.com/questions/7491700/how-to-change-color-of-a-column-in-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!