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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting a Paged Datagrid - help please! 2

Status
Not open for further replies.

barleywine

Programmer
Jan 11, 2003
7
GB
I want to sort a datagrid which has 5 columns by either of the first three. I am already paging the datagrid. I don't need to update the underlying recordset, just display sorted tables on the web page.

The Column names I want to sort on are: Species, Contact and Organisation.

My code behind looks like this:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SqlDataAdapter1.Fill(DsLPSppContact1)
DataGrid1.DataBind()
End If
End Sub

'Paging
Private Sub DataGrid1_PageIndexChanged(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
DataGrid1.CurrentPageIndex = e.NewPageIndex
SqlDataAdapter1.Fill(DsLPSppContact1)
DataGrid1.DataBind()
End Sub

'Sorting
Private Sub DataGrid1_SortCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) _
Handles DataGrid1.SortCommand
Dim dt As New DataTable()
Dim dataview1 As New DataView(dt)
dataview1.Sort = e.SortExpression
DataGrid1.DataBind()
End Sub

But if I try to sort on, for example "Contact" I keep getting the error: System.IndexOutOfRangeException: Cannot find column Contact

Please can anyone help?

Cheers

Barleywine
 
Hi there!
Here's a solution that I'm currently using:
Code:
<asp:DataGrid ID="dgTest" Runat="server" AllowPaging="True" PageSize="15" AllowSorting="True" 
 AutoGenerateColumns="False" CellPadding="2" CellSpacing="2">
 <PagerStyle Font-Size="8pt" HorizontalAlign="Center" Mode="NumericPages" />
 <Columns>
  <asp:BoundColumn HeaderText="Column 1" DataField="userid" SortExpression="userid" />
  <asp:BoundColumn HeaderText="Column 2" DataField="username" SortExpression="username" />
  ...
 </Columns>
</asp:DataGrid>

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 If Not IsPostBack() Then
  If Session.Item("order") = Nothing Then
   Session.Item("order") = "userid ASC"
  End If
  make_dg()
 End If
End Sub

Sub make_dg()
 Dim objCon As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("ConnString"))
 Dim objCmd As SqlCommand = New SqlCommand("your sql query", objCon) 'I'm using a stored procedure here
 Try
  objCon.Open()
  Dim daSQL As SqlDataAdapter = New SqlDataAdapter(objCmd)
  Dim dsTest As DataSet = New DataSet
  daSQL.Fill(dsTest)
  dsTest.Tables(0).DefaultView.Sort = Session.Item("order")
  dgTest.DataSource = dsTest.Tables(0).DefaultView
  If Not dsTest.Tables(0).Rows.Count = 0 Then
   If dsTest.Tables(0).Rows.Count <= 15 Then
    dgTest.AllowPaging = False
   Else
    dgTest.AllowPaging = True
  End If
  dgTest.DataBind()
 Finally
  objCon.Close()
  objCon = Nothing
 End Try
End Sub

Private Sub dgTest_Paginate(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs) Handles dgTest.PageIndexChanged
 dgTest.CurrentPageIndex = e.NewPageIndex
 make_dg()
End Sub

Private Sub dgTest_Sort(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles dgTest.SortCommand
 Dim x = e.SortExpression
 If Session.Item("order") = (x & " ASC") Then
  Session.Item("order") = x & " DESC"
 Else
  Session.Item("order") = x & " ASC"
 End If
 make_dg()
End Sub

I hope this helps!

[morning]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top