台灣最大程式設計社群網站
線上人數
1428
 
會員總數:243311
討論主題:188091
歡迎您免費加入會員
討論區列表 >> VB.NET >> gridveiw 排序加上下圖示
[]  
[我要回覆]
1
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
gridveiw 排序加上下圖示
價值 : 100 QP  點閱數:577 回應數:3

樓主

MStone Lin
門外漢
0 63
1380 91
發送站內信


請問
gridview加上排序圖示
一直無法顯示
是少了設定什麼??

test.aspx
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" >
<Columns>
<asp:BoundField DataField="主辦單位" HeaderText="主辦單位" SortExpression="主辦單位" />
<asp:BoundField DataField="類別" HeaderText="類別" SortExpression="類別" />
.....
</Columns>

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:WebDataConnectionString %>"
></asp:SqlDataSource>


test.aspx.vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

If Not IsPostBack Then
ViewState("SortExpression") = "收文日" '設定預設的排序欄位
ViewState("SortDirection") = SortDirection.Ascending ' 設定預設的排序方式
End If
dbview()
End Sub

Sub dbview()

Dim sqlstr As String = "select a.primarydep as 主辦單位,a.docgrp as 類別,....."

SqlDataSource1.SelectCommand = sqlstr

Dim dv As DataView = SqlDataSource1.Select(New DataSourceSelectArguments())

dv.Sort = String.Format("{0} {1}", ViewState("SortExpression").ToString(), If(DirectCast(ViewState("SortDirection"), SortDirection) = SortDirection.Ascending, "ASC", "DESC"))
GridView1.DataSource = dv
'GridView
GridView1.DataBind()

End Sub

Protected Sub GridView1_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PageIndexChanged
dbview()
End Sub

Protected Sub GridView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
GridView1.PageIndex = e.NewPageIndex
End Sub

Private Sub GridView1_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.PreRender
dbview()
MyBase.OnPreRender(e)
End Sub

Protected Sub GridView1_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles GridView1.Sorting
ViewState("SortExpression") = e.SortExpression
If ViewState("SortExpression").ToString() = e.SortExpression Then
ViewState("SortDirection") = If(DirectCast(ViewState("SortDirection"), SortDirection) = SortDirection.Ascending, SortDirection.Descending, SortDirection.Ascending)
Else
ViewState("SortDirection") = SortDirection.Ascending
End If
End Sub


Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.Header Then
For Each field As DataControlField In GridView1.Columns
If GridView1.SortExpression <> "" And field.SortExpression = GridView1.SortExpression Then
If GridView1.SortDirection = SortDirection.Ascending Then
e.Row.Cells(GridView1.Columns.IndexOf(field)).Controls.Add(New LiteralControl(" ▲"))
Else
e.Row.Cells(GridView1.Columns.IndexOf(field)).Controls.Add(New LiteralControl(" ▼"))
End If
End If
Next
End If

End Sub

我是參考這篇
https://dotblogs.com.tw/621122/2013/09/02/116139
但其中並於header的cells中加入literal或img的控制項 不懂....
且gridview1.sortexpression 一直是空值

請問那邊少加了什麼嗎
謝謝



搜尋相關Tags的文章: [ gridvew ] , [ 排序 ] , [ 圖示 ] ,
本篇文章發表於2016-05-02 16:12
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
1樓
回應

彩虹
檢舉此回應
試試看這個

在 GridView 中對欄位進行排序時顯示上下箭頭
https://dotblogs.com.tw/johnny/2013/12/23/135675


另外,往後請您貼程式碼的時候
多多利用程式碼標

不然我們會看很眼花,無法更直覺幫你看錯誤在哪
本篇文章回覆於2016-05-03 10:02
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
2樓
作者回應

MStone Lin
檢舉此回應
回彩虹

我的主機還未升級至asp4.0
還使用asp2.0(win2000+iis6)
...

本篇文章回覆於2016-05-03 15:44
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
3樓
回應

bear
檢舉此回應
Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.Header Then
For Each field As DataControlField In GridView1.Columns
If ViewState("SortExpression") <> "" And field.SortExpression = ViewState("SortExpression") Then
If ViewState("SortDirection") = SortDirection.Ascending Then
e.Row.Cells(GridView1.Columns.IndexOf(field)).Controls.Add(New LiteralControl(" ▲"))
Else
e.Row.Cells(GridView1.Columns.IndexOf(field)).Controls.Add(New LiteralControl(" ▼"))
End If
End If
Next
End If

End Sub
本篇文章回覆於2018-08-03 17:11
== 簽名檔 ==
--未登入的會員無法查看對方簽名檔--
   
1

回覆
如要回應,請先登入.