Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't Call DataGrid SortCommand Event handler

Status
Not open for further replies.

xtremeLogic

Programmer
Dec 19, 2003
53
CA
Hi,

I am trying to implement sorting to my datagrid. I have set the
AllowSorting property to true and set the OnSortCommand Event handler to a
sub in my vb code file. When my page loads up, the column headers are
underline, but when I click on them, nothing happens. I tried changing the
name of my event handler but then the page gives an error when loading
saying it doesn't recognize the new name.

Any ideas, what is wrong as this is driving me crazy!!!

Thanks,
 
You'd have to post some code....make sure you gave each field a sort Expression....ah, what else...

dlc
 
xtreme: Here's a test aspx page with sorting in case there might be a detail here that could help (other non-relevant code deleted).

'********* DataGrid page w/sorting ************

<%@ Page Language=&quot;VB&quot; Debug=&quot;false&quot;%>
<%@Import Namespace = &quot;Microsoft.VisualBasic&quot;%>
<%@Import Namespace = &quot;System&quot;%>
<%@Import Namespace = &quot;System.Web&quot;%>
<%@Import Namespace = &quot;System.Web.UI&quot;%>
<%@Import Namespace = &quot;System.Web.UI.WebControls&quot;%>
<%@Import Namespace = &quot;System.Web.UI.HtmlControls&quot;%>
<%@Import Namespace = &quot;System.Data&quot;%>
<%@Import Namespace = &quot;System.Data.OleDb&quot;%>
<script runat=&quot;server&quot;>
Private Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
GetSite(&quot;mysortstring&quot;)
End If
End Sub

Sub GetSites (sortField As String)
'code to connect and bind datagrid data...
End Sub

Sub Sort_Grid(ByVal Sender As Object, ByVal e As DataGridSortCommandEventArgs)
Dim SortExprs() As String
Dim CurrentSearchMode As String, NewSearchMode As String
Dim ColumnToSort As String, NewSortExpr as String
'Parse the sort expression - delimiter space
SortExprs = Split(e.SortExpression, &quot; &quot;)
ColumnToSort = SortExprs(0)
'If a sort order is specified get it, else default is ascending
If SortExprs.Length() > 1 Then
CurrentSearchMode = SortExprs(1).ToUpper()
If CurrentSearchMode = &quot;ASC&quot; Then
NewSearchMode = &quot;DESC&quot;
Else
NewSearchMode = &quot;ASC&quot;
End If
Else 'If no mode specified, Default is descending
NewSearchMode = &quot;ASC&quot;
End If
'Derive the new sort expression.
NewSortExpr = ColumnToSort & &quot; &quot; & NewSearchMode
Dim iIndex As String
If ColumnToSort = &quot;AwwSiteCode&quot; Then
iIndex = 0
ElseIf ColumnToSort = &quot;Group_Name&quot; Then
iIndex = 1
ElseIf ColumnToSort = &quot;Waterbody_Name&quot; Then
iIndex = 2
ElseIf ColumnToSort = &quot;Description&quot; Then
iIndex = 3
ElseIf ColumnToSort = &quot;LastDate&quot; Then
iIndex = 7
ElseIf ColumnToSort = &quot;ChemCt&quot; Then
iIndex = 8
ElseIf ColumnToSort = &quot;BacCt&quot; Then
iIndex = 9
End If
lblA.Text = NewSortExpr
'alter the column's sort expression
dgGroups.Columns(iIndex).SortExpression = NewSortExpr
'Sort the data in new order
GetSites(NewSortExpr)
End Sub
</script>
<HTML>
<HEAD>
<title>Sites by County</title>
</HEAD>
<body background=&quot;images/lgtbrown.gif&quot;>
<form id=&quot;Form1&quot; runat=&quot;server&quot;>
<div style=&quot;height:500px;overflow:auto;width:850px;&quot;>
<asp:DataGrid
id=&quot;dgGroups&quot; runat=&quot;server&quot;
AutogenerateColumns=&quot;false&quot;
AllowSorting=&quot;True&quot;
OnSortCommand=&quot;Sort_Grid&quot;
BorderColor..
..
CellPadding=&quot;4&quot;>
<ItemStyle ForeColor=&quot;#330099&quot; BackColor=&quot;white&quot; Font-Size=&quot;9&quot; ></ItemStyle>
<HeaderStyle Font-Size=&quot;10&quot; horizontalalign=&quot;Center&quot; verticalalign=&quot;Middle&quot; ForeColor=&quot;#FFFFCC&quot; BackColor=&quot;#990000&quot;></HeaderStyle>
<Columns>
<asp:BoundColumn DataField=&quot;AWWSitecode&quot; HeaderText=&quot;Site Code&quot; SortExpression=&quot;AwwSiteCode&quot;/>
<asp:BoundColumn DataField=&quot;Group_Name&quot; HeaderText=&quot;Group Name&quot; Sortexpression=&quot;Group_Name&quot;/>
<asp:BoundColumn DataField=&quot;Waterbody_Name&quot; HeaderText=&quot;Waterbody Name&quot; Sortexpression=&quot;Waterbody_Name&quot;/>
<asp:BoundColumn DataField=&quot;Description&quot; HeaderText=&quot;Site Description&quot; ItemStyle-width=&quot;160px&quot; Sortexpression=&quot;Description&quot;/>
<asp:BoundColumn DataField=&quot;Latitude&quot; HeaderText=&quot;Latitude&quot; DataFormatString=&quot;{0:f4}&quot; />
<asp:BoundColumn DataField=&quot;Longitude&quot; HeaderText=&quot;Longitude&quot; DataFormatString=&quot;{0:f4}&quot; />
<asp:BoundColumn DataField=&quot;LastDate&quot; HeaderText=&quot;Last Date&quot; DataFormatString=&quot;{0:dd MMM yyyy}&quot; Sortexpression=&quot;LastDate&quot;/>
<asp:BoundColumn DataField=&quot;ChemCt&quot; HeaderText=&quot;#Chm&quot; Sortexpression=&quot;ChemCt&quot;/>
<asp:BoundColumn DataField=&quot;BacCt&quot; HeaderText=&quot;#Bac&quot; Sortexpression=&quot;BacCt&quot;/>
<asp:BoundColumn DataField=&quot;BioCt&quot; HeaderText=&quot;#Bio&quot; Sortexpression=&quot;BioCt&quot;/> </Columns>
</asp:DataGrid>
</DIV>
</form>
</body>
</HTML>
 
Hey thanks for the insight guys, I somehow got it to work doing basic sorting. Another problem that I am having is that it only sorts for the current page in the datagrid. Once I click on the next page, the sort goes away. Any ideas on how to make it consistent? Thanks for the help!
Here's some sample code:
DatGrid
Code:
<asp:DataGrid id=&quot;DataGrid1&quot; Font-Names=&quot;Arial, Helvetica, Verdana, sans-serif&quot; Font-Name=&quot;Arial, Helvetica, Verdana, sans-serif&quot; Font-Size=&quot;10pt&quot; Width=&quot;100%&quot; Runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; AlternatingItemStyle-BackColor=&quot;#EEECD9&quot; AllowPaging=&quot;True&quot; AllowSorting=&quot;True&quot; OnPageIndexChanged=&quot;dgGetDocs_Paged&quot; OnSortCommand=&quot;DataGrid1_SortCommand&quot;>
<AlternatingItemStyle BackColor=&quot;#EEECD9&quot;></AlternatingItemStyle>
<Columns>

<asp:HyperLinkColumn Target=&quot;_blank&quot; DataNavigateUrlField=&quot;FullFilePath&quot; DataTextField=&quot;FileName&quot; SortExpression=&quot;1&quot; HeaderText=&quot; Document #&quot;>
<HeaderStyle Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; BorderWidth=&quot;1px&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Solid&quot; BorderColor=&quot;White&quot; BackColor=&quot;#2F5594&quot;></HeaderStyle>
</asp:HyperLinkColumn>

<asp:BoundColumn DataField=&quot;DocSize&quot; SortExpression=&quot;2&quot; HeaderText=&quot; Size (kb)&quot;>
<HeaderStyle Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; BorderWidth=&quot;1px&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Solid&quot; BorderColor=&quot;White&quot; BackColor=&quot;#2F5594&quot;></HeaderStyle>
</asp:BoundColumn>
					
<asp:BoundColumn DataField=&quot;DocDate&quot; SortExpression=&quot;3&quot; HeaderText=&quot; Date Created&quot; DataFormatString=&quot;{0:dd-MMM-yyyy}&quot;>
<HeaderStyle Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; BorderWidth=&quot;1px&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Solid&quot; BorderColor=&quot;White&quot; BackColor=&quot;#2F5594&quot;></HeaderStyle>
</asp:BoundColumn>

<asp:BoundColumn DataField=&quot;DocType&quot; SortExpression=&quot;4&quot; HeaderText=&quot; Document Type&quot;>
<HeaderStyle Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; BorderWidth=&quot;1px&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Solid&quot; BorderColor=&quot;White&quot; BackColor=&quot;#2F5594&quot;></HeaderStyle>
</asp:BoundColumn>

<asp:HyperLinkColumn Text=&quot;Edit&quot; Target=&quot;topFrame&quot; DataNavigateUrlField=&quot;pk_docroomID&quot; DataNavigateUrlFormatString=&quot;form.aspx?edit=true&fileID={0}&quot;>
<HeaderStyle Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; BorderWidth=&quot;1px&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Solid&quot; BackColor=&quot;#2F5594&quot;></HeaderStyle>
</asp:HyperLinkColumn>

<asp:ButtonColumn Text=&quot;Delete&quot; CommandName=&quot;Delete&quot;>
<HeaderStyle Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; BorderWidth=&quot;1px&quot; ForeColor=&quot;White&quot; BorderStyle=&quot;Solid&quot; BackColor=&quot;#2F5594&quot;></HeaderStyle>
</asp:ButtonColumn>
</Columns>

<PagerStyle NextPageText=&quot;   Next >>&quot; Font-Size=&quot;10pt&quot; Font-Names=&quot;Arial&quot; Font-Bold=&quot;True&quot; PrevPageText=&quot;<< Prev   &quot; HorizontalAlign=&quot;Right&quot; ForeColor=&quot;#2F5594&quot;></PagerStyle>
</asp:DataGrid>

VB Code
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     If Not Page.IsPostBack Then
          'Load DataGrid
           BindData(0)
     End If
End Sub

'Sort Data Event Handler
Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
     BindData(e.SortExpression)
End Sub

' Bind DataGrid DataSource
Sub BindData(ByVal sortExpr As Integer)
     Dim Doclist As New DocRoomMain()
     'Set datagrid's datasource to the dataset & databind
     DataGrid1.DataSource = Doclist.newGetDocuments(Session(&quot;projectID&quot;), sortExpr).DefaultView
     DataGrid1.DataBind()

     'Display info on what page we are currently viewing
     lblMessage.Text = &quot;Showing Page &quot; &  DataGrid1.CurrentPageIndex + 1 & _
     &quot; of &quot; & DataGrid1.PageCount
End Sub
 
Exclude BindData(0) from If Not Page.IsPostBack and store the sort expression into a Session.
Something like:
Code:
Sub Page_Load
 If Session.Item(&quot;order&quot;) = Nothing Then
  Session.Item(&quot;order&quot;) = default_sort
 End If
End Sub

Sub DataGrid1_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand
 Session.Item(&quot;order&quot;)=e.SortExpression
 BindData(Session.Item(&quot;order&quot;))
End Sub
 
Hmm ... better :
Code:
Sub Page_Load
 If Session.Item(&quot;order&quot;) = Nothing Then
  Session.Item(&quot;order&quot;) = default_sort
 End If
 BindData(Session.Item(&quot;order&quot;))
End Sub
I'm a little tired and sorry about this second post
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top