Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改DataGridView中按钮的颜色_C#_.net_Winforms_Datagridview_Datagridviewbuttoncolumn - Fatal编程技术网

C# 更改DataGridView中按钮的颜色

C# 更改DataGridView中按钮的颜色,c#,.net,winforms,datagridview,datagridviewbuttoncolumn,C#,.net,Winforms,Datagridview,Datagridviewbuttoncolumn,我到处寻找这个问题的答案。这篇帖子上的答案:不回答我关于字体的问题 我尝试了以下方法: DataGridViewRow r = dataGridView.Rows[0]; r.Cells[1].Style.BackColor = Color.Red; 我也尝试过: DataGridViewButtonColumn btnCOl = new DataGridViewButtonColumn(); btnCOl.FlatStyle = FlatStyle.Popup; DataGridViewRo

我到处寻找这个问题的答案。这篇帖子上的答案:不回答我关于字体的问题

我尝试了以下方法:

DataGridViewRow r = dataGridView.Rows[0];
r.Cells[1].Style.BackColor = Color.Red;
我也尝试过:

DataGridViewButtonColumn btnCOl = new DataGridViewButtonColumn();
btnCOl.FlatStyle = FlatStyle.Popup;
DataGridViewRow r = dataGridView.Rows[0];
r.Cells[1].Style = new DataGridViewCellStyle { BackColor = Color.LightBlue };
还是没有用

我还注释了这一行:

// Application.EnableVisualStyles();
如果有人知道如何更改DataGridViewButtonColumn中单个按钮的背景色,请提供帮助

编辑:
我想为列中的单元格设置不同的颜色,例如,一些单元格为红色,而其他单元格为绿色。我不想为整个列设置颜色。

更改整个列的背景色

作为一个选项,您可以将
DataGridViewButtonColumn
的属性设置为
Flat
,并将其设置为
样式。BackColor
为所需的颜色:

var C1 = new DataGridViewButtonColumn() { Name = "C1" };
C1.FlatStyle = FlatStyle.Flat;
C1.DefaultCellStyle.BackColor = Color.Red;

更改单个单元格的背景色


如果要为不同的单元格设置不同的颜色,在将列或单元格的
FlatStyle
设置为
Flat
后,将不同单元格的
Style.BackColor
设置为不同的颜色就足够了:

var cell = ((DataGridViewButtonCell)dataGridView1.Rows[1].Cells[0]);
cell.FlatStyle =  FlatStyle.Flat;
dataGridView1.Rows[1].Cells[0].Style.BackColor = Color.Green;
如果要有条件地更改单元格的背景色,可以基于单元格值在
CellFormatting
事件中进行更改

注意

如果您喜欢
按钮的标准外观而不是平面样式,则可以处理
CellPaint
事件:

void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
    if (e.RowIndex < 0 || e.ColumnIndex < 0)
        return;
    if (e.ColumnIndex == 0) // Also you can check for specific row by e.RowIndex
    {
        e.Paint(e.CellBounds, DataGridViewPaintParts.All
            & ~( DataGridViewPaintParts.ContentForeground));
        var r = e.CellBounds;
        r.Inflate(-4, -4);
        e.Graphics.FillRectangle(Brushes.Red, r);
        e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground);
        e.Handled = true;
    }
}
void dataGridView1\u CellPaint(对象发送方,DataGridViewCellPaintingEventArgs e)
{
如果(e.RowIndex<0 | | e.ColumnIndex<0)
返回;
如果(e.ColumnIndex==0)//还可以通过e.RowIndex检查特定行
{
e、 绘制(如CellBounds、DataGridViewPaintParts.All
&~(DataGridViewPaintParts.ContentForeground));
var r=e.CellBounds;
r、 充气(-4,-4);
e、 图形。填充矩形(画笔。红色,r);
e、 绘制(例如CellBounds、DataGridViewPaintParts.ContentForeground);
e、 已处理=正确;
}
}
试试这个

DataGridViewButtonCell bc = new DataGridViewButtonCell();
bc.FlatStyle = FlatStyle.Flat;
bc.Style.BackColor = Color.AliceBlue;
您可以将此单元格分配给所需的行

下面是一个小示例,其中已在表单中插入了一个DataGridView dgvSample

for (int i = 0; i <= 10; i++)
{
    DataGridViewRow fr = new DataGridViewRow();
    fr.CreateCells(dgvSample);

    DataGridViewButtonCell bc = new DataGridViewButtonCell();
    bc.FlatStyle = FlatStyle.Flat;

    if (i % 2 == 0)
    {
        bc.Style.BackColor = Color.Red;
    }   
    else
    {
        bc.Style.BackColor = Color.Green;
    }

    fr.Cells[0] = bc;
    dgvSample.Rows.Add(fr);
}

用于(int i=0;i Aghael,我只想为列中的不同单元格而不是整个列设置颜色如果要为不同的单元格设置不同的颜色,在将
列的
FlatStyle
设置为
Flat
后,将不同单元格的
Style.BackColor
设置为不同的颜色就足够了。请确保阅读最后一个代码部分,它使您能够使用按钮的标准外观,而不是平面样式。请记住,使用
CellFormatting
事件比使用for循环要好得多。您可以简单地使用btnCOl.style.BackColor=Color.LightBlue;.NET当然知道如何带来痛苦。为什么他们甚至添加了功能勉强正常的DataGridViewBUttoColumn类?现在我必须重写所有内容。