0

我有GridView两个 Date 列,它们是EditTemplates. 如何比较两个日期?

4

2 回答 2

0

通常我想我会做这样的事情:

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

如果对您不起作用,您可以使用它DateDiff()来查找日期之间的差异。DateTime.Compare()

你试过什么?

于 2011-11-19T09:36:13.580 回答
0

试试这样。。

<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

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

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

于 2011-11-19T10:27:22.713 回答