2013-02-22 42 views

回答

4

你可能想使用在GridView的RowDataBound事件,像这样:

<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="False" 
     DataSourceID="SqlDataSource1" OnRowDataBound="GridView1_RowDataBound"> 

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType != DataControlRowType.DataRow) 
      return; 

     for (int i = 0; i < e.Row.Cells.Count; i++) 
     { 
      TableCell Cell = e.Row.Cells[i]; 

      // if both row and column are odd, color then black 
      // if both row and column are even, color then yellow 
      if (((e.Row.RowIndex % 2 == 1) && (i % 2 == 1)) || 
       ((e.Row.RowIndex % 2 == 0) && (i % 2 == 0))) 
       Cell.BackColor = Color.Black; 
      else 
       Cell.BackColor = Color.Yellow; 
     } 
    } 
+0

是的,谢谢!这是工作。我可以排除第一列吗?我想第一列是黑色的,因为它是一个矩阵。 – Equilibrium 2013-02-22 18:56:44

+0

在int = 1处启动for循环,这将排除第一列 – MUG4N 2013-02-22 19:19:38

+0

要成为第一列黑色,我使用下面的代码(内部行数据绑定)并且工作完美。 谢谢。问题解决了。 'string color =“#000000”; (“Style”,“background-color:”+ color +“;”);' – Equilibrium 2013-02-22 19:21:49

0

设置ItemStyle和AlternatingItemStyle属性,并指定BackGroundColor和Color属性。这些控制这些颜色。

+0

谢谢你的回复。我忘了提及从page_load上调用的存储过程动态填充gridview。 GridView是一个矩阵,存储过程包含数据透视表。所以我没有列将它们转换为模板 – Equilibrium 2013-02-22 18:46:32