C# dataGridView 组合框 AlternatingRowsDefaultCellStyle

问题描述 投票:0回答:1

我有一个带有 AlternatingRowsDefaultCellStyle 的 dataGridView

一切都很好,一行白色,所有列都是灰色。

但现在我添加了一列

DataGridViewComboBoxDisplayStyle.DropDownButton
并且对于该列,只有颜色每行不交替。

有什么提示如何用组合框交替列中的行列吗?

c# datagridview combobox
1个回答
0
投票

我设法做到了这一点,执行以下操作:

  1. 使用 Visual Studio 设计器,编辑列,将组合框列 DisplayStyle 设置为“Nothing”。这是让所应用的颜色显示出来所必需的。

  2. 在DataGridView的CellFormatting方法中使用类似于以下代码的内容:

             if (e.ColumnIndex == #YourComboboxColumnIndex#)
             {
                 DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)#YourDatagridViewName#.Rows[e.RowIndex].Cells[#YourComboboxColumnIndex#];
                 cell.Style.BackColor = (e.RowIndex % 2 == 0) ? Color.White : Color.FromArgb(192, 255, 192) ;
             }
    

注意: 确保应用白色背景色。否则,一些“时髦”的东西会通过“无”显示样式渗透。

© www.soinside.com 2019 - 2024. All rights reserved.