DataGridView选择了单元格样式

Yan*_*rez 4 c# winforms

如何更改DataGridView(winforms)上的"选择样式"?

Lui*_*uis 5

通过将值分配给Grid的DefaultCellStyle的SelectedBackColor和SelectedForeColor,您可以轻松更改选定单元格的前景色和背景色.

如果您需要进行任何进一步的样式设置,则需要处理SelectionChanged事件

编辑:(其他代码示例有错误,调整多个选定单元格[如在fullrowselect中])

using System.Drawing.Font;

private void dataGridView_SelectionChanged(object sender, EventArgs e)
        {

            foreach(DataGridViewCell cell in ((DataGridView)sender).SelectedCells)
        {
            cell.Style = new DataGridViewCellStyle()
            {
                BackColor = Color.White,
                Font = new Font("Tahoma", 8F),
                ForeColor = SystemColors.WindowText,
                SelectionBackColor = Color.Red,
                SelectionForeColor = SystemColors.HighlightText
            };
        }
        }
Run Code Online (Sandbox Code Playgroud)