使单元格的颜色变红,如果它小于今天的日期....... ASP.Net C# [英] Make the color of cell red, if it is less than todays date....... ASP.Net C#

查看:144
本文介绍了使单元格的颜色变红,如果它小于今天的日期....... ASP.Net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨frnds,



正在使用C#和Sqlserver 2005在Asp.net Gridview上工作。



我的要求是:



在我的网页上我有Gridview,如果到期日期小于今天的日期,我有i列到期日。单元格必须变成红色。

这是我的gridview中的单元格[4]。



请你帮我怎么做,



谢谢。

解决方案

  protected   void  gvtest_OnRowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime tempDate = DateTime();
尝试 {tempDate = Convert.ToDateTime(e.Row.Cells [ 4 ]。 。修剪()); }
catch (Exception ex){}

if ( tempDate < DateTime.Now)
e.Row.Cells [ 4 ]。BackColor = System.Drawing.Color.Red;

}
}





在上面的代码中,行:

e.Row.Cells [4] .BackColor = System.Drawing.Color.Red;

会使单元格背景颜色变为红色。将第4号更改为您想要的任何列号或名称。



--Nayan


您好,



你可以使用gridview的OnRowDataBound事件。

示例:



在HTML部分;

< asp:gridview runat =serverid =gvtestonrowdatabound =gvtest_OnRowDataBoundxmlns:asp =#unknown>< / asp :gridview> 





在后面的代码中;

  protected   void  gvtest_OnRowDataBound( object  sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime tempDate = new DateTime();
尝试 {tempDate = Convert.ToDateTime(e.Row.Cells [ 4 ]。 。修剪()); }
catch (Exception ex){}

if ( tempDate < DateTime.Now)
e.Row.Cells [ 4 ]。BackColor = System.Drawing.Color.Red;

}
}





希望这会有所帮助。

Happy编码.. !!



-Nayan

[如果它解决了你的问题,请不要忘记将答案标记为正确]


为什么不使用 DateTime.Compare方法?请参阅以下链接



DateTime.Compare方法 [ ^ ]



该文章显示如下代码片段:



 DateTime date1 = new DateTime(2009,8,1,0,0,0); 
DateTime date2 = new DateTime(2009,8,1,12,0,0);
int result = DateTime.Compare(date1,date2);
字符串关系;

if(result< 0)
relationship =早于;
else if(result == 0)
relationship =与是同时;
else
relationship =晚于;





你知道如何比较日期上面的代码块?尝试在 GvManageUsers_RowDataBound 事件中实现它。



快乐编码。 :笑:


Hi frnds,

am working on Asp.net Gridview using C# and Sqlserver 2005.

my requirement is :

on my webpage i have Gridview, in this i have i column expiry date, if expiry date is less than todays date. the cell must become red.
This is cell[4] in my gridview.

Please can you help me how to do this,

Thanks.

解决方案

protected void gvtest_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime tempDate = new DateTime();
        try { tempDate = Convert.ToDateTime(e.Row.Cells[4].Text.Trim()); }
        catch(Exception ex){ }
        
        if(tempDate < DateTime.Now)
           e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
        
    }
}



In the above code, line:
e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
will make the cell background color red. Change No. 4 to whatever column number or name you want.

--Nayan


Hi,

You can use OnRowDataBound event of gridview.
Example:

In the HTML part;

<asp:gridview runat="server" id="gvtest" onrowdatabound="gvtest_OnRowDataBound" xmlns:asp="#unknown"></asp:gridview>



In the code behind;

protected void gvtest_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime tempDate = new DateTime();
        try { tempDate = Convert.ToDateTime(e.Row.Cells[4].Text.Trim()); }
        catch(Exception ex){ }
        
        if(tempDate < DateTime.Now)
           e.Row.Cells[4].BackColor = System.Drawing.Color.Red;
        
    }
}



Hope this helps.
Happy coding..!!

-Nayan
[Please do not forget to mark the answer as correct if it resolved your issue]


Why dont you make use of DateTime.Compare Method? Refer to below link

DateTime.Compare Method[^]

That article shows code snippet something like below:

DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;

if (result < 0)
   relationship = "is earlier than";
else if (result == 0)
   relationship = "is the same time as";         
else
   relationship = "is later than";



Do you get idea how to compare dates from the above code block? Try to implement it in your GvManageUsers_RowDataBound Event.

Happy coding.! :laugh:


这篇关于使单元格的颜色变红,如果它小于今天的日期....... ASP.Net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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