Hi everyone,
Sorry if such question has already been asked, but i cant find a topic that helps me completely.
First of all, Im using a RadGrid with most of the grid functions (Sorting, Paging, Grouping and Filtering). Im populating my grid with data coming from my database (as a ObservableCollection<Class>) through the page's code-behind, columns are auto-generated (nothing special so far). Note that my grid's EnableViewState="False".
The first problem i encountered was i had to use the OnNeedDataSource property (which i added), otherwise i was losing my grid data content when using the sorting and such. I then noticed that OnNeedDataSource was always requesting my database so I created a ViewState variable that will contain my collection variable in order to save those database round-trips (for each sorting, page change, etc.). The approach i chose was to only create a ViewState on the first grid function (ex: first time we do a sort). The reason is because most of my users wont do grid functions at all (so for those users, Viewstate wont be used at all). I accomplished this by adding some handlers (OnItemCommand, OnGroupsChanging) that create the ViewState if it doesnt exist already (OnNeedDataSource checks also for it).
Everything seemed to work as expected until i noticed that one grid function seemed to be failing at random occasion: playing with the expendable/foldable rows (when header(s) are grouped). The only way i could fix this was by adding the EnableViewState="True" to the grid, but now i end up with 2 ViewState per grid...which i dont really like.
My questions:
1-Is it normal that I depend on the EnableViewState="True" for expendable/foldable rows (where all other functions work with my "custom" Viewstate?)
2-Can that second "auto-generated" Viewstate (EnableViewState="True") be used to do the job of the first one? If yes, how can we access it?
3-Is there a way to force the OnItemCommand and OnGroupsChanging handlers to be fired before OnNeedDataSource? Having this would save me a round-trip.
4-Do you propose a better way to save the database round-trips?
5-I was wondering if it is a best practice to manually clear Viewstate variable that we create ourselves (when page quits/redirect)? If yes, any example?
6-Do you think letting the grid asking the database everytime would be less load on the server than 2 Viewstates?
Here are snippets of my code i have so far:
Thanks...and sorry btw for the long post but i wanted to make everything clear.
Sorry if such question has already been asked, but i cant find a topic that helps me completely.
First of all, Im using a RadGrid with most of the grid functions (Sorting, Paging, Grouping and Filtering). Im populating my grid with data coming from my database (as a ObservableCollection<Class>) through the page's code-behind, columns are auto-generated (nothing special so far). Note that my grid's EnableViewState="False".
The first problem i encountered was i had to use the OnNeedDataSource property (which i added), otherwise i was losing my grid data content when using the sorting and such. I then noticed that OnNeedDataSource was always requesting my database so I created a ViewState variable that will contain my collection variable in order to save those database round-trips (for each sorting, page change, etc.). The approach i chose was to only create a ViewState on the first grid function (ex: first time we do a sort). The reason is because most of my users wont do grid functions at all (so for those users, Viewstate wont be used at all). I accomplished this by adding some handlers (OnItemCommand, OnGroupsChanging) that create the ViewState if it doesnt exist already (OnNeedDataSource checks also for it).
Everything seemed to work as expected until i noticed that one grid function seemed to be failing at random occasion: playing with the expendable/foldable rows (when header(s) are grouped). The only way i could fix this was by adding the EnableViewState="True" to the grid, but now i end up with 2 ViewState per grid...which i dont really like.
My questions:
1-Is it normal that I depend on the EnableViewState="True" for expendable/foldable rows (where all other functions work with my "custom" Viewstate?)
2-Can that second "auto-generated" Viewstate (EnableViewState="True") be used to do the job of the first one? If yes, how can we access it?
3-Is there a way to force the OnItemCommand and OnGroupsChanging handlers to be fired before OnNeedDataSource? Having this would save me a round-trip.
4-Do you propose a better way to save the database round-trips?
5-I was wondering if it is a best practice to manually clear Viewstate variable that we create ourselves (when page quits/redirect)? If yes, any example?
6-Do you think letting the grid asking the database everytime would be less load on the server than 2 Viewstates?
Here are snippets of my code i have so far:
<telerik:RadGrid ID="RadGrid1" runat="server" PageSize="5" Width="75%" AllowSorting="True" AllowFilteringByColumn="True" AllowMultiRowSelection="True" AllowPaging="True" ShowGroupPanel="True" GridLines="None" AutoGenerateColumns="True" ShowFooter="True" OnItemCommand="testcomm" OnGroupsChanging="testgroup" OnColumnsReorder="testreorder" OnNeedDataSource="RadGrid1_NeedDataSource" Skin="WebBlue" EnableViewState="True" > <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle> <ClientSettings AllowDragToGroup="True" AllowColumnsReorder="True" ReorderColumnsOnClient="True"> <Resizing AllowColumnResize="true" /> </ClientSettings> <GroupingSettings ShowUnGroupButton="true" /> </telerik:RadGridprotected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) { ObservableCollection<PersonDTO> col = new ObservableCollection<PersonDTO>(); if (ViewState["RadGrid1_DataSource"] != null) { col = (ObservableCollection<PersonDTO>)ViewState["RadGrid1_DataSource"]; } else { col = GridDataSource2(); } RadGrid1.DataSource = col; } private void LoadViewState() { ObservableCollection<PersonDTO> col = new ObservableCollection<PersonDTO>(); if (ViewState["RadGrid1_DataSource"] == null) { // collection isn't in view state, so we need to load it from scratch. if (RadGrid1.DataSource == null) { col = GridDataSource2(); } else { col = (ObservableCollection<PersonDTO>)RadGrid1.DataSource; } if (col != null) { ViewState.Add("RadGrid1_DataSource", col); } } } protected void testcomm(object o, Telerik.Web.UI.GridCommandEventArgs e) { LoadViewState(); } protected void testreorder(object o, Telerik.Web.UI.GridColumnsReorderEventArgs e) { LoadViewState(); } protected void testgroup(object o, Telerik.Web.UI.GridGroupsChangingEventArgs e) { LoadViewState(); }Thanks...and sorry btw for the long post but i wanted to make everything clear.