我有一个GridView
,其中有两个日期列EditTemplates
。我该如何比较2个日期?
答案 0 :(得分:0)
通常我认为我会做这样的事情:
if (Convert.ToDateTime(GridView1.Rows[1].Cells[1].Text) > DateTime.Now)....
如果DateDiff()
不适合您,您可以使用DateTime.Compare()
查找日期之间的差异。
你有什么尝试?
答案 1 :(得分: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
您可以根据实际情况更改索引或任何内容。
我希望它会帮助你......