TheUser
TheUser

Reputation: 77

Loop through gridview and look if todays date is between the ones spesified in gridview

I have a grid view with columns (year(checkbox))(Start),(End). I want to loop through the gridview and see between which end and start date todays date falls and highlight it and check the checkbox.

I've tried/think something like:

Dim today As String = DateTime.Now
Dim column As DataColumn
For x As Integer = 0 To Me.grdFinYear.Rows.Count - 1
    If grdFinYear.Rows(x).Cells(1).Value.ToString = column.ColumnName Then
        ' lblActiveFinYear.Text = current fin year
        ' colunm.DefaultCellStyle.BackColor = Color.Red
    End If
    '
Next

Upvotes: 1

Views: 545

Answers (2)

Sergey Litvinov
Sergey Litvinov

Reputation: 7458

Yes, as Mych said yoy can use OnRowDataBound event. Here is a sample working code:

ASPX markup:

<asp:GridView ID="gvGrid" runat="server" OnRowDataBound="gvGrid_OnRowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox runat="server" ID="cbYear"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Backend code:

Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim items = New List(Of GridSomeItem)
        items.Add(New GridSomeItem() With
                  {.StartDate = Date.Now.AddMonths(-2),
                   .EndDate = Date.Now.AddDays(-5)
                  })
        items.Add(New GridSomeItem() With
                  {.StartDate = Date.Now.AddDays(-10),
                   .EndDate = Date.Now.AddDays(10)
                  })
        items.Add(New GridSomeItem() With
                  {.StartDate = Date.Now.AddDays(-20),
                   .EndDate = Date.Now.AddDays(-10)
                  })

        gvGrid.DataSource = items
        gvGrid.DataBind()

    End Sub


    Protected Sub gvGrid_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
        If e.Row.DataItem Is Nothing Then
            Return
        End If

        Dim item As GridSomeItem = e.Row.DataItem
        Dim today As Date = Date.Now

        If today > item.StartDate And today < item.EndDate Then
            e.Row.BackColor = Drawing.Color.Red
            Dim cbYear As CheckBox = e.Row.FindControl("cbYear")

            cbYear.Checked = True

        End If

    End Sub
End Class

Public Class GridSomeItem
    Public Property StartDate As DateTime
    Public Property EndDate As DateTime
End Class

It just databound sample items and check are that satisfy condition, and then change color and value for Checkbox

Upvotes: 1

Mych
Mych

Reputation: 2553

I normally do this using the RowDataBoundEvent this fires as each row is bound. Check that the row bound is a data row and not a header or footer. You can then go to the column you are interested in and if the criteria matches you can change background colour of cell.

Upvotes: 0

Related Questions