首页 > 解决方案 > 是否可以禁用 DataGridView 中单个列的交替行?

问题描述

我正在尝试向已填充的 DataGridView 添加一列,但我需要此列单元格的颜色全部相同,因为它是每行上所有列的总和。

我以这种方式添加列:

dt.Columns.Add("Acumulable", typeof(float));
dt.Columns["Acumulable"].Expression = "Enero+Febrero+Marzo+Abril+Mayo+Junio+Julio+Agosto+Septiembre+Octubre+Noviembre+Diciembre";

我这样设置颜色:

DGVGastosVariables.Columns["Acumulable"].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#3498DB");

它工作正常,因为它确实改变了单元格的颜色,但它保持了交替的行模式。

有没有办法特别禁用该列的交替行?

谢谢。

标签: c#datagridview

解决方案


尝试这个。订阅您DataGridViewCellFormatting活动:

private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == /* Acumulable column index */)
    {
        e.CellStyle.BackColor = ColorTranslator.FromHtml("#3498DB");
    }
}

你也可以使用CellPainting事件。


推荐阅读