Dynamic Bound Columns OnSortCommand not called (Solution)

C

Chad Devine

Alright, I am posting my solution to a problem I've been having. I have
finally found the answer, and it took me a while so I thought I'd
provide the final solution here, since it may be benificial to others
and because I had to search for the solution (not the problem) in order
to find a working one.

I had a datagrid that wasn't sorting, the onsortcommand seemed as
though it wasn't being called, and I had dynamically bound columns in
the page_load sub. That was the big mistake.

Apparently, in order for the sortcommand to work correctly, you need to
bound your columns in a different sub, the page_init event sub. Once
you successfully do this, all is well. After doing a few very specific
searches I found this answer (in a rather vague form) on usenet.

The page_init sub runs before the page_load sub is run, and is only run
once when the page is first loaded, not on every page load.

------------
The code should look like:
Sub Page_Init(Sender As Object, E As EventArgs)
'Don't forget to open a sql connection here, and fill the dataset.

objDataTable = objDataSet.Tables(0)
For i = 0 To objDataTable.Columns.Count - 1
objCol = New BoundColumn()
objCol.HeaderText = objDataTable.Columns(i).ColumnName
objCol.DataField = objDataTable.Columns(i).ColumnName
objCol.SortExpression = objDataTable.Columns(i).ColumnName
DataGrid1.Columns.Add(objCol)
Next

End Sub

Some settings in the datagrid:
AutoGenerateColumns="False"
OnSortCommand="SQLDataGrid_SortCommand"
AllowSorting="True"
 
G

Guest

Or you could do this:

private page_load()
{
if ( !Page.IsPostBack)
{
//Build Dynamic columns here
}
}

You are only supposed to build (bind) your columns once, not everytime the
page_load event is called.

ENJOY !
 
Joined
Feb 6, 2008
Messages
1
Reaction score
0
little late

I know this forum has had no activity, but I found it while searching for a solution to the same problem the original poster had. I also followed everything to make my sortcommand event work. Every time i would try to sort, the datagrid would disappear. When using the debugger, it was clear that the sorting event was never called.

After setting up the grid in the OnInit, everything was fine. I am not sure if it was an ordering issue of when events are registered or what, but this solution worked for me. Thank you.

=?Utf-8?B?RGlyazQx?= : I was checking if the page was a post back. When you clicked to sort, the page would always call Page_Load and determine it was a post back, then do nothing (I had no code otherwise).

So, if all else fails, give this solution a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top