在datagridview中更改行backcolor

时间:2013-01-25 13:40:48

标签: c# winforms datagridview backcolor

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

我的代码到目前为止:

 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


    }
}

3 个答案:

答案 0 :(得分:5)

使用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;

        }

if检查是否为Header。使用你想要的颜色..如果你不想绘制标题,只需删除if。

中的所有代码

我想要一个渐变背色告诉我..

编辑:

这是一个用一种颜色绘制对行并在另一种颜色中绘制的代码。您也必须使用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;
            }
        }

编辑2:细胞绘画事件在哪里?

enter image description here

答案 1 :(得分:3)

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

dataGridView1.RowsDefaultCellStyle.BackColor = Color.Orange;

UPDATE(如果你只想画一些行)你可以使用CellFormatting事件:

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

您可以阅读有关changing DataGridView styles on msdn的更多信息。

答案 2 :(得分:0)

 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