C# DataGridView选定的单元格样式

C# DataGridView选定的单元格样式,c#,winforms,C#,Winforms,如何更改DataGridView(winforms)上的“选择样式”?使用GridView和DataGridViewCell的。处理DataGridView上的SelectionChanged事件,并添加如下代码: private void dataGridView1_SelectionChanged(object sender, EventArgs e) { foreach (DataGridViewRow row in this.dataGridView1.R

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

使用GridView和DataGridViewCell的。处理DataGridView上的SelectionChanged事件,并添加如下代码:

    private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        foreach (DataGridViewRow row in this.dataGridView1.Rows)
        {
            foreach (DataGridViewCell c in row.Cells)
            {
                c.Style = this.dataGridView1.DefaultCellStyle;
            }
        }


        DataGridViewCellStyle style = new DataGridViewCellStyle();
        style.BackColor = Color.Red;
        style.Font = new Font("Courier New", 14.4f, FontStyle.Bold);
        foreach (DataGridViewCell cell in this.dataGridView1.SelectedCells)
        {
            cell.Style = style;
        } 
    }

通过为网格的DefaultCellStyle的SelectedBackColor和SelectedForeColor指定值,可以轻松更改所选单元格的前景色和后景色

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

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


您可以尝试本文提供的解决方案。我已经测试并批准了它


希望这会有所帮助。

有了它,您甚至可以为选定的单元格绘制彩色边框

private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected == true)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
            using (Pen p = new Pen(Color.Red, 1))
            {
                Rectangle rect = e.CellBounds;
                rect.Width -= 2;
                rect.Height -= 2;
                e.Graphics.DrawRectangle(p, rect);
            }
            e.Handled = true;
        }
    }
}

无益。使用此代码在空表单上使用DataGridView进行测试(仅更改第一个选定单元格):private void dataGridView1_SelectionChanged(object sender,EventArgs e){dataGridView1.SelectedCells[0]。Style.BackColor=Color.Beige;}只有在将选择更改为另一个单元格后,样式才可见。已测试。不完整。不应用背景,而字体为。奇怪的是,背景是适用的,但“选择”的颜色覆盖了它。如果双击要编辑的单元格,您将看到背景色。SelectedBackColor,SelectedForeColor OK。似乎没有办法为不同的单元格设置不同的颜色:-(在DefaultCellStyle属性上使用SelectedBackColor和SelectedForeColor时,将对任何选定的单元格应用相同的颜色。如果要为不同的单元格设置样式,答案再次是处理SelectionChanged事件,并根据选定单元格的行或列执行一些条件赋值。阐明了我的任务ion.我的意思是:在选定单元格时,更改其整个外观(样式,包括背景)。如果您从未看到视觉变化,则更改其样式是没有意义的。我对误解深表歉意:-(.我的英语又差了。对不起)-:
private void dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
    {
        if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected == true)
        {
            e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
            using (Pen p = new Pen(Color.Red, 1))
            {
                Rectangle rect = e.CellBounds;
                rect.Width -= 2;
                rect.Height -= 2;
                e.Graphics.DrawRectangle(p, rect);
            }
            e.Handled = true;
        }
    }
}