Le *_*ung 2 c# datagridview backcolor winforms
我在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
}
}
Run Code Online (Sandbox Code Playgroud)
使用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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2:细胞绘画事件在哪里?
