在DataGridView中更改Button的颜色 [英] Change color of Button in DataGridView

查看:1115
本文介绍了在DataGridView中更改Button的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题的答案上下搜寻。这篇文章的答案为:更改DataGridView Cell中按钮的颜色不能回答我的问题,因为它涉及字体。

I have searched high and low for an answer to this question.The answer on this post: Change Color of Button in DataGridView Cell does not answer my question as it regards font.

我尝试了以下操作:

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 };

仍然无济于事。

I还注释掉了这一行:

I also commented out this line:

// Application.EnableVisualStyles();

如果有人知道如何更改菜单中单个按钮的背景颜色DataGridViewButtonColumn ,请提供帮助。

If there is anyone who knows how to change the background color of a single button in a DataGridViewButtonColumn, please help.

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

I want to set different colors for the cells in the column e.g. some will be red while others will be green. I don't want to set the color for the whole column.

推荐答案

尝试一下

DataGridViewButtonCell bc = new DataGridViewButtonCell();
bc.FlatStyle = FlatStyle.Flat;
bc.Style.BackColor = Color.AliceBlue;

您可以将此单元格分配给所需的行

You can than assign this cell to the row you need

这是一个带有 DataGridView dgvSample 的小例子,

Here is a small example with a DataGridView dgvSample Already inserted in form

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);
}

这篇关于在DataGridView中更改Button的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆