Setting SortExpression programmatically

G

Guest

Hi!

When I add a bound column programatically, like:

BoundColumn bc = new BoundColumn();
bc.DataField = "myField";
bc.SortExpression = "myField";
myGrid.Columns.Add(bc);

the SortCommand event does not trig at all!

If I replace the code whith the markup syntax in the ascx file, it works
fine. I've google around but the only solution to the problem seemes to be
moving the code to Page_Init.

This is not possible because at that time I dont know the structure of the
table. Depending on a selection in a dropdown, i get diffrent datatables to
bind with the grid. The selectedindexchanged event on the dropdown will not
fire until much later in execution, so in Page_Init I wouldn't know what was
selected yet. Furthermore i cant understand what Page_Init has to do with all
of this.

I'm confused, any ideas?

Thanks,
Paul
 
K

Kalpesh

Hi,

What is the order in which controls are placed ?
If I understand this correctly, you have a custom control (similar to
datagrid) & a dropdown.
The dropdown has list of fields that you allow the user to sort on.
Depending upon user selection, you wish to sort the grid on the
selected column & show it to user

Correct ?
If yes, place the grid control beneath the dropdown. Set the SortField
in grid's initialization event (or some event which fires before the
grid is rendered)

Does this help ?

Kalpesh
 
G

Guest

Hi and thanks for answering!

With your suggestion I’ll probably get one step closer, but unfortunately
not all the way. One problem remains, I don’t even know how many bound
columns the table will have. I get different data tables to bind with
depending on the selection in the drop down, and there’s no way I can predict
the column structure of the table.

I also did a simple check with a new blank web project. When I change
selection in a dropdown (autopostback=true), the events fire in the following
order:

Page_Init
Page_Load
DropDownList1_Load
DataGrid1_Load
DropDownList1_SelectedIndexChanged (This is where I create columns and bind
the grid)
Page_PreRender
DropDownList1_PreRender
DataGrid1_PreRender

Thanks again,
Paul
 
V

Vadivel Kumar

My suggestion would be, add the bound column in page_init event. And, later
on whenever you want, like in ItemBound event, use FindControl to get the
current ListItem and if the type of the control found is BoundColumn, assign
proper sort expressions, etc.,

I think this is right approach, if you've problems
do post here.

Regards
Vadivel Kumar
http://blogs.wdevs.com/varahe
 
K

Kalpesh

Hi,

Can you explain the scenario exactly ?
This will help understand it better

Kalpesh
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top