根据datagrid的列里的数值大小,用不同的颜色在datagrid的单元格中标记出来
可是我这样转换为什么会出问题        int temp1=int.Parse (e.Item .Cells [17].Text) ;
        int temp2=int.Parse (e.Item .Cells [16].Text) ;        if(temp1<temp2)
{
       for(int i=0;i<e.Item .Cells .Count ;i++)
      {
e.Item .Cells [16].BackColor =System .Drawing .Color.Yellow  ;

      }

}为什么总是报错,这种转换格式有问题吗?

解决方案 »

  1.   

    你代码写在哪里的,贴全点看看for(int i=0;i<e.Item .Cells .Count ;i++)
          {
    e.Item .Cells [16].BackColor =System .Drawing .Color.Yellow  ;      }这里让人看不懂了,循环中的语句与循环没啥关系,
      

  2.   

    这是datagrid里循环比较单元格
      

  3.   

    报什么错误,
    e.Item .Cells [16].BackColor =System .Drawing .Color.Yellow  ;
    16 是不是溢出了
      

  4.   

    把代码写到ItemDataBound事件中,下面代码参考
    private void (控件名)_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer )
    {
    if (e.Item.ItemType != ListItemType.Separator)
    {
    if (e.Item.Cells[3].Text == "补考")
    {
    e.Item.Cells[3].ForeColor = Color.Yellow;
    }
    if (e.Item.Cells[3].Text == "重考")
    {
    e.Item.Cells[3].ForeColor = Color.Red;
    }
    }
    }
    }
      

  5.   

    不是文本形式,而是数值的比较
    比如说
    e.Item.cell[1]<e.Item.cell[2]
    两列数值型的单元进行比较