没有ViewState的GridView排序 [英] GridView Sorting without ViewState

查看:93
本文介绍了没有ViewState的GridView排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为从DataTable填充的GridView实现排序功能。此外,我有一个过滤器/搜索功能,所以每次显示过滤器/搜索结果时我都不能使用ViewState,我尝试对它们进行排序,排序显示所有记录然后对它们进行排序。



这是我发现的唯一不使用ViewState的代码,但它不起作用。每当我点击专栏听筒时都没有任何反应。只是我的消息'空'弹出窗口。

I’m trying to implement a Sort feature for my GridView that is being populated from a DataTable. Also I have a Filter/Search feature so I can’t use ViewState cause every time the Filter /Search results are displayed and I try to Sort them the Sort displays all the records then Sorts them.

This is the only code that I found that doesn't use a ViewState but, it doesn't work. Whenever I click on the column hearder nothing happens. Just my message 'Empty' pops-ups.

Private Function ConvertSortDirectionToSql(ByVal sortDirection As SortDirection) As String
        Dim newSortDirection As String = String.Empty

        Select Case SortDirection
            Case SortDirection.Ascending
                newSortDirection = "ASC"

            Case SortDirection.Descending
                newSortDirection = "DESC"
        End Select

        Return newSortDirection
    End Function

    Protected Sub grdNurseVisits_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Handles grdNurseVisits.Sorting

        Dim dtable As DataTable = TryCast(grdNurseVisits.DataSource, DataTable)

        If Not dtable Is Nothing Then
            MsgBox("Not Empty")
            Dim dataView As DataView = New DataView(dtable)
            dataView.Sort = e.SortExpression & " " & ConvertSortDirectionToSql(e.SortDirection)

            grdNurseVisits.DataSource = dataView
            grdNurseVisits.DataBind()
        ElseIf dtable Is Nothing Then
            MsgBox("Empty")
        End If
    End Sub

推荐答案

检查这些

使用ViewState关闭GridView排序 - 双重查询的解决方案和为什么我的排序事件处理程序不会触发? [ ^ ]

GridView排序,不使用会话,ViewState或缓存进行分页 [ ^ ]
Check these
GridView sorting with ViewState off - a solution to double querying and "why doesn't my Sorting event handler fire?"[^]
GridView Sorting, Paging without using Session, ViewState or Cache[^]


Is这是一个Windows或Web应用程序? DataGridView是Windows,但你在谈论ViewState,所以我假设你有一个带有GridView的web应用程序。假设是这种情况,每次都会收到空消息,因为网格的数据源在回发时没有任何内容(这是正常的)。如果要访问数据源,请在绑定数据源时将其添加到ViewState,然后在排序处理程序中将其提取出来。这也将解决您的主要问题:当您应用排序时,您正在重新绑定,因此您需要保留并重新应用任何存在的过滤器。这样的事情:



Is this a Windows or web app? DataGridView is Windows, but you're talking about ViewState, so I assume you've got a web app with a GridView. Assuming that's the case, you're getting your "Empty" message every time because your grid's data source is nothing on postback (which is normal). If you want access to the data source, add it to ViewState at the same time you bind it, and then fetch it out here in the sort handler. And this will also address your primary question: when you apply your sort, you are rebinding, so you need to preserve and re-apply any filter that is present as well. Something like this:

Using dv As New DataView(ViewState(MY_DATA))
    dv.Sort = e.SortExpression & " " & ConvertSortDirectionToSql(e.SortDirection)
    ViewState(MY_SORT) = dv.Sort
    If ViewState(MY_FILTER) IsNot Nothing Then dv.RowFilter = ViewState(MY_FILTER) 'preserve current filter
    grdNurseVisits.DataSource = dv
    grdNurseVisits.DataBind()
End Using


这篇关于没有ViewState的GridView排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆