Last post Aug 16, 2007 07:09 AM by Techie Zhang - MSFT
Aug 14, 2007 08:22 PM|thehaunt|LINK
Hi there, I'm having a problem where once I sort on a column in a gridview, when I try to reload the gridview (databind) it tries to sort the columns based on the most recent sort performed.
So For example I sort on UserId then later databind the grid to another table (say PageTbl) and when it tries to do the databind it tells me it can't find column "userId" to sort on.
I can't quite figure this out, should the sortexpression be cleared when I clear the columns of the gridview (line 6) ?
Here's the relate code...
Protected Sub LoadTableData()
Dim Cols As New DBConnection(cMaster, False)
Dim SQL As String
Dim resultCnt As Integer
sdsResults.SelectCommand = "SELECT "
SQL = "SELECT COLUMN_NAME FROM " & ddlDB.SelectedValue & ".INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" & ddlTbl.SelectedValue & "'"
addHeaderItem(gvResults, Cols.Reader(0), Cols.Reader(0), "ViewDB.aspx")
sdsResults.SelectCommand &= " [" & Cols.Reader(0) & "],"
SQL = "SELECT COUNT(1) FROM " & ddlDB.SelectedValue & "..[" & ddlTbl.SelectedValue & "]"
resultCnt = Cols.ExecuteScalar(SQL)
sdsResults.SelectCommand = sdsResults.SelectCommand.TrimEnd(",")
sdsResults.SelectCommand &= " FROM " & ddlDB.SelectedValue & "..[" & ddlTbl.SelectedValue & "]"
ResultsPanel.Visible = True
lblResultCount.Text = resultCnt & " Results"
Line 18 produces an out of range exception telling me that it can't find the column from the previous sort.
Basically I'd like to know how to tell it NOT to sort when it does the databind.
Aug 14, 2007 11:00 PM|d4dennis@inspir3|LINK
Columns.Clear() will not clear the sort expression.
Try following: (not tested)
Hope it helps!
reset sort expression
Aug 15, 2007 12:34 PM|thehaunt|LINK
A good idea, but it doesn't work for me, I started getting timeout errors when I added the sort("", SortDirection.Ascending). I need to keep the gvResults.Columns.Clear() line in there to wipe out old column data. Thanks for the thought!
Aug 15, 2007 11:02 PM|d4dennis@inspir3|LINK
How do you sort your GridView, could you show me your Sorting bit so that it give more rough idea where to start.
Aug 16, 2007 07:09 AM|Techie Zhang - MSFT|LINK
Please try putting the statement gvResults.Sort("",
SortDirection.Ascending); before you clear the previous columns. That is gvResults.Columns.Clear()
It should work.