在datagridview中改变行backcolor [英] changing row backcolor in datagridview

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

问题描述

我在Windows窗体中改变行颜色有问题。我用列做了,并对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在dataGridView1.Rows中)//尝试将所有行更改为橙色
dr.DefaultCellStyle.BackColor = Color.Orange; //但是它不起作用

dataGridView1.Rows [0] .DefaultCellStyle.BackColor = Color.Orange; //不起作用
dataGridView1.Refresh();
dataGridView1.Update();

dataGridView1.Columns [0] .DefaultCellStyle.BackColor = Color.Beige; //这个工程


}
}


解决方案

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



我想渐变backcolor告诉我。



编辑:



这是一个代码,用于绘制一种颜色的对行,并在另一种颜色中损坏。您必须使用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:细胞固定事件在哪里? p>


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?

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


    }
}

解决方案

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

        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.

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..

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?

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

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