【问题标题】:Color the rows and column in c# datagridview在 c# datagridview 中为行和列着色
【发布时间】:2020-09-18 16:54:56
【问题描述】:

我正在尝试在将 excel 工作表导入 datagridview 后为行和列着色。

  1. 行(A1、A2、A3、A4、A5、A6)应以红色突出显示。 (最大行数为 35)

  2. 列(字段、记录 1、记录 2、记录 3、记录 4、记录 5、记录 6、记录 7)应以橙色突出显示。 (列数不固定,每次都会不同)。

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

但上面的代码为不需要的区域着色。如何解决这个问题。

(附图中的彩色区域应为白色,箭头指向的区域应考虑着色。)

【问题讨论】:

标签: c# visual-studio datagridview windows-forms-designer excel-reader


【解决方案1】:

您可以尝试以下代码设置第一行的颜色和第一列的颜色。

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

结果:

【讨论】:

    【解决方案2】:

    您需要设置默认行颜色和备用行颜色样式。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多