2013-01-25 89 views
2

我有在Windows窗体中更改行颜色的问题。我用Columns做了它,并尝试了相同的行,但它没有工作。有人可以告诉我该怎么做吗?在datagridview中更改行backcolor

我迄今为止代码:

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 


    } 
} 
+0

像http://stackoverflow.com/a/14508584/1080742 – spajce

回答

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 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:哪里的cellpainting事件是?

enter image description here

+0

感谢,我有一个愚蠢的问题,我无法找到Cellpainting事件,我怎么能增加吗? –

+0

如果您使用窗体设计器创建了DataGridView,则CellPainting事件位于“显示”中,位于“鼠标”上方。其他附近的事件是“CellFormatting”,“Cellparsing”等。您可以通过编程方式创建它,但正如我所说的,如果您使用设计器创建了datagridview,则可以使用它来创建事件。 – Andres

+0

检查我贴的图片 – Andres

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的更多信息。

+0

谢谢,但如果我不想画所有的行,只有几个,所以我会怎么做? –

+0

你是什么意思只是几个?知道该画什么和哪些不画的条件是什么?使用我的代码来做到这一点..我会更新我的答案,在一种颜色上绘制一对行,在另一种颜色上绘制一排障碍行 – Andres

+0

@LeVietHung查看我的更新 –

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