根据asp.net C#中的当前日期更改Gridview行值 [英] Change Gridview row value based on current date in asp.net C#

查看:123
本文介绍了根据asp.net C#中的当前日期更改Gridview行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决这个问题。我想检查收到的日期(DateOfRecd)是否从当前日期开始传递,那么它应该是红色的。

但我不能。请帮帮我。我的代码是:

 < asp:BoundField DataField =ItemTypeNameHeaderText =ItemTypeNameSortExpression =ItemTypeName/> ; 
< asp:BoundField DataField =BrandNameHeaderText =BrandNameSortExpression =BrandName/>
< asp:BoundField DataField =ModelNameHeaderText =ModelNameSortExpression =ModelName/>
< asp:BoundField DataField =ItemSerialHeaderText =ItemSerialSortExpression =ItemSerial/>
< asp:BoundField DataField =DateOfRecdHeaderText =DateOfRecdDataFormatString ={0:dd-MMM-yy}SortExpression =DateOfRecd/>

我的C#代码是:

<$ p $如果(e.RowType == DataControlRowType.DataRow)
{
string v_ExpiryDate =(字符串)DataBinder.Eval(e.Row.DataItem,DateOfRecd);
string Test = DateTime.Compare(DateTime.Now,Convert.ToDateTime(v_ExpiryDate))。ToString();
if(Test ==0)
{
e.Row.BackColor = System.Drawing.Color.Red;
}
else
{
e.Row.BackColor = System.Drawing.Color.White;




解决方案

我非常怀疑 Test的值会永远是 0 DateTime.Compare 给出3个可能的值,-1,0和1.如果T1小于T2,则为负,当T1大于T2时为1。
当两个日期相同时这个值只会是0,并且这是非常不可能的,因为 DateTime.Now 会给你第二个时间。因此,如果 DateOfRecd 不准确到第二次打开页面时,GridView行永远不会变红。



我认为你只需要日期部分,而不是小时和秒。所以使用`DateTime.Now.Date'

  string Test = DateTime.Compare(DateTime.Now.Date,Convert.ToDateTime v_ExpiryDate))的ToString(); 

或直接比较日期。



<$如果(DateTime.Now.Date> ExpiryDate)
{
e.Row.BackColor = System.Drawing.Color.Red; p $ p>
}


I am not able to solve this. I want to check if the received date (DateOfRecd) is passed from current date, then it should be red.

But I cant. Please help me. My code is:

<asp:BoundField DataField="ItemTypeName" HeaderText="ItemTypeName" SortExpression="ItemTypeName" />
<asp:BoundField DataField="BrandName" HeaderText="BrandName" SortExpression="BrandName" />
<asp:BoundField DataField="ModelName" HeaderText="ModelName" SortExpression="ModelName" />
<asp:BoundField DataField="ItemSerial" HeaderText="ItemSerial" SortExpression="ItemSerial" />
<asp:BoundField DataField="DateOfRecd" HeaderText="DateOfRecd" DataFormatString="{0:dd-MMM-yy}" SortExpression="DateOfRecd" />

My C# Code is:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string v_ExpiryDate = (string)DataBinder.Eval(e.Row.DataItem, "DateOfRecd");
        string Test = DateTime.Compare(DateTime.Now, Convert.ToDateTime(v_ExpiryDate)).ToString();
        if (Test == "0")
        {
            e.Row.BackColor = System.Drawing.Color.Red;
        }
        else
        {
            e.Row.BackColor = System.Drawing.Color.White;
        }
    }
}

解决方案

I highly doubt the value of Test will ever be 0. DateTime.Compare gives 3 possible values, -1, 0 and 1. It will be negative if T1 is smaller than T2 and 1 when T1 is bigger than T2. The value will only be 0 when the two dates are the same and that is highly unlikely since DateTime.Now give you the time to the second. So if DateOfRecd is not accurate to the second the moment you open the page the GridView row will never be red.

I think you only want the Date part, not the hours and seconds. So use `DateTime.Now.Date'

string Test = DateTime.Compare(DateTime.Now.Date, Convert.ToDateTime(v_ExpiryDate)).ToString();

Or just compare the dates directly.

if (DateTime.Now.Date > ExpiryDate)
{
    e.Row.BackColor = System.Drawing.Color.Red;
}

这篇关于根据asp.net C#中的当前日期更改Gridview行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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