我有一个带有 AlternatingRowsDefaultCellStyle 的 dataGridView
一切都很好,一行白色,所有列都是灰色。
但现在我添加了一列
DataGridViewComboBoxDisplayStyle.DropDownButton
并且对于该列,只有颜色每行不交替。
有什么提示如何用组合框交替列中的行列吗?
我设法做到了这一点,执行以下操作:
使用 Visual Studio 设计器,编辑列,将组合框列 DisplayStyle 设置为“Nothing”。这是让所应用的颜色显示出来所必需的。
在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) ;
}
注意: 确保应用白色背景色。否则,一些“时髦”的东西会通过“无”显示样式渗透。