0

我有一个网格视图,其中一个绑定字段包含一个日期(CureExpires),如果日期早于当前日期和时间,我希望将此日期替换为“过期”一词。我将如何做到这一点?

它的外观示例: 在此处输入图像描述

我的代码:

<asp:GridView runat="server" ID="PendingList" CssClass="wiretable" AutoGenerateColumns="False"
     AllowPaging="True" BorderColor="#E8CC6B" BorderStyle="Solid" BorderWidth="1px"
     Width="100%" OnPageIndexChanging="PendingList_PageIndexChanging" PageSize="10"
     ShowFooter="True" OnRowDataBound="PendingList_RowDataBound">
     <Columns>
       <asp:BoundField DataField="MemberName" HeaderText="Member Name" />
       <asp:BoundField DataField="Owes" HeaderText="Owes" />
       <asp:BoundField DataField="Paid" HeaderText="Paid" />
       <asp:BoundField DataField="CureExpires" DataFormatString="{0:MM/dd/yy}"
                        HeaderText="Expires" />
       <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="Payment.aspx?ID={0}"
                        Text="Record Payment" HeaderText="" />
      </Columns>
</asp:GridView>

更新:更多代码

  public partial class Branch : System.Web.UI.Page
{
    public class Member
    {
        public int ID { get; set; }
        public string MemberName { get; set; }
        public int Owes { get; set; }
        public int Paid { get; set; }
        public DateTime? CureExpires { get; set; }
    }
 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            bindGridView();
    }
  public void bindGridView()
    {
        var id = Request.Params["ID"];
        string connStr = "";
        SqlConnection mySQLconnection = new SqlConnection(connStr);
        if (mySQLconnection.State == ConnectionState.Closed)
        {
            mySQLconnection.Open();
        }
        SqlCommand mySqlCommand = new SqlCommand(@"SELECT
                                   C.ID, C.MemberName, C.CureExpires,
                               C.CureAmt - COALESCE(SUM(P.PaymentAmt),0) as Owes,
                               COALESCE (SUM(P.PaymentAmt),0) as Paid,
                               C.CureAmt
                            FROM
                               Cure C
                               LEFT JOIN CurePayment P
                               ON C.ID = P.CureID
                            WHERE
                           C.Status = '2' and C.TransWorldAcct = '0'
                           and C.Branch = " + "'" + id + "'" + " GROUP BY C.MemberName, C.CureExpires, C.CureAmt, C.ID", mySQLconnection);

        SqlDataAdapter mySqlAdapter = new SqlDataAdapter(mySqlCommand);
        DataSet myDataSet = new DataSet();
        mySqlAdapter.Fill(myDataSet);
        PendingList.DataSource = myDataSet;
        PendingList.DataBind();
        if (mySQLconnection.State == ConnectionState.Open)
        {
            mySQLconnection.Close();
        }
     }
    protected void PendingList_RowDataBound(object sender,
                                                GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[2].ForeColor = System.Drawing.Color.Green;
            e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;

            var expiresLiteral = e.Row.FindControl("ExpiresLiteral") as Literal;
            var member = e.Row.DataItem as Member;

            if (!member.CureExpires.HasValue)
                expiresLiteral.Text = "N/A";
            else if (member.CureExpires.Value < DateTime.Now)
                expiresLiteral.Text = "Expired";
            else
                expiresLiteral.Text = member.CureExpires.Value.ToShortDateString();
           }
4

1 回答 1

4

你快到了。你只需要在里面添加逻辑PendingList_RowDataBound

在此处输入图像描述

public class Member
{
    public int ID { get; set; }
    public string MemberName { get; set; }
    public int Owes { get; set; }
    public int Paid { get; set; }
    public DateTime? CureExpires { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        PendingList.DataSource = new List<Member>
            {
                new Member {ID = 1, MemberName = "John", Owes = 10, 
                    Paid = 5, CureExpires = new DateTime(2013, 5, 25)},
                new Member {ID = 2, MemberName = "Sam", Owes = 100, 
                    Paid = 50, CureExpires = new DateTime(2013, 5, 23)},
                new Member {ID = 3, MemberName = "Mark", Owes = 12, 
                    Paid = 2, CureExpires = new DateTime(2013, 6, 1)},
                new Member {ID = 3, MemberName = "Lisa", Owes = 40, 
                    Paid = 35, CureExpires = null},
            };
        PendingList.DataBind();
    }
}

protected void PendingList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var expiresLiteral = e.Row.FindControl("ExpiresLiteral") as Literal;
        var member = e.Row.DataItem as Member;

        if (!member.CureExpires.HasValue)
            expiresLiteral.Text = "N/A";
        else if (member.CureExpires.Value < DateTime.Now)
            expiresLiteral.Text = "Expired";
        else
            expiresLiteral.Text = member.CureExpires.Value.ToShortDateString();
    }
}

<asp:GridView runat="server" ID="PendingList" AutoGenerateColumns="False" 
  OnRowDataBound="PendingList_RowDataBound">
    <Columns>
        <asp:BoundField DataField="MemberName" HeaderText="Member Name" />
        <asp:BoundField DataField="Owes" HeaderText="Owes" />
        <asp:BoundField DataField="Paid" HeaderText="Paid" />
        <asp:TemplateField HeaderText="Expires">
            <ItemTemplate>
                <asp:Literal runat="server" ID="ExpiresLiteral" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:HyperLinkField DataNavigateUrlFields="ID"   
             DataNavigateUrlFormatString="Payment.aspx?ID={0}"
            Text="Record Payment" HeaderText="" />
    </Columns>
</asp:GridView>

为 DataSet 数据源更新

protected void PendingList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DateTime cureExpires;
        var expiresLiteral = e.Row.FindControl("ExpiresLiteral") as Literal;
        var obj = ((DataRowView) e.Row.DataItem)["CureExpires"];

        if (obj != null && DateTime.TryParse(obj.ToString(), out cureExpires))
        {
            if (cureExpires < DateTime.Now)
                expiresLiteral.Text = "Expired";
            else
                expiresLiteral.Text = cureExpires.ToShortDateString();
        }
        else
        {
            expiresLiteral.Text = "N/A";
        }
    }
}
于 2013-05-24T14:33:20.260 回答