Dynamic columns and sorting/paging in DataGrid

J

javatopia

I am creating a data grid's columns on the fly in my web page. The Bind
operation works just fine, and data is displayed correctly when I change the
layout of the grid (add/remove columns).

I change the data grid by first removing all of the columns then adding each
of the columns that I need for a particular report.

My data grid has a "sort" handler:

this.Report.SortCommand += new
System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.Report_SortCommand);

which is set in the InitializeComponent() method of my WebForm. I setup the
column data model in my BindData() method of the form, which occurs AFTER the
initialization (of course).

This is my Report_SortCommand:

private void Report_SortCommand(object source,
System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
string expr = e.SortExpression;
string oldexpr = (string)ViewState["DataSortExpression"];
if(oldexpr == expr)
{
ViewState["DataSortExpression"] = expr + " DESC";
}
else
{
ViewState["DataSortExpression"] = expr;
}
BindData(); // -> This creates the re-creates the columns in the
grid
}

So I put a breakpoint in the sort method and click on one of the column
titles. The data grid goes blank and my breakpoint never gets hit.

SOOO - when I remove the columns from a datagrid, does the sort event
delegate get cleared ?? How do I get the sort event to call my custom sort
handler?

Thanks

-- Jake
 

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,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top