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

发布于 2024-12-16 22:43:13 字数 86 浏览 5 评论 0原文

我有一个 GridView ,其中有两个日期列,它们是 EditTemplates 。如何比较这两个日期?

I have a GridView with two Date columns which are EditTemplates. How do I compare the 2 dates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-12-23 22:43:13

通常我想我会这样做:

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

您可以使用 DateDiff() 查找日期之间的差异,如果 DateTime.Compare()不适合你。

你尝试了什么?

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.

What have you tried?

妖妓 2024-12-23 22:43:13

尝试这样..

<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

您可以根据实际情况更改索引或任何内容。

我希望它能帮助你......

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....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文