question

AbbaiSrividhyavathi-9907 avatar image
0 Votes"
AbbaiSrividhyavathi-9907 asked LanHuang-MSFT answered

How to determine Warning or Error status based on gridview date column

I have a gridview with column named "Maturity Date". We need to send warning and error notification based on the "maturity Date". Conditions applied to determine Warning and error is below
a) warning = a week away from the Maturity Date and equal to Maturity Date
b) error = past the Maturity Date

My code condition is always returning true.

          `Protected Sub DG_Importer_OnRowDataBound(ByVal sender As System.Object, ByVal e As GridViewRowEventArgs) Handles DG_Importer.RowDataBound

         Dim MaturityDate As String = DirectCast(e.Row.DataItem, System.Data.DataRowView).Row.ItemArray(3).ToString

         Dim MD As DateTime = Convert.ToDateTime(MaturityDate)
         Dim weekAwayDate As DateTime = MD.AddDays(-6)
         Dim datepassed As DateTime = MD.AddDays(+1)

         If datepassed > MD Then
                 cell.BackColor = Drawing.Color.MistyRose
                 cell.FindControl("error").Visible = True
         Else If (weekAwayDate < MD) Then
                 cell.BackColor = Drawing.Color.LightYellow
                 cell.FindControl("warning").Visible = True                         
         End If
   End Sub`
dotnet-aspnet-generaldotnet-aspnet-webforms
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This logic should be done in the SQL query used to populate the GridView.

0 Votes 0 ·

I agree but my team lead wants it in code behind on rowbound

0 Votes 0 ·
surferonwww avatar image
0 Votes"
surferonwww answered

Below is a GridView which shows the rows of orders shipped after 1998/1/1 are highlighted in yellow.

239723-image.jpg

The event handler for the GirdView.RowDataBound event is used as follows:

 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {        
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     DataRowView drv = (DataRowView)e.Row.DataItem;
     total = total + (decimal)drv["Freight"];
     
     // Code to highlight the rows
     DateTime date = new DateTime(1998, 1, 1, 0, 0, 0);
     if ((DateTime)drv["ShippedDate"] > date)
     {
       e.Row.CssClass = "style1";
     }
    
   }
   else if (e.Row.RowType == DataControlRowType.Footer)
   {
     e.Row.Cells[1].Text = "Freight Total";
     e.Row.Cells[2].Text = String.Format("${0:N2}", total);
     
     e.Row.Cells[1].ColumnSpan = 2;
     e.Row.Cells.RemoveAt(0);            
   }        
 }


The css "style1" used in the above code is:

 <style type="text/css">
     .style1
     {
         background-color: yellow;
     }
 </style>

The data source is the Orders table of Microsoft sample SQL Server database Northwind. See below:

239677-orders.jpg



image.jpg (35.4 KiB)
orders.jpg (101.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @AbbaiSrividhyavathi-9907,

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>

240019-4.jpg
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.


4.jpg (35.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.