Monday, March 26, 2012

Setting SortExpression programmatically

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,
PaulMy 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

"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:5629A06B-7026-4DB2-B9AD-B0384F64B60C@.microsoft.com...
> 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
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
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

"Vadivel Kumar" wrote:

> 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
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:5629A06B-7026-4DB2-B9AD-B0384F64B60C@.microsoft.com...
> > 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
>
Hi,

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

Kalpesh

0 comments:

Post a Comment