帮助! GridView在运行时排序 [英] HELP! GridView Sorting at Runtime

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

问题描述

大家好,

我有一个在运行时绑定数据的网格...

Hi Guys,

I have a grid that binds the data at runtime...

Dim myCommand = New SqlCommand(strSql, myConnection)
Dim dr As SqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

gvLiveCourses.Visible = True
gvLiveCourses.DataSourceID = ""
gvLiveCourses.DataSource = dr
gvLiveCourses.DataBind()



接下来是OnSort事件和函数.实际上,我只是从其他论坛复制了它.



Next is the OnSort event and function. Actually I just copied it from other forums.

Sub gvLiveCourses_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) 'Handles gvLiveCourses.Sorting
    Dim m_DataTable As DataTable = TryCast(gvLiveCourses.DataSource, DataTable)
    If m_DataTable IsNot Nothing Then
        Dim m_DataView As New DataView(m_DataTable)
        m_DataView.Sort = e.SortExpression + " " & ConvertSortDirectionToSql(e.SortDirection)
        gvLiveCourses.DataSource = m_DataView
        gvLiveCourses.DataBind()
    End If
End Sub
Function ConvertSortDirectionToSql(ByVal sortDirection As SortDirection)
    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







现在,当我单击gridview列时,它不会排序.当我使用断点对其进行跟踪时,我发现m_DataTable为空.但是,由于网格显示数据,所以这变成空的.我不明白.

请让我知道如何更正此代码.我在大多数应用程序中都使用sqldatareader,并且需要在每个gridview上添加排序功能.在gridview中没有问题,只需要添加排序功能.

我希望您能给我一个有关使用sqldatareader对gridview进行排序的示例,该示例在运行时绑定数据.


谢谢

Hifiger







Now, when I click on the gridview columns it doesn''t sort. And when I traced it using breakpoints, I found out that the m_DataTable is empty. But how come this becomes empty since the grid displays the data. I don''t get it.

Please let me know on how to correct this code. I am using sqldatareader most of my applications, and I need to add sorting feature on every gridview. There''s no problem in the gridview, only the sort feature that has to be added.

I hope you could give me a sample on sorting the gridview using sqldatareader that binds the data at runtime.


Thanks

Hifiger

推荐答案



这听起来像是回发问题.您必须确保
您的数据会以某种方式回发.最简单的方法
以便在每次回发时重新绑定您的数据源.但这可能不是
选项.还有其他可能性.

通过查询"gridview回发数据源排序"或类似方法找到它们.关于该主题的讨论很多.

祝你好运
Hi,

this sounds like a postback-issue. You have to make sure that
your data is retrieved during a postback somehow. Easiest way would
be to rebind your datasource on every postback. But that may not be an
option. There are other possibilities.

Find them by binging for "gridview postback datasource sorting" or similiar. There are lots of discussions about that topic out there.

Good luck



GridView始终以HTML表格格式呈现(输出).
GridView只是用于容纳行和列的容器.
您在网页上查看的数据是呈现的数据表,而不是GridView或Datatable.


现在,您要在数据仪式上应用排序.因此,您为此需要数据,将数据表从数据库表中重新绑定,或者将数据表存储在viewstate或会话中,然后进行检索,再次进行排序,将其绑定到GridView DataSource.

GridView always render (output) as html table format.
GridView is just an container for hoding rows and columns.
The data you viewing on web page is the rendered data table not the GridView or Datatable.


Now where you want to apply sorting, on data rite. So you need data for that, rebind the datatable from DB table or store your datatable in viewstate or in session then retrive, do sorting again bind to GridView DataSource.


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

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