如何根据日期更改Gridview颜色 [英] How Do I Change Change Gridview Color Based On Date

查看:66
本文介绍了如何根据日期更改Gridview颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,请你帮我吧..如果日期过去,我想改变gridview行的颜色...我是编程的新手,帮我解决这个问题......

解决方案

试试这个..



  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime date =(DateTime)DataBinder。 Eval(e.Row.DataItem, DateColumnofYourDataSource);
if (DateTime.Now > date)
e.Row.BackColor = System.Drawing.Color.Red;
else
e.Row.BackColor = System.Drawing.Color.Green;
}
}


  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

Label lbldate =(Label )e.Row.FindControl( 标签的ID); // 哪一个包含Gridview中的日期字段

datetime presentdate;
presentdate = System.Datetime.Now.Tostring();
if (lbldate.text> presentdate)
{

e.Row.BackColor = System.Drawing.Color .FromName( #ffcdea);
}

else
{
e.Row.BackColor = System.Drawing.Color.FromName ( #FFFFFF);
}

}
}


使用Gridview1_RowDataBound事件



尝试这样(根据您的要求更改代码): -

  protected   void  GridView1_RowDataBound( object  sender,GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
int dtrslt = DateTime.Compare(e.Row.DataItem( DATE_COLUMN_NAME),Date.Now );
if (dtrslt < 0
e.Row.BackColor = System.Drawing.Color.Red;
// 或者对于十六进制颜色
e.Row.BackColor = ColorTranslator。 FromHtml( #D1ECC2);
else
e.Row.BackColor = System.Drawing.Color.yellow;
// 或者对于十六进制颜色
e.Row.BackColor = ColorTranslator。 FromHtml( #E7B3B3
}
}


hello can u please help me on this ..i want to to change the color of the row of gridview if the date is passed ...i am new to programming help me to solve this ...

解决方案

Try this..

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
       {

           if (e.Row.RowType == DataControlRowType.DataRow)
           {
               DateTime date = (DateTime)DataBinder.Eval(e.Row.DataItem, "DateColumnofYourDataSource")   ;
               if (DateTime.Now > date )
                   e.Row.BackColor = System.Drawing.Color.Red;
               else
                   e.Row.BackColor = System.Drawing.Color.Green;
           }
       }


protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

 Label lbldate= (Label)e.Row.FindControl("id of your label"); //which one contain the date field in Gridview

datetime presentdate;
presentdate=System.Datetime.Now.Tostring();
if (lbldate.text> presentdate)
               {
                  
                   e.Row.BackColor = System.Drawing.Color.FromName("#ffcdea");
               }

else
{
  e.Row.BackColor = System.Drawing.Color.FromName("#FFFFFF");
}

}
}


use Gridview1_RowDataBound event

try like this(change in code as per your requirement):-

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int dtrslt = DateTime.Compare(e.Row.DataItem("DATE_COLUMN_NAME"), Date.Now);
        if ( dtrslt < 0)
     e.Row.BackColor = System.Drawing.Color.Red;
                   //Or for hex color
    e.Row.BackColor = ColorTranslator.FromHtml("#D1ECC2");
        else
     e.Row.BackColor = System.Drawing.Color.yellow;
    //Or for hex color
            e.Row.BackColor = ColorTranslator.FromHtml("#E7B3B3")
    }
}


这篇关于如何根据日期更改Gridview颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆