简体   繁体   English

在 c# datagridview 中为行和列着色

[英]Color the rows and column in c# datagridview

I'm, trying to color the rows and columns after importing the excel sheet into datagridview.我在将 Excel 工作表导入 datagridview 后尝试为行和列着色。

  1. Rows (A1, A2, A3, A4, A5, A6) should be highlighted in red.行(A1、A2、A3、A4、A5、A6)应以红色突出显示。 (maximum row count is 35) (最大行数为 35)

  2. Columns (Fields, Record 1, Record 2, Record 3, Record 4, Record 5, Record 6, Record 7) should be highlighted in orange.列(字段、记录 1、记录 2、记录 3、记录 4、记录 5、记录 6、记录 7)应以橙色突出显示。 (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.) (附图中的彩色区域应为白色,箭头尖区域应考虑着色。)

在此处输入图片说明

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:结果: 在此处输入图片说明

You need to set the default row color and alternate row color styles.您需要设置默认行颜色和备用行颜色样式。

https://docs.microsoft.com/en-us/dotnet/desktop/winforms/controls/how-to-set-alternating-row-styles-for-the-windows-forms-datagridview-control?view=netframeworkdesktop-4.8 https://docs.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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM