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

Sorting Datagrid without going back to database

Status
Not open for further replies.

babsjoy

Programmer
Sep 1, 2003
46
0
0
US
I am new to VB.NET. I am working with VS 2003.
My issue is that I want to sort the current binded datagrid without going back to the database to obtain the records based on the new sort criteria selected by the user.
Upon intially loading the datagrid was binded using a collection.
Public Class NewCompCollection
Inherits ArrayList
End Class

I have been researching and all documentation refers to datasets or dataviews or going back to the database to resort the data. Can I re-sort the datagrid as I am trying to do below or is more complicated since a collection was used to bind the datagrid. See code below.

ON PAGE LOAD:

Private Function getRecords() As Boolean
Dim NewCollection As NewCompCollection
Dim NewCompRec As New NewCompClass
Dim i As Integer

Try
' get records from Oracle db
NewCollection = NewCompClass.GetRecords()

dgTravel.DataSource = NewCollection
dgTravel.DataBind()
Session("NewCompCol") = NewCollection

Catch ex As Exception
Throw ex
End Try
End Function

HTML DATAGRID CONTAINS:

OnSortCommand="dgTravel_SortCommand"

VB PROCEDURE:

Public Sub dgTravel_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgTravel.SortCommand

' Extract current sort column and order.
Dim currSortExpr As String = dgTravel.Attributes("SortExpr")
Dim newSortExpr As String = e.SortExpression

' If the sort expression is the same as the current 'one,just reverse the direction.

If Not (currSortExpr Is Nothing) AndAlso currSortExpr.ToString = _
e.SortExpression Then
newSortExpr &= " DESC"
End If

dgTravel.Attributes("SortExpr") = newSortExpr
dgTravel.DataSource = ???

dgTravel.DataBind()
End Sub

Thanks for your help in advance !!!!


 
I think the issue is not so much that you're using a collection. Rather it's that you're using a nonstandard class to hold the records.

What's NewCompClass and what's the GetRecords method? What other functionality does this class have? And why are you using these instead of, say, a datareader class, going along lines like those described here?
 
Thanks for responding. The NewCompClass sets up private variables and public properties as well as performing all database functions such as getting (GetRecords below), inserting, updating and deleting records from the Oracle database.

Public Shared Function GetRecords() As NewCollection
Dim conn As OracleConnection
Dim myCommand As New OracleCommand
Dim cr As OracleDataReader
Dim NewCompCol As New NewCollection

Try
conn = Connection()
myCommand.Connection = conn
myCommand.CommandType CommandType.StoredProcedure
myCommand.CommandText = "DATABASE.MISTPK520_COMP.SELECT_RECORDS"
myCommand.Parameters.Add(New OracleParameter("select_cursor", OracleDbType.RefCursor, ParameterDirection.Output))
cr = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

While cr.Read()
Dim NewCompRec As New NewCompClass
NewCompRec.claimFolderNum =
cr("CLAIM_FOLDER_NUM").ToString
NewCompRec.claimCount = cr("CLAIM_COUNT").ToString
NewCompRec.employeeName = cr("EMPLOYEE_NAME").ToString
NewCompRec.shop = cr("SHOP").ToString
travelCompCol.Add(NewCompRec)
End While

Return NewCompCol

End Function

I am doing this since I am adding a page to an existing solution. Other pages were developed in this manner. I wanted to stay consistent. During my research I did not run into the article associated with your link above. I think there is alot I can learn from this.
 
<I think there is alot I can learn from this
Can't we all! :)

I didn't mean to suggest that you were doing it wrong. Rather, that you probably need to look at this "NewCompClass" class as well, since the difficulty with sorting could just as easily be occurring there.
 
I didn't think that at all .... [smile] Just wanted to let you know where I was coming from. I know that I could create a different function in the NewCompClass that would retrieve data from the database based on the sorted column selected by the user but I would rather not if thats what you were suggesting.

Thanks Again !
 
Nothing that specific. Rather that the class probably is the place you're going to solve your problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top