需要根据日期条件在gridview单元格中更改单元格颜色 [英] Need cell color change in gridview cell based on dates condition

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

问题描述

Hello Friends,我正在使用Asp.net c#,SqlServer 2005.



在我的SqlServer 2005数据库中,我有一个包含两个日期DateFrom和DateTo的表,这两个数据都使用数据类型为nvarchar(10)....因为我需要显示为2013/01/16。现在很好



我需要在这个日期有条件.....请帮帮我。



我有两个日期......,作为DateFrom和DateTo



此数据显示在Asp.net Gridview中......在Gridview中,DateFrom是Cell [4 ]和DateTo是Cell [5] ....现在我只想根据日期条件更改单元格的颜色。





DateFrom日期至

----------- ------------

2012/10/28 2013 / 03/30

2012/11/19 2013/03/21

2012/12/20 2013/03/26

2013 / 01/11 2013/01/12





如果DateFrom <  =今天的日期和日期来自>  =今天的日期
{
item.cells [ 4 ]。BackColor = color.FromName( #77FF77 );
item.cells [ 5 ]。BackColor = color.FromName( #77FF77\" );
}

其他 如果 DateFrom < 今天的日期和日期< todaysdate
{
item.cells [ 4 ]。BackColor = color.FromName( #FF6A6A) ;
item.cells [ 5 ]。BackColor = color.FromName( #FF6A6A);
}
其他 如果 datefrom > =今天的日期和日期> = todaysdate
{
item.cells [ 4 ]。BackColor = color.FromName( #77FF77);
item.cells [ 5 ]。BackColor = color.FromName( #77FF77\" );
}



这里#77FF77 表示绿色

#FF6A6A 表示红色。



请帮助我,我还需要其他条件。



谢谢。

解决方案

一些有用的链接可以帮助您解决这个问题 -

http://weblogs.asp.net/hpreishuber/archive/2006/ 01/09 / 434889.aspx [ ^ ]

在ASP.NET中更改GridView中行的颜色 [ ^ ]

http://www.daniweb.com/web-development/aspnet/threads/113079/gridview-change-color-to-column-values [ ^ ]


请参考一些链接,你一定会得到一些参考继续:

在ASP.NET中的GridView中更改行的颜色 [ ^ ]

单击时更改GridView行颜色而不回发 [ ^ ]

如何制作网格视图行颜色/单元格颜色/文本颜色 [ ^ ]



类似讨论:

[ ^ ]

C#如何更改datagridview中的行颜色 [ ^ ]

根据C#中的条件更改GridView行颜色 [ ^ ]

  protected   void  grdA_RowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
十进制Val = GetCellValue( 5 ,e);
if (Val!= null
{
if (Val< 0
{
ColorThisCellBackColor( 5 ,e,Color.FromArgb( 255 235 214 )); }
}
}
}





  private   void  ColorThisCellBackColor( int  ColumnIndex,GridViewRowEventArgs e,Color ColorName)
{
e.Row.Cells [ColumnIndex] .BackColor = ColorName;
}





 私人 十进制 GetCellValue(int ColumnIndex,GridViewRowEventArgs e)
{
try
{
十进制 cellval;
if (HttpUtility.HtmlDecode(e.Row.Cells [ColumnIndex] .Text).Trim()== string .Empty)
{
cellval = 0;
}
else
{
cellval = Convert.ToDecimal(HttpUtility.HtmlDecode(e.Row.Cells [ColumnIndex] ]。文本).Trim());
}

return cellval;

}
catch (例外)
{

throw;
}

}


Hello Friends, Am working on Asp.net c#, SqlServer 2005.

In my SqlServer 2005 Database I have a table with two dates DateFrom and DateTo, for this both datates is used datatypes as nvarchar(10)....Because i need to display as 2013/01/16. its fine now

I need a Condition on this dates.....please help me.

I have Two Dates...., as DateFrom and DateTo

This data is displaying in Asp.net Gridview...In Gridview the DateFrom is Cell[4] and DateTo is Cell[5]....and now just i want to change the colors of cells based on date conditions.


DateFrom Date To
----------- ------------
2012/10/28 2013/03/30
2012/11/19 2013/03/21
2012/12/20 2013/03/26
2013/01/11 2013/01/12


If DateFrom <= todays date and datefrom >= todays date
{
item.cells[4].BackColor = color.FromName ("#77FF77");
item.cells[5].BackColor = color.FromName ("#77FF77");
}

else if DateFrom  < Todays date and dateto < todaysdate
{
item.cells[4].BackColor = color.FromName ("#FF6A6A");
item.cells[5].BackColor = color.FromName ("#FF6A6A");
}
else if datefrom >= todays date and dateto >= todaysdate
{
item.cells[4].BackColor = color.FromName ("#77FF77");
item.cells[5].BackColor = color.FromName ("#77FF77");
}


here #77FF77 indicates Green color
and #FF6A6A indicates red color.

Please help me, i need good else condition.

Thanks.

解决方案

Some useful links that should help you solve this problem -
http://weblogs.asp.net/hpreishuber/archive/2006/01/09/434889.aspx[^]
Changing the color of a row in a GridView in ASP.NET[^]
http://www.daniweb.com/web-development/aspnet/threads/113079/gridview-change-color-to-column-values[^]


Please refer some links, you will surely get some reference to move on:
Changing the color of a row in a GridView in ASP.NET[^]
Change the GridView row color on click without postback[^]
How to make a Gridview Row Color/ Cell Color/ Text Color[^]

Similar discussion:
Change the color of gridview row when the row is selected[^]
C# How to change row color in datagridview[^]
Change GridView row color based on condition in C#[^]


protected void grdA_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Decimal Val= GetCellValue(5, e);
            if (Val!= null)
            {
               if (Val< 0)
               {
                 ColorThisCellBackColor(5, e, Color.FromArgb(255, 235, 214));         }
            } 
        }
    }



private void ColorThisCellBackColor(int ColumnIndex, GridViewRowEventArgs e, Color ColorName)
   {
       e.Row.Cells[ColumnIndex].BackColor = ColorName;
   }



private Decimal GetCellValue(int ColumnIndex, GridViewRowEventArgs e)
 {
     try
     {
         Decimal cellval;
         if (HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim() == string.Empty)
         {
             cellval = 0;
         }
         else
         {
             cellval = Convert.ToDecimal(HttpUtility.HtmlDecode(e.Row.Cells[ColumnIndex].Text).Trim());
         }

         return cellval;

     }
     catch (Exception)
     {

         throw;
     }

 }


这篇关于需要根据日期条件在gridview单元格中更改单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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