This is a migrated thread and some comments may be shown as answers.

Dynamically Generated RadGrid, Custom Columns, and Sorting

4 Answers 328 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Golem
Top achievements
Rank 1
Golem asked on 27 Feb 2009, 08:17 PM
Hello.  I am trying to build a custom grid (extended from the RadGrid) dynamically at run-time, and the grid makes use of some custom column types that i have defined in a class file.   As an added complication, the grid is housed in a user control.

I am having trouble getting the SortCommand to fire.  It is my understanding that for the SortCommand to fire, the structure of the grid (i.e. the columns) needs to be created in the Init event of the grid.  It is also my understanding, that because I am using custom column types, the MasterTableView's "EnableColumnViewState" property needs to be set to false.

The problem I'm having is that the structure of the grid changes depending on what the user chooses to do.  In my case, I have a TreeView control with nodes, and each node causes a different DataTable to be generated, and the columns change from one table to the next.

So... until the user actually clicks a node to tell the application what data they want to view, I can't figure out how to set up the grid in the Init event since the Click event won't fire until after the Init event.  So for now, I build the grid structure and bind in the click event, which works for displaying the data, but because it has happened too late in the event chain, ViewState isn't properly handled and the SortCommand event never fires when a user clicks on a column header.

I would appreciate any suggestions on how to set this up properly.  Thanks.

4 Answers, 1 is accepted

Sort by
0
Accepted
Sebastian
Telerik team
answered on 03 Mar 2009, 12:10 PM
Hello Golem,

Indeed to change the grid column structure dynamically on each postback, you will need to intercept the OnInit event and use the approach presented in this topic from the documentation.

The idea is to use the Request.Form collection (or alternatively the Request.Params("__EVENTTARGET") or Request.Params("__EVENTARGUMENT") values) to determine the initiator of the postback request and change the data presentation. Only thus the viewstate/lifecycle of the grid will remain consistence.

The other solution would be to use auto-generated columns and merely change the data source of the grid dynamically when necessary.

Kind regards,
Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Golem
Top achievements
Rank 1
answered on 03 Mar 2009, 03:54 PM
Thanks for your reply, Sebastian.  The link you provided has pointed me in the right direction, I think.  The code was very similar similar to what I was trying to do, except that my RadGrid was inside a UserControl that was placed on the page in the HTML.  I switched over to using a PlaceHolder on the page, and inserted the UserControl programmatically (like the example does with the RadGrid). 

I still have some minor issues to work out, but most of my grid features, including the SortCommand event, are working, so I think I'm on the right track.

Thanks again for your help.

0
Golem
Top achievements
Rank 1
answered on 03 Mar 2009, 07:08 PM
I have another follow-up question... The example you provided uses a dropdown list to trigger an auto-postback to the server.  The server-side code then intercepts the selected index using the Request.Form.Get method to ask for the value of the selected index.

In my project, I'm trying to use a RadTreeView object with a collection of parent/child nodes instead of a dropdown.  Is there an easy way to capture the RadTreeView selected node at the Init event?  It doesn't seem to be as easy as looking at the Request.Form collection to get the selected node.  I can use __EVENTTARGET and __EVENTARGUMENT to get at the index of the selected node (i.e. 1:1 for the 2nd child of the 2nd node) but I was hoping there is an easier way to intercept the node information at the Init event than trying to parse the __EVENT strings and trying to map them back to the Nodes collections to get the node information.

Thanks in advance...
0
Sebastian
Telerik team
answered on 04 Mar 2009, 09:57 AM

Hello Golem,

I am afraid there is no easier way in this particular case than using the __EVENTARGUMENT or __EVENTTARGET values from the Request.Params collection. The other solution I can think of is to avoid generating different set of grid columns on each postback but rather use auto-generated columns and merely switch the data source for the control.

Kind regards,

Sebastian
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Golem
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Golem
Top achievements
Rank 1
Share this question
or