我有一个网格视图,其中一个绑定字段包含一个日期(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();
}