This has to be pretty common and simple and that must be why I havent' been able to find an answer :-)
The RadGrid seemed fine for us until we tried to replace a vanilla grid on a page that uses buttons to dynamically change the datasource AND that also Sorts.
For example,
Say one button on the webform connects to a source of the result of Membership.GetAllUsers() which of course returns a collection of Membership Users objects and then another button fills the grid with a results of some SQL.
Okay... you start with adding the two buttons and in their click events you put standard grid.Datasource=XXX / grid.DataBind() lines.
Everything is good.
BUT it seems to us that when you do this "simple data binding" you lose the ability to SORT the grid. When you sort, the RadGrid disappears (because it is just a rendered table and it is empty.. even though there was ViewState there is no data and so there is no more grid).
So instead we tried going the Telerik way and not using DataBind()... instead using the NeedDataSource event. But this has the problem of it only apparently allowing us to use the grid for one single dedicated purpose - such as GetAllUsers() instead of dynamically changing the data source via buttons.
We thought that somehow we could put some information in the GridNeedDataSourceEventArgs and read it in the NeedDataSource event and do a switch/case but it is readonly and the docs even imply that we only have one data soruce option:
"
In the code of this event you should prepare the data source (list of objects) for Telerik RadGrid and assign it to the grid's DataSource property.
"
How can we both change the grid data source on-demand AND have sorting? It's a more or less built-in feature for other grids including the C1/MS freebies in VS so we took it for granted that it would be here too. Do we have to go back to old style manual code for sorting or Are we just missing something in front of us?
8 Answers, 1 is accepted
:)
So.... does the lack of answers mean that the answer is just stupidly obvious that it doesn't deserve a reply or does it mean that unlike most all other grid products you Can't dynamically bind one grid to different data sources on-demand and also get sorting?
Um... this is a pretty common scenario right?
Dumping the RadGrid product for the Videosoft/C1 freebie that comes with VS2008 is the correct answer, I guess. ;-(
I am sorry to hear that you are having a hard time configuring your grid instance to operate in par with your requirements. I will summarize the two approaches you can utilize in this case:
- Using simple binding with DataBind() calls:
If you choose this method you will need to handle the sorting, paging, etc. operations manually intercepting the corresponding server events (as you will do with regular MS GridView control). The information from this demo targets this subject.
- Using advanced binding with NeedDataSource event handling:
Here you can assign difference data source for the grid conditionally inside the specified event handler using your custom logic. When the grid needs to be refreshed or bound to a new data source, merely invoke its Rebind() method which will trigger the NeedDataSource event explicitly. Thus the sorting, paging, etc. features will not require any coding.
Further details about the control lifecycle and event sequence/Rebind() method usage can be found here.
Kind regards,
Sebastian
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
thing is though that even though there is VIewState for the page and the grid it keeps going back to the well for completely fresh data. I'ved read different things on the telerik site about that... some say that that is right and others say that it viewstate is there then NeedDataSource will use that.
Also if you *don't* use the NeedDataSource and instead use traditional methods but also enable sorting then sorting will work but only if the user clicks that Column Header Text. If instead the user clicks on the little sort graphic (the up/dow arrow on the currently sorted column) the grid will blow up with an exception traced to EnableEventValidation.... so it seems that if we want to use sorting then we have to use the NeedDataSource route (and default to getting complete data every time even when there is viewstate).
Good enough now that we know. And good thing we bought the source code :)
Thanks.
I can load 1-4 differennt data views to fill my grid and i use the needdatasource event to fill it. Works great the first time I load the data - paging seems to work,
but when i try to sort, it calls needdatasource again - although I already have a datasource.
Hence - no sorting ...... The answer does not address this problem.
There are two additional options, which you may want to consider in this situation.
The first is to dynamically alter the control's structure, as demonstrated in this article:
http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html
Or, alternatively, if for example the structures of the grids are rather different, you can use separate user controls, each containing a separate instance of the grid. Depending on which control is needed, you can load these dynamically, or toggle on/off their visibility. In any case, the grid's structure needs to be properly created, and either a datasource control, or the NeedDataSource event needs to be used, to provide data for the grid.
I hope one of these options is suitable for you.
Regards,
Yavor
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
These all work great with the Needs datasource to load the data but every sorting and paging click call causes a postback and hits the needs datasource event again. How can I persist the dynamic datasource or avoid the postback ?
making a CASE statement for each possible sort is not a solution.
I went to a SQLDatasource Then in Page_Init all I had to do was change the SelectCommand.
No binding or rebinding was needed or needs data source was necessary. if they clicked my "View Type" buttons it caused a full reload and the grid worked like it should.
.
| Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init |
| 'default query = MY Open |
| sdsSO.SelectCommand = "SELECT * FROM tpsv_Portal_ServiceOrders_Employee WHERE (TechAssigned = " & Utilities.GetCookie("EmployeeNumber") & ") and (DateClosed is null) and (Status <> 'Void')" |
| Label1.Text = "My Open Service Orders" |
| ' Account View |
| If (Request.QueryString("a")) <> Nothing Then |
| Dim searchParameter As String = Request.QueryString("a").ToString() |
| sdsSO.SelectCommand = "SELECT * FROM tpsv_Portal_ServiceOrders_Employee WHERE AccountNumber = " & searchParameter |
| Try |
| Dim AccountName As String = PortalDataAccess.GetAccountName(CInt(Request.QueryString("a"))) |
| Page.Title = "Service Orders: " & AccountName |
| Label1.Text = "Service Orders: " & AccountName |
| Catch ex As Exception |
| End Try |
| AccountNumber.Value = Request.QueryString("a") |
| End If |
| ' Reset to My |
| If (Request.QueryString("my")) <> Nothing Then |
| If (Request.QueryString("my")) = 1 Then |
| Dim searchParameter As String = Request.QueryString("my").ToString() |
| sdsSO.SelectCommand = "SELECT * FROM tpsv_Portal_ServiceOrders_Employee WHERE (TechAssigned = '" & Utilities.GetCookie("EmployeeNumber") & "')" |
| Label1.Text = " My Service Orders" |
| End If |
| 'Show All |
| If (Request.QueryString("my")) = 0 Then |
| sdsSO.SelectCommand = "SELECT * FROM tpsv_Portal_ServiceOrders_Employee " |
| Label1.Text = "All Service Orders" |
| End If |
| End If |
| End Sub |
The NeedDataSource event handler is raised for each sorting operation. This is because a rebind is issued in the control. The latest solution which you have come up with is also a good approach. Setting the select command on each page load will ensure that the proper set of records are returned each time.
I hope that the setup is working as per your requirements now.
Kind regards,
Yavor
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.