filtering rows in a gridview

Go To StackoverFlow.com

1

I have a gridview that contains user info and one of the columns is a date when the user was added to the system.

Is it possible to use the GridView.onRowCreated method to check if the user was added within a given time frame? If the user was added within that timeframe, then the row is added, if not, then that row is not added.

Note that I can't modify the datasource of the gridview, so I need to do this somehow as the gridview is being created.

2012-04-04 17:12
by SkyeBoniwell
Use rowdatabound event to capture the user http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspxhuMpty duMpty 2012-04-04 17:22
"Can't modify the datasource" are you sure, that does not make sense - Magnus 2012-04-04 17:38
I see how I can use RowDataBound to change the style of a row, but I cant figure out how to actually get the gridview to display or not display the row based on a condition - thank - SkyeBoniwell 2012-04-04 17:41
Change/Increase/Decrease your DataSource before assigning it to GridView - Pankaj 2012-04-04 19:15


1

You can use RowBoundData and set teh row to hide based on your certain condition

Protected Sub GridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.DataRow Then
        e.Row.Visible = True ' base on some condition
    End If
End Sub

Another Method is to filter your Data. pass the filter expression in the below function and it will return you the desired output. You can do this before your binding.

Public NotInheritable Class GetFilteredData
    Private Sub New()
    End Sub
    <System.Runtime.CompilerServices.Extension> _
    Public Shared Function FilterDataTable(Dt As DataTable, FilterExpression As String) As DataTable
        Using Dv As New DataView(Dt)
            Dv.RowFilter = FilterExpression
            Return Dv.ToTable()
        End Using
    End Function
End Class
2012-04-04 17:37
by Pankaj


1

You can filter out your gridview datasource and bind to gridview depends upon your where condition on date column. this will be easy solution than putting condition on the gridview event.

Cheers!!

2012-04-04 17:38
by Sunil Chavan
Ads