locked
SPGridView - problems with Paging and filtering RRS feed

  • Question

  • I'm using the below code to render a Grid from various Document Libraries. I have 3 issues though.


    1. The Filtering works, however, when I Filter a Column and then use the Paging, the content is no longer filtered.
    2. I'd like to be able to Filter on Multiple Columns
    3. I'd like to provide a better paging with something like a First - LAst button, NEXT previous, OF x numof Files, etc.

    Imports System
    Imports System.Data
    Imports System.Runtime.InteropServices
    Imports System.Web
    Imports System.Web.UI
    Imports System.Web.UI.WebControls
    Imports System.Web.UI.WebControls.WebParts
    Imports System.Xml.Serialization
    
    Imports Microsoft.SharePoint
    Imports Microsoft.SharePoint.WebControls
    Imports Microsoft.SharePoint.WebPartPages
    
    Namespace NHQ.DisplayDocuments
    
        <Guid("b2aebcac-dc52-4f37-8e42-954d2f48e8aa")> _
        Public Class WebPart1
            Inherits System.Web.UI.WebControls.WebParts.WebPart
    
            Private grid As SPGridView
            Private gridDS As ObjectDataSource
            Public URL As String() = New String(0) {}
    
            Public Function SelectData() As DataTable
                Dim site As SPWeb = SPContext.Current.Web
                Dim dataSource As New DataTable()
                Dim view As DataView = dataSource.DefaultView
    
                'dataSource.Columns.Add("ID"); 
                dataSource.Columns.Add("Title")
                dataSource.Columns.Add("ReleaseableTo")
                dataSource.Columns.Add("FileSize")
                dataSource.Columns.Add("DocumentLanguage")
                dataSource.Columns.Add("URL")
                dataSource.Columns.Add("Folder")
                dataSource.Columns.Add("Date")
    
                Dim sInput As String = ""
                Dim sTitle As String = ""
                Dim TrimDate As String = ""
    
                For Each list As SPList In site.Lists
                    If TypeOf list Is SPDocumentLibrary _
                                    AndAlso Not list.Hidden _
                                    AndAlso list.Title <> "Pages" _
                                    AndAlso list.Title <> "Images" _
                                    AndAlso list.Title <> "Workflow Tasks" _
                                    AndAlso list.BaseTemplate <> SPListTemplateType.PictureLibrary Then
    
    
                        For Each ListItem As SPListItem In list.Items
                            If ListItem("Document Language") IsNot Nothing _
                                AndAlso ListItem("Releaseable To") IsNot Nothing _
                                AndAlso ListItem("Folder") IsNot Nothing _
                                AndAlso ListItem("Title") IsNot Nothing Then
    
                                If ListItem("Title") IsNot Nothing Then
                                    sTitle = ListItem("Title").ToString()
                                Else
                                    sTitle = ""
                                End If
    
                                sInput = ListItem("Document Language").ToString()
                                Dim aMyResult As String() = sInput.Split(New String() {"#"}, StringSplitOptions.None)
    
                                sInput = ListItem("Releaseable To").ToString()
                                Dim aMyResultRelTo As String() = sInput.Split(New String() {"#"}, StringSplitOptions.None)
    
                                sInput = ListItem("Folder").ToString()
                                Dim aMyResultFolder As String() = sInput.Split(New String() {"#"}, StringSplitOptions.None)
    
                                If ListItem("Modified") IsNot Nothing Then
                                    Dim sDate As DateTime = CDate(ListItem("Modified"))
                                    TrimDate = sDate.ToShortDateString()
                                Else
                                    TrimDate = ""
                                End If
    
                                Dim iFilesize As Long = CLng(Convert.ChangeType(ListItem("File Size"), GetType(Long)))
    
                                dataSource.Rows.Add(sTitle, aMyResultRelTo(1), GetFileSize(iFilesize), aMyResult(1), ListItem.File.ServerRelativeUrl, aMyResultFolder(1), TrimDate)
                            End If
                        Next
                    End If
                Next
    
                view.Sort = "Folder"
    
                Return dataSource
            End Function
    
            Protected NotOverridable Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
                MyBase.LoadViewState(savedState)
    
                If Context.Request.Form("__EVENTARGUMENT") IsNot Nothing AndAlso Context.Request.Form("__EVENTARGUMENT").EndsWith("__ClearFilter__") Then
                    ' Clear FilterExpression 
                    ViewState.Remove("FilterExpression")
                End If
            End Sub
    
            Protected NotOverridable Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
                GenerateColumns()
                grid.DataBind()
    
                MyBase.Render(writer)
    
    
            End Sub
    
            Protected NotOverridable Overloads Overrides Sub CreateChildControls()
                Const GRIDID As String = "grid"
                Const DATASOURCEID As String = "gridDS"
    
                gridDS = New ObjectDataSource()
                gridDS.ID = DATASOURCEID
                gridDS.SelectMethod = "SelectData"
                gridDS.TypeName = Me.[GetType]().AssemblyQualifiedName
                AddHandler gridDS.ObjectCreating, AddressOf gridDS_ObjectCreating
                Me.Controls.Add(gridDS)
    
                grid = New SPGridView()
                grid.ID = GRIDID
                grid.DataSourceID = gridDS.ID
                grid.AutoGenerateColumns = False
    
                ' Paging 
                grid.AllowPaging = True
                grid.PageSize = 5
                grid.EnableViewState = True
    
                ' Sorting 
                grid.AllowSorting = True
    
                '#Region "Controls" 
                ' Filtering 
                grid.AllowFiltering = True
                grid.FilterDataFields = "Folder,Title,Date,DocumentLanguage,FileSize,ReleaseableTo"
                grid.FilteredDataSourcePropertyName = "FilterExpression"
                grid.FilteredDataSourcePropertyFormat = "{1} = '{0}'"
                AddHandler grid.RowDataBound, AddressOf grid_RowDataBound
                AddHandler gridDS.Filtering, AddressOf gridDS_Filtering
                AddHandler grid.Sorting, AddressOf grid_Sorting
    
                '#End Region 
    
                Me.Controls.Add(grid)
                Dim pager As New SPGridViewPager()
                pager.GridViewId = grid.ID
    
                Me.Controls.Add(pager)
                grid.PagerTemplate = Nothing
    
            End Sub
    
            Private Sub GenerateColumns()
    
                Dim column As New BoundField()
    
                column = New BoundField()
                column.DataField = "Folder"
                column.SortExpression = "Folder"
                column.HeaderText = "Folder"
                'column.Visible = false; 
                grid.Columns.Add(column)
    
                Dim selectTaskColumn As New TemplateField()
                selectTaskColumn.HeaderText = "Title"
                selectTaskColumn.SortExpression = "Title"
                selectTaskColumn.ItemTemplate = New NewTemplate(ListItemType.Item, "DocTitle")
                grid.Columns.Add(selectTaskColumn)
    
                column = New BoundField()
                column.DataField = "DocumentLanguage"
                column.SortExpression = "DocumentLanguage"
                column.HeaderText = "Lang"
                grid.Columns.Add(column)
    
                column = New BoundField()
                column.DataField = "Date"
                column.DataFormatString = "{0:yy-MM}"
                column.SortExpression = "Date"
                column.HeaderText = "Published"
                grid.Columns.Add(column)
    
                column = New BoundField()
                column.DataField = "FileSize"
                column.SortExpression = "Size"
                column.HeaderText = "FileSize"
                grid.Columns.Add(column)
    
                column = New BoundField()
                column.DataField = "ReleaseableTo"
                column.SortExpression = "ReleaseableTo"
                column.HeaderText = "Releaseable To"
                grid.Columns.Add(column)
    
            End Sub
    
            Private Sub gridDS_ObjectCreating(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs)
                e.ObjectInstance = Me
            End Sub
    
    
            Private Sub grid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
                If e.Row.RowType = DataControlRowType.DataRow Then
                    Dim selectedDoc As Label = DirectCast(e.Row.FindControl("DocTitle"), Label)
    
                    If selectedDoc IsNot Nothing Then
                        Dim data As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
                        Dim s As String
                        Dim returnValue As String
    
                        s = data("Url").ToString()
                        returnValue = s.Replace(" ", "%20")
                        selectedDoc.Text = "<a href=" + returnValue + ">" + data("Title").ToString() + "</a>"
                    End If
    
                    Dim filterIcon As Image = DirectCast(e.Row.FindControl("filterIcon"), Image)
                    If filterIcon IsNot Nothing Then
    
                        Dim data As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
                        Dim ModifiedDate As Date = CDate(data("Date"))
                        Dim NumberOfDays As Double
                       
                        Dim D1 As Date = Date.Now
                        Dim D2 As Date = ModifiedDate
    
                        Dim difference As TimeSpan = D1.Subtract(D2)
                        NumberOfDays = difference.TotalDays
    
                        'Randomly picked date period
                        If NumberOfDays <= 10 Then
                            filterIcon.ImageUrl = "/_layouts/1033/images/new.gif"
                        Else
                            filterIcon.ImageUrl = "/_layouts/images/blank.gif"
                        End If
                        filterIcon.Style(HtmlTextWriterStyle.MarginLeft) = "2px"
    
                    End If
    
                End If
    
                    If sender Is Nothing OrElse e.Row.RowType <> DataControlRowType.Header Then
                        Exit Sub
                    End If
    
                    Dim grid As SPGridView = TryCast(sender, SPGridView)
    
                    If [String].IsNullOrEmpty(grid.FilterFieldName) Then
                        Exit Sub
                    End If
    
                    ' Show icon on filtered column 
                    For i As Integer = 0 To grid.Columns.Count - 1
                        Dim field As DataControlField = grid.Columns(i)
    
                        If field.SortExpression = grid.FilterFieldName Then
                            Dim filterIcon As New Image()
                            filterIcon.ImageUrl = "/_layouts/images/filter.gif"
                            filterIcon.Style(HtmlTextWriterStyle.MarginLeft) = "2px"
    
                            ' If we simply add the image to the header cell it will 
                            ' be placed in front of the title, which is not how it 
                            ' looks in standard SharePoint. We fix this by the code 
                            ' below. 
                            Dim headerText As New Literal()
                            headerText.Text = field.HeaderText
    
                            Dim panel As New PlaceHolder()
                            panel.Controls.Add(headerText)
                            panel.Controls.Add(filterIcon)
    
                            e.Row.Cells(i).Controls(0).Controls.Add(panel)
    
                            Exit For
                        End If
                    Next
            End Sub
    
            Private Sub grid_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
                If ViewState("FilterExpression") IsNot Nothing Then
    
                    gridDS.FilterExpression = DirectCast(ViewState("FilterExpression"), String)
    
                End If
    
                Dim lastExpression As String = ""
                If ViewState("SortExpression") IsNot Nothing Then
                    lastExpression = ViewState("SortExpression").ToString()
                End If
    
                Dim lastDirection As String = "asc"
                If ViewState("SortDirection") IsNot Nothing Then
                    lastDirection = ViewState("SortDirection").ToString()
                End If
    
                Dim newDirection As String = "asc"
                If grid.SortExpression = lastExpression Then
                    newDirection = If((lastDirection = "asc"), "desc", "asc")
                End If
    
                ViewState("SortExpression") = grid.SortExpression
                ViewState("SortDirection") = newDirection
    
            End Sub
    
            Private Sub gridDS_Filtering(ByVal sender As Object, ByVal e As ObjectDataSourceFilteringEventArgs)
    
                ViewState("FilterExpression") = DirectCast(sender, ObjectDataSourceView).FilterExpression
    
            End Sub
    
    
            Private Function GetFileSize(ByVal Bytes As Long) As String
                If Bytes >= 1073741824 Then
                    Dim size As [Decimal] = [Decimal].Divide(Bytes, 1073741824)
                    Return [String].Format("{0:##.##} gb", size)
                ElseIf Bytes >= 1048576 Then
                    Dim size As [Decimal] = [Decimal].Divide(Bytes, 1048576)
                    Return [String].Format("{0:##.##} mb", size)
                ElseIf Bytes >= 1024 Then
                    Dim size As [Decimal] = [Decimal].Divide(Bytes, 1024)
                    Return [String].Format("{0:##.##} kb", size)
                ElseIf Bytes > 0 And Bytes < 1024 Then
                    Dim size As [Decimal] = Bytes
                    Return [String].Format("{0:##.##} bytes", size)
                Else
                    Return "0 Bytes"
                End If
            End Function
    
            Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
                If Not String.IsNullOrEmpty(gridDS.FilterExpression) Then
                    gridDS.FilterExpression = String.Format(grid.FilteredDataSourcePropertyFormat, grid.FilterFieldValue.Replace("'", "''"), grid.FilterFieldName)
                End If
                MyBase.OnPreRender(e)
    
            End Sub
        End Class
    
       
    
        Class NewTemplate
            Implements ITemplate
    
            Private _itemType As ListItemType
            Private _columnName As String
    
    
            Public Sub New(ByVal itemType As ListItemType, ByVal columnName As String)
    
                _itemType = itemType
                _columnName = columnName
    
            End Sub
    
    
            Public Sub InstantiateIn1(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
    
                Select Case _itemType
    
                    Case ListItemType.Header
    
                        Dim header As New LiteralControl()
                        header.Text = String.Format("<b>{0}</b>", _columnName)
                        container.Controls.Add(header)
                        Exit Select
    
                    Case ListItemType.Item
    
                        Dim Labelitem As New Label()
                        Labelitem.ID = "DocTitle"
                        Labelitem.Visible = True
                        container.Controls.Add(Labelitem)
    
                        Dim filterIcon As New Image()
                        filterIcon.ID = "filterIcon"
                        container.Controls.Add(filterIcon)
    
                        Exit Select
                    Case Else
    
                        Exit Select
    
                End Select
            End Sub
    
        End Class
    End Namespace
    • Moved by Mike Walsh FIN Thursday, February 25, 2010 8:57 AM When your question is about code don't post to the General forum. Moving to the programming forum (From:SharePoint - General Question and Answers and Discussion (pre-SharePoint 2010))
    Thursday, February 25, 2010 7:51 AM

All replies

  • Hi,

    1. You should put code in this: grid_PageIndexChanging() method

    2. For filtering, you can use FilterDataFields property.

    This will allow you to specify which columns should the SPGridView utilize for filtering results.

    grid.FilterDataFields=",Title, FileSize";


    -PM
    Thursday, February 25, 2010 10:25 AM
  • Apart from the above, for having custom Paging buttons like First, Last button, you need to write your custom code


    1. Add your custom buttons First, Next, Prev, Last using ASP: Buttons. Display them in an HTML table and write a function to calculate the page numbers for the above 4 situations

    otherwise, you can use either of the pagination ways available in GridView:

    2.

    XofYPager xofy = new XofYPager("{0} of {1}", grid);
    grid.PagerTemplate = xofy;

    or

    SmartPager smartpager = new SmartPager(1, grid);
    grid.PagerTemplate = smartpager;


    Thanks,
    PM

    Thursday, February 25, 2010 10:48 AM
  • Thanks for the help.  Using the below code I was finally able to get the Paging and filtering to work together finally.  I have 2 new issues now though. 

    Firstly - when using the LAST button in the Paging, I get the error 'Specified argument was out of the range of valid values. Parameter name: value '

    Secondly - when I use the filtering, the paging works but once I leave the first page, the Filtering continues to work but the 'CLEAR Filter from Folder' is greyed out and the only option in the Filters list is what is being filtered on.

    Ideas?

            Private grid As SPGridView
            Private gridDS As ObjectDataSource
            Public URL As String() = New String(0) {}
    
            Public Function SelectData() As DataTable
                Dim site As SPWeb = SPContext.Current.Web
                Dim dataSource As New DataTable()
                Dim view As DataView = dataSource.DefaultView
    
                'dataSource.Columns.Add("ID"); 
                dataSource.Columns.Add("Title")
                dataSource.Columns.Add("ReleaseableTo")
                dataSource.Columns.Add("FileSize")
                dataSource.Columns.Add("DocumentLanguage")
                dataSource.Columns.Add("URL")
                dataSource.Columns.Add("Folder")
                dataSource.Columns.Add("Date")
    
                Dim sInput As String = ""
                Dim sTitle As String = ""
                Dim TrimDate As String = ""
    
                For Each list As SPList In site.Lists
                    If TypeOf list Is SPDocumentLibrary _
                                    AndAlso Not list.Hidden _
                                    AndAlso list.Title <> "Pages" _
                                    AndAlso list.Title <> "Images" _
                                    AndAlso list.Title <> "Workflow Tasks" _
                                    AndAlso list.BaseTemplate <> SPListTemplateType.PictureLibrary Then
    
    
                        For Each ListItem As SPListItem In list.Items
                            If ListItem("Document Language") IsNot Nothing _
                                AndAlso ListItem("Releaseable To") IsNot Nothing _
                                AndAlso ListItem("Folder") IsNot Nothing _
                                AndAlso ListItem("Title") IsNot Nothing Then
    
                                If ListItem("Title") IsNot Nothing Then
                                    sTitle = ListItem("Title").ToString()
                                Else
                                    sTitle = ""
                                End If
    
                                sInput = ListItem("Document Language").ToString()
                                Dim aMyResult As String() = sInput.Split(New String() {"#"}, StringSplitOptions.None)
    
                                sInput = ListItem("Releaseable To").ToString()
                                Dim aMyResultRelTo As String() = sInput.Split(New String() {"#"}, StringSplitOptions.None)
    
                                sInput = ListItem("Folder").ToString()
                                Dim aMyResultFolder As String() = sInput.Split(New String() {"#"}, StringSplitOptions.None)
    
                                If ListItem("Modified") IsNot Nothing Then
                                    Dim sDate As DateTime = CDate(ListItem("Modified"))
                                    TrimDate = sDate.ToShortDateString()
                                Else
                                    TrimDate = ""
                                End If
    
                                Dim iFilesize As Long = CLng(Convert.ChangeType(ListItem("File Size"), GetType(Long)))
    
                                dataSource.Rows.Add(sTitle, aMyResultRelTo(1), GetFileSize(iFilesize), aMyResult(1), ListItem.File.ServerRelativeUrl, aMyResultFolder(1), TrimDate)
                            End If
                        Next
                    End If
                Next
    
                view.Sort = "Folder"
    
                Return dataSource
            End Function
    
            Protected NotOverridable Overloads Overrides Sub LoadViewState(ByVal savedState As Object)
                If True Then
                    MyBase.LoadViewState(savedState)
    
                    If Context.Request.Form("__EVENTARGUMENT") IsNot Nothing AndAlso Context.Request.Form("__EVENTARGUMENT").EndsWith("__ClearFilter__") Then
                        ' Clear FilterExpression 
                        ViewState.Remove("FilterExpression")
                    End If
                End If
    
            End Sub
    
            Protected NotOverridable Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
                GenerateColumns()
                grid.DataBind()
    
                MyBase.Render(writer)
    
    
            End Sub
    
            Protected NotOverridable Overloads Overrides Sub CreateChildControls()
                Const GRIDID As String = "grid"
                Const DATASOURCEID As String = "gridDS"
    
                gridDS = New ObjectDataSource()
                gridDS.ID = DATASOURCEID
                gridDS.SelectMethod = "SelectData"
                gridDS.TypeName = Me.[GetType]().AssemblyQualifiedName
                AddHandler gridDS.ObjectCreating, AddressOf gridDS_ObjectCreating
                Me.Controls.Add(gridDS)
    
                grid = New SPGridView()
                grid.ID = GRIDID
                grid.DataSourceID = gridDS.ID
                grid.AutoGenerateColumns = False
                Dim pagerRow As SPGridViewRow = CType(grid.BottomPagerRow, SPGridViewRow)
    
                ' Paging 
                grid.AllowPaging = True
                grid.PageSize = 5
                grid.PagerStyle.HorizontalAlign = HorizontalAlign.Center
    
                grid.PagerSettings.Mode = PagerButtons.NextPreviousFirstLast
                grid.PagerSettings.NextPageImageUrl = "~/_layouts/images/paging/go-next.png"
                grid.PagerSettings.NextPageText = "Next Page"
    
                grid.PagerSettings.PreviousPageImageUrl = "~/_layouts/images/paging/go-prev.png"
                grid.PagerSettings.PreviousPageText = "Previous Page"
    
                grid.PagerSettings.LastPageImageUrl = "~/_layouts/images/paging/go-last.png"
                grid.PagerSettings.LastPageText = "Last Page"
    
                grid.PagerSettings.FirstPageImageUrl = "/_layouts/images/paging/go-first.png"
                grid.PagerSettings.FirstPageText = "First Page"
                grid.EnableViewState = False
    
                ' Sorting 
                grid.AllowSorting = True
    
                '#Region "Controls" 
                ' Filtering 
                grid.AllowFiltering = True
                grid.FilterDataFields = "Folder,Title,Date,DocumentLanguage,FileSize,ReleaseableTo"
                grid.FilteredDataSourcePropertyName = "FilterExpression"
                grid.FilteredDataSourcePropertyFormat = "{1} = '{0}'"
    
                AddHandler grid.RowDataBound, AddressOf grid_RowDataBound
                AddHandler gridDS.Filtering, AddressOf gridDS_Filtering
                AddHandler grid.Sorting, AddressOf grid_Sorting
                AddHandler grid.PageIndexChanging, AddressOf grid_PageIndexChanging
    
                'Maintains the ViewState for the Paging
                Dim req As HttpRequest = HttpContext.Current.Request
                If req.Form("__CALLBACKID") Is Nothing OrElse req.Form("__CALLBACKPARAM") Is Nothing OrElse Not req.Form("__CALLBACKID").EndsWith("ExampleGrid") Then
                    If ViewState("FilterExpression") IsNot Nothing Then
                        gridDS.FilterExpression = DirectCast(ViewState("FilterExpression"), String)
                    End If
                End If
    
                '#End Region 
    
                Me.Controls.Add(grid)
                Dim pager As New SPGridViewPager()
                pager.GridViewId = grid.ID
    
                Me.Controls.Add(pager)
                grid.PagerTemplate = Nothing
    
            End Sub
    
            Private Sub GenerateColumns()
    
                Dim column As New BoundField()
    
                column = New BoundField()
                column.DataField = "Folder"
                column.SortExpression = "Folder"
                column.HeaderText = "Folder"
                'column.Visible = false; 
                grid.Columns.Add(column)
    
                Dim selectTaskColumn As New TemplateField()
                selectTaskColumn.HeaderText = "Title"
                selectTaskColumn.SortExpression = "Title"
                selectTaskColumn.ItemTemplate = New NewTemplate(ListItemType.Item, "DocTitle")
                grid.Columns.Add(selectTaskColumn)
    
                column = New BoundField()
                column.DataField = "DocumentLanguage"
                column.SortExpression = "DocumentLanguage"
                column.HeaderText = "Lang"
                grid.Columns.Add(column)
    
                column = New BoundField()
                column.DataField = "Date"
                column.DataFormatString = "{0:yy-MM}"
                column.SortExpression = "Date"
                column.HeaderText = "Published"
                grid.Columns.Add(column)
    
                column = New BoundField()
                column.DataField = "FileSize"
                column.SortExpression = "Size"
                column.HeaderText = "FileSize"
                grid.Columns.Add(column)
    
                column = New BoundField()
                column.DataField = "ReleaseableTo"
                column.SortExpression = "ReleaseableTo"
                column.HeaderText = "Releaseable To"
                grid.Columns.Add(column)
    
            End Sub
    
            Private Sub gridDS_ObjectCreating(ByVal sender As Object, ByVal e As ObjectDataSourceEventArgs)
                e.ObjectInstance = Me
            End Sub
    
            Private Sub Grid_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
                grid.PageIndex = e.NewPageIndex
                grid.DataBind()
            End Sub
    
    
            Private Sub grid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    
                If e.Row.RowType = DataControlRowType.DataRow Then
                    Dim selectedDoc As Label = DirectCast(e.Row.FindControl("DocTitle"), Label)
    
                    If selectedDoc IsNot Nothing Then
                        Dim data As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
                        Dim s As String
                        Dim returnValue As String
    
                        s = data("Url").ToString()
                        returnValue = s.Replace(" ", "%20")
                        selectedDoc.Text = "<a href=" + returnValue + ">" + data("Title").ToString() + "</a>"
                    End If
    
                    Dim filterIcon As Image = DirectCast(e.Row.FindControl("filterIcon"), Image)
                    If filterIcon IsNot Nothing Then
    
                        Dim data As DataRowView = DirectCast(e.Row.DataItem, DataRowView)
                        Dim ModifiedDate As Date = CDate(data("Date"))
                        Dim NumberOfDays As Double
                       
                        Dim D1 As Date = Date.Now
                        Dim D2 As Date = ModifiedDate
    
                        Dim difference As TimeSpan = D1.Subtract(D2)
                        NumberOfDays = difference.TotalDays
    
                        'Randomly picked date period
                        If NumberOfDays <= 10 Then
                            filterIcon.ImageUrl = "/_layouts/1033/images/new.gif"
                        Else
                            filterIcon.ImageUrl = "/_layouts/images/blank.gif"
                        End If
                        filterIcon.Style(HtmlTextWriterStyle.MarginLeft) = "2px"
    
                    End If
    
                End If
    
                    If sender Is Nothing OrElse e.Row.RowType <> DataControlRowType.Header Then
                        Exit Sub
                    End If
    
                    Dim grid As SPGridView = TryCast(sender, SPGridView)
    
                    If [String].IsNullOrEmpty(grid.FilterFieldName) Then
                    Return
                    End If
    
                    ' Show icon on filtered column 
                    For i As Integer = 0 To grid.Columns.Count - 1
                        Dim field As DataControlField = grid.Columns(i)
    
                        If field.SortExpression = grid.FilterFieldName Then
                            Dim filterIcon As New Image()
                            filterIcon.ImageUrl = "/_layouts/images/filter.gif"
                            filterIcon.Style(HtmlTextWriterStyle.MarginLeft) = "2px"
    
                            ' If we simply add the image to the header cell it will 
                            ' be placed in front of the title, which is not how it 
                            ' looks in standard SharePoint. We fix this by the code 
                            ' below. 
                            Dim headerText As New Literal()
                            headerText.Text = field.HeaderText
    
                            Dim panel As New PlaceHolder()
                            panel.Controls.Add(headerText)
                            panel.Controls.Add(filterIcon)
    
                            e.Row.Cells(i).Controls(0).Controls.Add(panel)
    
                            Exit For
                        End If
                    Next
            End Sub
    
            Private Sub gridDS_Filtering(ByVal sender As Object, ByVal e As ObjectDataSourceFilteringEventArgs)
                ViewState("FilterExpression") = DirectCast(sender, ObjectDataSourceView).FilterExpression
            End Sub
    
            Private Sub grid_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
                If ViewState("FilterExpression") IsNot Nothing Then
                    gridDS.FilterExpression = DirectCast(ViewState("FilterExpression"), String)
                End If
            End Sub
    
    
            Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
    
                If True Then
                    ViewState("FilterExpression") = gridDS.FilterExpression
    
                    MyBase.OnPreRender(e)
                End If
    
            End Sub
    
            Private Function GetFileSize(ByVal Bytes As Long) As String
                If Bytes >= 1073741824 Then
                    Dim size As [Decimal] = [Decimal].Divide(Bytes, 1073741824)
                    Return [String].Format("{0:##.##} gb", size)
                ElseIf Bytes >= 1048576 Then
                    Dim size As [Decimal] = [Decimal].Divide(Bytes, 1048576)
                    Return [String].Format("{0:##.##} mb", size)
                ElseIf Bytes >= 1024 Then
                    Dim size As [Decimal] = [Decimal].Divide(Bytes, 1024)
                    Return [String].Format("{0:##.##} kb", size)
                ElseIf Bytes > 0 And Bytes < 1024 Then
                    Dim size As [Decimal] = Bytes
                    Return [String].Format("{0:##.##} bytes", size)
                Else
                    Return "0 Bytes"
                End If
            End Function
    
        End Class
    
        Class NewTemplate
            Implements ITemplate
    
            Private _itemType As ListItemType
            Private _columnName As String
    
    
            Public Sub New(ByVal itemType As ListItemType, ByVal columnName As String)
    
                _itemType = itemType
                _columnName = columnName
    
            End Sub
    
    
            Public Sub InstantiateIn1(ByVal container As System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
    
                Select Case _itemType
    
                    Case ListItemType.Header
    
                        Dim header As New LiteralControl()
                        header.Text = String.Format("<b>{0}</b>", _columnName)
                        container.Controls.Add(header)
                        Exit Select
    
                    Case ListItemType.Item
    
                        Dim Labelitem As New Label()
                        Labelitem.ID = "DocTitle"
                        Labelitem.Visible = True
                        container.Controls.Add(Labelitem)
    
                        Dim filterIcon As New Image()
                        filterIcon.ID = "filterIcon"
                        container.Controls.Add(filterIcon)
    
                        Exit Select
                    Case Else
    
                        Exit Select
    
                End Select
            End Sub
    
        End Class
       
    
    Friday, February 26, 2010 12:43 PM
  • Hi, Tim.

    Thank you for posting the fix to your original quesiton.

    Did you find an answer to your Last Page button problem and 'Clear Filter' being grayed out?

     

    Thanks.

    Vron Jones

    Marvin Windows & Doors

    Wednesday, March 31, 2010 5:46 PM