[英]gridview sorting based on columns
嗨,我有一個網格視圖,在此我顯示了幾列,分別是名稱,電話,信息和日期。 我的要求是,如果我單擊columns標題,則應該對其進行排序,我的aspx代碼在這里...
<asp:GridView ID="sorttest" runat="server" AllowSorting="true">
<Columns>
<asp:BoundField DataField="ID" HeaderText="Sno." Visible="False" />
<asp:BoundField DataField="Name" HeaderStyle-CssClass="tdHead3" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Phone" HeaderStyle-CssClass="tdHead3" HeaderText="Phone Number" SortExpression="Phone"/>
<asp:BoundField DataField="Information" HeaderStyle-CssClass="tdHead3" HeaderText="Information Of The Offender" SortExpression="Information"/>
<asp:BoundField DataField="Date" HeaderStyle-CssClass="tdHead3" HeaderText="Date of Graffiti" SortExpression="Date"/>
</Columns>
</asp:GridView>
我的代碼是:
Public Sub FillGrid()
Try
myconnection = New SqlConnection(conntection string)
cmd = New SqlCommand()
cmd.CommandText = "Retrievedetails"
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = myconnection
ds = New DataSet()
da = New SqlDataAdapter(cmd)
da.Fill(ds, "details")
myconnection.Open()
sorttest.DataSource = ds
sorttest.DataBind()
Catch ex As Exception
Finally
myconnection.Close()
End Try
End Sub
為此,如果我單擊列標題,則如果我下次單擊它應該首先按升序排序,然后應該按降序排序,這應該適用於網格視圖中列出的所有列...
請幫助我為此編碼...
您需要處理OnSorting
事件,這是我的處理方式:
ASPX:
<asp:GridView ID="sorttest" runat="server" AllowSorting="true"
OnSorting="SortGrid">
代碼隱藏:
//sorry I'm putting my CS code, as translating it to VB will be
//very tedious for me
protected void SortGrid(object sender, GridViewSortEventArgs e)
{
ViewState["SortDirection"] =
(e.SortDirection == SortDirection.Ascending) ? "asc": "desc";
ViewState["SortExp"] = e.SortExpression;
FillGrid();//CALL FILL GRID
}
現在在FillGrid
您需要基於以下值對要獲取的表(詳細信息)進行排序:
'Here sort the table (details) based on values stored in View State
'Sorry My VB skills are really rusty now :)
var dv as DataView = ds.Tables("details").DefaultView;
if not (ViewState("SortExp") is nothing) then 'check for null
if ViewState("SortDirection").ToString() == "asc" then
dv.Sort = ViewState("SortExp") & " " & "asc"
else
dv.Sort = ViewState("SortExp") & " " & "desc"
end if
end if
sorttest.DataSource = dv 'bind to dataview
sorttest.DataBind()
這是vb.net中的代碼。 您需要處理GridView的Sorting事件,還需要維護先前排序的視圖狀態。
Private Property PreviousSortField() As String
Get
If ViewState("SortField") Is Nothing Then
Return ""
Else
Return ViewState("SortField")
End If
End Get
Set(ByVal value As String)
ViewState("SortField") = value
End Set
End Property
Private Property PreviousSortDirection() As String
Get
If ViewState("SortDirection") Is Nothing Then
Return ""
Else
Return ViewState("SortDirection")
End If
End Get
Set(ByVal value As String)
ViewState("SortDirection") = value
End Set
End Property
Protected Sub sorttest_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles sorttest.Sorting
Dim SortDirection As String
If e.SortDirection = WebControls.SortDirection.Ascending Then
SortDirection = "DESC"
ElseIf e.SortDirection = WebControls.SortDirection.Descending Then
SortDirection = "ASC"
Else
SortDirection = "ASC"
End If
Dim ds As DataSet = sender.Datasource
Dim Data As DataTable = ds.Tables(0)
If Not Data Is Nothing Then
Dim dv As DataView = New DataView(Data)
If e.SortExpression = PreviousSortField And SortDirection = PreviousSortDirection Then
If SortDirection.ToUpper = "ASC" Then
SortDirection = "DESC"
ElseIf SortDirection.ToUpper = "DESC" Then
SortDirection = "ASC"
Else
SortDirection = "ASC"
End If
End If
dv.Sort = e.SortExpression & " " & SortDirection
sender.DataSource = dv
sender.DataBind()
End If
'Need to store sort info in view state
PreviousSortField = e.SortExpression
PreviousSortDirection = SortDirection
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.