Hi @Abbai, Srividhyavathi ,
My code condition is always returning true.
Because there is something wrong with your judgment conditions, you add and subtract on the basis of MaturityDate
and MaturityDate
makes its own judgment.
You need to re-declare a date for comparison. (My demo is compared to the first line).
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim error1 As Label = TryCast(e.Row.FindControl("error"), Label)
Dim warning As Label = TryCast(e.Row.FindControl("warning"), Label)
If e.Row.RowType = DataControlRowType.DataRow Then
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim MaturityDate As String = ""
MaturityDate = GridView1.Rows(0).Cells(1).Text
Dim MD As DateTime = Convert.ToDateTime(MaturityDate)
Dim weekAwayDate As DateTime = MD.AddDays(-6)
Dim datepassed As DateTime = MD.AddDays(+1)
Dim MaturityDate1 As String = ""
MaturityDate1 = GridView1.Rows(i).Cells(1).Text
Dim MD1 As DateTime = Convert.ToDateTime(MaturityDate1)
If datepassed > MD1 Then
e.Row.BackColor = Color.MistyRose
error1.Visible = True
warning.Visible = False
ElseIf (weekAwayDate < MD1) Then
e.Row.BackColor = Color.LightYellow
warning.Visible = True
error1.Visible = False
End If
Next
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim constr As String = ConfigurationManager.ConnectionStrings("DBCS").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT * FROM test1")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
GridView1.DataSource = cmd.ExecuteReader()
GridView1.DataBind()
con.Close()
End Using
End Using
End Sub
<asp:GridView ID="GridView1" runat="server" Width="50%" AutoGenerateColumns="false" ShowHeader="true" ShowFooter="true"
EmptyDataText="No Records Found" CellPadding="10" OnRowDataBound="GridView1_RowDataBound">
<HeaderStyle HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="MaturityDate" HeaderText="MaturityDate" />
<asp:TemplateField HeaderText="error" >
<ItemTemplate>
<asp:Label ID="error" runat="server" Text="error" Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="warning">
<ItemTemplate>
<asp:Label ID="warning" runat="server" Text="warning" Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle HorizontalAlign="Center" />
<PagerStyle CssClass="pad10" />
</asp:GridView>
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.