C# 更改 datagridview 中的行背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14522930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:09:50  来源:igfitidea点击:

changing row backcolor in datagridview

c#winformsdatagridviewbackcolor

提问by Le Viet Hung

I have Problem with changing rows color in Windows Forms. I did it with Columns and tried the same for Rows but it didn't work. Can someone show me how to do it?

我在更改 Windows 窗体中的行颜色时遇到问题。我用 Columns 做了它,并为 Rows 尝试了同样的方法,但它没有用。有人可以告诉我怎么做吗?

My Code so far:

到目前为止我的代码:

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        abc();
    }

    void abc()
    {
        DataTable hh = new DataTable();
        hh.Columns.Add("1", typeof(string));
        hh.Columns.Add("2", typeof(string));
        hh.Columns.Add("3", typeof(string));

        hh.Rows.Add(new object[] { "a", "b", "c" });
        hh.Rows.Add(new object[] { "a1", "b1", "c1" });
        hh.Rows.Add(new object[] { "a2", "b2", "c2" });

        dataGridView1.DataSource = hh;

        foreach (DataGridViewRow dr in dataGridView1.Rows) // trying to change all rows to orange
            dr.DefaultCellStyle.BackColor = Color.Orange;  // but it doesn't work

        dataGridView1.Rows[0].DefaultCellStyle.BackColor = Color.Orange; // doesn't work
        dataGridView1.Refresh();
        dataGridView1.Update();

        dataGridView1.Columns[0].DefaultCellStyle.BackColor = Color.Beige; // this works


    }
}

采纳答案by Andres

Use the Datagridview CellPainting event. Just copy this code there.

使用 Datagridview CellPainting 事件。只需在那里复制此代码。

        if (e.RowIndex == -1)
        {
            SolidBrush br= new SolidBrush(Color.Blue);
            e.Graphics.FillRectangle(br, e.CellBounds);
            e.PaintContent(e.ClipBounds);
            e.Handled = true;
        }
        else
        {
                SolidBrush br= new SolidBrush(Color.Orange);
                e.Graphics.FillRectangle(br, e.CellBounds);
                e.PaintContent(e.ClipBounds);
                e.Handled = true;

        }

The if checks if its a Header or not. Use the colors you want.. If you don′t want to paint the header just erase all the code inside the if.

if 检查它是否为 Header。使用你想要的颜色.. 如果你不想绘制标题,只需擦除 if 中的所有代码。

I you want a gradient backcolor tell me..

我你想要渐变背景色告诉我..

EDIT:

编辑:

Here is a code to paint pair rows in one color and impairs in another. You must use the Cellpainting event too..

这是一个用一种颜色绘制成对行并在另一种颜色中削弱的代码。您也必须使用 Cellpainting 事件..

else
        {
            if (e.RowIndex % 2 == 0)
            {
                SolidBrush br = new SolidBrush(Color.Gainsboro);

                e.Graphics.FillRectangle(br, e.CellBounds);
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
            }
            else
            {
                SolidBrush br = new SolidBrush(Color.White);
                e.Graphics.FillRectangle(br, e.CellBounds);
                e.PaintContent(e.ClipBounds);
                e.Handled = true;
            }
        }

EDIT 2: Where the cellpainting event is?

编辑 2:cellpainting 事件在哪里?

enter image description here

在此处输入图片说明

回答by Sergey Berezovskiy

Use DataGridView.RowsDefaultCellStyleto set background color for all rows:

使用DataGridView.RowsDefaultCellStyle为所有行设置背景颜色:

dataGridView1.RowsDefaultCellStyle.BackColor = Color.Orange;

UPDATE (if you want to paint only some rows) you can use CellFormattingevent:

更新(如果你只想绘制一些行)你可以使用CellFormatting事件:

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.RowIndex > 2) // condition
        e.CellStyle.BackColor = Color.YellowGreen;
}

You can read more about changing DataGridView styles on msdn.

您可以在 msdn 上阅读有关更改 DataGridView 样式的更多信息。

回答by Naoufel Bouslama

 Private Sub Coloriage()
    Dim fin As Integer = Gridarticles.Rows.Count - 1
    Dim i As Integer
    For i = 0 To fin
        If Gridarticles("stock", i).Value < 0 Then
            Gridarticles.Rows(i).DefaultCellStyle.BackColor = Color.Red
        ElseIf Gridarticles("stock", i).Value = 0 Then
            Gridarticles.Rows(i).DefaultCellStyle.BackColor = Color.Gray
        End If
    Next
End Sub