比如说我有一个dataGrid       现在所有行的颜色都一样我想对某一列做一个特殊的要求,比如说time这一列,想让time列中的值是今天时间的为红色,明天的为绿色,以后的保持原来的颜色
这个能不能实现啊,各位大虾们帮我看看啊。

解决方案 »

  1.   

    能,在rowdatabound事件里判断后设置e.Row.Cells[i].BackColor=red ,e.Row.Cells[i].BackColor=green
      

  2.   


    foreach(DataGridItem dgi in this.dg.Items)
    {
                                for(int i=0;i<dgi.Cells.Count;i++)
    {
    if(Convert.ToDateTime(dgi.Cells[15].Text)>= Convert.ToDateTime(System.DateTime.Now.AddDays(-1)))
    {
    dgi.Cells[i].BackColor = Color.Red;
    }
    } }
      

  3.   

    2 楼的代码放在绑定DataGrid之后
      

  4.   


    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                     //在这里处理
                }
            }
      

  5.   

    ItemDataBound事件中去写,比如:
    //如果TitleOfCourtesy列的值为"Mr."则设置该行的背景色为蓝色
    if((e.Item.Cells[3].Text)=="Mr.")
    e.Item.BackColor=Color.Blue;
    //如果TitleOfCourtesy列的值为"Ms."则设置该行的背景色为红色
    if((e.Item.Cells[3].Text)=="Ms.")
    e.Item.BackColor=Color.Red;
      

  6.   

    dataGrid  哪有RowDataBound这个事件啊
      

  7.   

    你把二楼的代码放在datagrid的数据绑定之后的后面
      

  8.   

    在ItemCreated事件中试下,win form少接触
      

  9.   

    放在 paint 事件里。之前 datagrid已经被绑定数据。
      

  10.   

    呵呵,楼主用的不是winform,而是用的2003的dataGrid      
    当然不会有2005 dataview的事件
      

  11.   

     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
    //判断是否今天
                if(e.Row.cells["datatime"]==datatime.now.year+"-"+datatime.now.month+"-"+datatime.now.day)
    //添加背景色
    e.Row.Attributes.Add(this.style.backgroundColor=red)
            }
        }
    就是上面这个原理了..以上代买为手写..请自己测试后在写了用
      

  12.   

    放在DataGrid 的ItemDataBound事件里protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
            {
                if (e.Item.ItemIndex!=-1)
                {
                     //在这里处理
                }
            }
      

  13.   

    日期是放在模板列里的啊,但是我的dataGrid第一列的代码是这样的
    <asp:TemplateColumn>
    <HeaderStyle HorizontalAlign="Center" ForeColor="White"></HeaderStyle>
    <ItemTemplate>
    <input type="hidden" id="SelectedID" runat="server" value='<%# DataBinder.Eval(Container.DataItem, "OrderId")%>' NAME="SelectedID"/>
    </ItemTemplate>
    </asp:TemplateColumn>绑定了一列
      

  14.   

    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (state.Equals("1"))
                {                if (!e.Row.Cells[14].Text.Equals("") && !e.Row.Cells[14].Text.Equals("&nbsp;"))
                    {
                        e.Row.Cells[14].Text = Convert.ToDateTime(e.Row.Cells[14].Text).ToString("yyyy-MM-dd");
                        if (dal.Expired(this.GridView1.DataKeys[e.Row.RowIndex].Value.ToString()))
                        {
                            e.Row.BackColor = System.Drawing.Color.FromName("#FF99CC");
                        }
                    }
                }
    }
      

  15.   

    22楼的也是gridview.你用的是datagrid.
      

  16.   

    你还在用2003?2005里是没有datagrid的
      

  17.   


    如果你是想time这一个单元格变色.foreach(DataGridItem dgi in this.dg.Items)
                {
                        if(Convert.ToDateTime(dgi.Cells[15].Text)>= Convert.ToDateTime(System.DateTime.Now.AddDays(-1)))
                        {
                            dgi.Cells[15].BackColor = Color.Red;//time所在的单元格
                        }
                    }            }代码放在你的数据绑定之后
    或者
       protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
            {
                if (e.Item.ItemIndex!=-1)
                {
                   if(Convert.ToDateTime(e.Item.Cells[15].Text)>= Convert.ToDateTime(System.DateTime.Now.AddDays(-1)))
                        {
                            e.Item.Cells[15].BackColor = Color.Red;//time所在的单元格
                        }
                 }
            }
    跟你第一列 绑定OrderId有什么关系呢?你是哪一列绑定的time 就判断哪一列啊...