简体   繁体   English

比较ASP.NET中GridView中的两个日期

[英]Compare two dates in a GridView in ASP.NET

I have a GridView with two Date columns which are EditTemplates . 我有一个带有两个Date列的GridView ,它们是EditTemplates How do I compare the 2 dates? 如何比较这两个日期?

Normally I think I would do something like this: 通常我想我会做这样的事情:

if (Convert.ToDateTime(GridView1.Rows[1].Cells[1].Text) > DateTime.Now)....

You could use DateDiff() to find the difference between the dates if DateTime.Compare() does not work for you. 如果DateTime.Compare()对您不起作用,则可以使用DateDiff()查找日期之间的差异。

What have you tried? 你尝试了什么?

try like this.. 尝试这样..

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
          <asp:TemplateField HeaderText="Start Date">
            <ItemTemplate>
              <asp:Label runat="server" ID="Label1" Text='<%# Eval("StartDate") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox runat="server" ID="TextBox1" Text='<%# Eval("StartDate","{0:d}")  %>'></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField HeaderText="End Date">
            <ItemTemplate>
              <asp:Label runat="server" ID="Label2" Text='<%# Eval("EndDate") %>'></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox runat="server" ID="TextBox2" Text='<%# Eval("EndDate","{0:d}") %>'></asp:TextBox>
            </EditItemTemplate>
          </asp:TemplateField>
          <asp:CommandField ShowEditButton="true" />
        </Columns>
</asp:GridView>  

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If (e.Row.RowState = DataControlRowState.Edit) Then
            Dim cv As CompareValidator = New CompareValidator
            e.Row.Cells(1).Controls.Add(cv)
            cv.ControlToValidate = "TextBox2"
            cv.Type = ValidationDataType.Date
            cv.Operator = ValidationCompareOperator.GreaterThan
            cv.ErrorMessage = "End date should be later than start date!"
            cv.ValueToCompare = CType(e.Row.FindControl("TextBox1"),TextBox).Text
        End If
    End Sub

You can change index or anything as your real situation. 您可以根据实际情况更改索引或任何其他内容。

I hope it will helps you.... 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM