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

Programmatically set datasource of detail table

16 Answers 436 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Laura
Top achievements
Rank 1
Laura asked on 19 Mar 2009, 03:39 PM
I have a 3 level hierarchical grid. I set the datasource of each child grid in the ASPX. I now have added a checkbox to include older data from the tables, and if that check box ix checked, I want to change the datasources of the master and detail tables. I have an oncheckboxchanged event handler for the checkbox, and can set the master grid datasource fine. What is the code to set the detail table's datasource in C# in the checkboxchanged event handler?

Thanks
  protected void outdatedChanged(object sender, EventArgs e) 
    { 
        CheckBox cb = (CheckBox)sender; 
        if (cb.Checked) 
        { 
            providersGrid.DataSourceID = "CCprovidersIncludingOutdated"// master grid       
         //I want to set the detail grids datasources here for outdated content 
        } 
        else 
        { 
            providersGrid.DataSourceID = "CCproviders"// master grid 
            //I want to set the detail grids datasources here 
             
        } 
    } 
 

16 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 20 Mar 2009, 05:51 AM
Hello Laura,

You can access the DetailTables and set its datasource as shown below:
protected void outdatedChanged(object sender, EventArgs e)  
    {  
        CheckBox cb = (CheckBox)sender;  
        if (cb.Checked)  
        {  
            providersGrid.DataSourceID = "CCprovidersIncludingOutdated"// master grid        
          foreach (GridDataItem dataItem in providersGrid.MasterTableView.Items) 
           { 
            if (dataItem.Expanded) 
            { 
                GridTableView nestedTableView = (GridTableView)dataItem.ChildItem.NestedTableViews[0]; 
                nestedTableView.DataSourceID = ""//set datasource here; 
                //similarly you can repeat the same for the 3rd level detailtable 
            } 
            
          } 
        }       
    }  

Also refer to the following help document which explains on how to access detail tables:
Traversing detail tables/items in Telerik RadGrid



Thanks
Princy.
0
Laura
Top achievements
Rank 1
answered on 22 Mar 2009, 02:33 AM
If I use the code you suggested, and my grid is not expanded, the datasources of the detail tables won't be set. I want to set all 3 levels of the hierarchacy at once when the checkbox(outside the grid) is checked.

So tried doing it from the link you recommended and I can set the datasource this way for the second level hierarchy:
providersGrid.DataSourceID = "CCprovidersIncludingOutdated";
GridTableView nestedTableViewService = (providersGrid.MasterTableView.Items[0] as GridDataItem).ChildItem.NestedTableViews[0];
nestedTableViewService.DataSourceID = "CCservicesIncludingOutdated";


But I am having trouble setting the third level. It gives me an out of bounds error if I try this:
 GridTableView nestedTableViewFeed = nestedTableViewService.Items[0].ChildItem.NestedTableViews[0];


Any suggestions?

Thanks
0
Yavor
Telerik team
answered on 24 Mar 2009, 07:34 AM
Hi Laura,

When a level is not expanded and you are using a standard loading mode, its nested items will not be accessible. Hence the exception.
To work-around this, you can use client load mode for the control - all the info for the detail tables will be pre-fetched.
I hope this helps.

Sincerely yours,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 27 Mar 2009, 09:25 PM
Will I have to do client side loading for the whole grid then? I have over 10,000 records, and it will take too long. Maybe I just don't have the full understanding of the hierarchy and client side binding. Could you please give me some more information on how I would do the client load mode if I wanted to change the datasource of my detail tables when it is not expanded?

THanks
0
Yavor
Telerik team
answered on 30 Mar 2009, 12:12 PM
Hi Laura,

There are basically two modes of hierarchy loading - server and client. With server loading, only the top-level of the control is fetched initially. All other inner levels are fetched as they are expanded. With client load mode, all levels are pre-fetched, and no subsequent posts to the server are made, as the levels are expanded. One possible option in this case would be to enable client side loading for the hierarchy, but also allow paging. This will balance the setup.

Sincerely yours,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 31 Mar 2009, 03:54 AM
I did have paging allowed, and it still took a very long time to load. Is there a sample code of what I need to accomplish? I know in the past when I would have a question, the telerik staff would create a sample program detailing exactly what had to be done, and it was very helpful. I have 2 problems at the moment - this one which I need to change the datasource of the detail tables, and the other problem related to it is setting the currentPageIndex for the master and detail tables. Would it be possible to give a small sample with these two items in it? I guess it would relate to the libraray submission that I used to keep the detail tables expanded on rebind. Thank you.
0
Yavor
Telerik team
answered on 02 Apr 2009, 12:32 PM
Hi Laura,

Can you please elaborate on the required setup? How do you need to change the source for the detail table? How/where do you need to change the page index? Any additional information will help me better address the question at hand.

Kind regards,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 02 Apr 2009, 02:10 PM
THank you, and I hope you can help. This is my setup:

I have a 3 level hierarchical grid. The 3 tables are providers, services, feeds. A provider can have 1 or more services and a service can have one or more feeds. There are multiple pages of providers, and there may be multiple pages in the expanded state of services and feeds if there are more than 10.
Each of the tables has a field called end_date.It starts out at null. If the user deletes a provider, service or feed I set the end_date to now so that we keep a record of when that particular record was terminated.

When I create my original dataset for the grid for all 3 tables, my query has a "where end_date is null or end_date > getdate()" so that I retrieve all records where the end_date is not less than right now. I have a checkbox that the user can check if he wants to see all data including outdated data (which would include records where end_date<getdate() ). So in my code behind, I wanted to have an oncheckbox changed event handler, which if it is checked, change all 3 table datasourceIDs to the query retrieving all records and not only those with valid dates. And then if it is not checked, changed it back in the codebehind (or client side if necessary) to only select those records the have an end_date either of null or > now. That is the first problem I am having of trying to set not only the providers (which works fine) datasourceID, but the services and feeds datatsourceID as well even if the grid is not expanded.

Second problems is with the paging of detail tables. I have implemented the library submission for keeping a hierarchical grid expanded on a rebind. I practially took the code as is, dropped it in my code, and except for changing a few variable names, it works on the surface exceprt for a few glitches which I don't know how or where to fix.

The feeds table is my lowest level detail grid. I may have many pages of feeds under a service. Most of the editing is being done at the feeds level. I created rad popup custom windows to do the editing of feeds. If I am on the 3rd page of feeds under a particualr service and edit a feed there, a window pops up to do the editing, When the user clicks ok on that page, that page's code saves the feed, and returns to the grid, and the grid rebinds. I want to stay on the current feed page to most likely edit the next feed, but it returns to the first page(in the detail table) of feeds.You suggested setting the currentpageindex for the master and detail tables, but I do not know which function that should be set in, and I dont know the proper syntax of how to use it.

I hope I made myself clear enough for you to understand my questions, and hope this is enough information for you to help me.

Thank you,
Laura
0
Yavor
Telerik team
answered on 03 Apr 2009, 08:09 AM

Hi Laura,

Thank you for the additional details supplied, which make the setup clear.
Basically, altering the datasources for the controls is not recommended in this scenario. You can, however, take a more stable and straightforward approach.
One possible option is to directly alter the select statement(s) for the present datasources, and rebind the grid. For example, let us take the following setup, demonstrated in this sample:

http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/declarativerelations/defaultcs.aspx

It is a three level hierarchy. For each level of the hierarchy, a different datasource control is used, like so:

   <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Customers"
            runat="server"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM Orders Where CustomerID = @CustomerID"
            runat="server">
            <SelectParameters>
                <asp:SessionParameter Name="CustomerID" SessionField="CustomerID" Type="string" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Order Details] where OrderID = @OrderID"
            runat="server">
            <SelectParameters>
                <asp:SessionParameter Name="OrderID" SessionField="OrderID" Type="Int32" />
            </SelectParameters>
        </asp:SqlDataSource>

The first select statement is:

SelectCommand="SELECT * FROM Customers"

This selects all fields and records from the Customers table. Now, let us assume that you wanted to display only Records, which CustomerID field starts with a "B". To do this, you can alter the select statement to include a where clause:

"...Where CustomerID LIKE 'B%'"

and rebind the grid. This logic can be moved in the event handler for another control - for example the checkbox check changed event. Rebinding the grid will ensure the new query is applied.
Further, with respect to the page index. The following example allows editing in a hierarchy:

http://demos.telerik.com/aspnet-ajax/grid/examples/dataediting/threelevel/defaultcs.aspx

If you go to an inner level, and go to the last page, to edit a record, the page index will be preserved. This is demonstrated in the screenshot attached to this message.

Give these suggestions a try and let me know how it goes, or if you find that I am leaving something out.

Greetings,

Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 03 Apr 2009, 12:50 PM
Thank you for the much more detailed response. The first part of the response make complete sense and I will change the select command in the on checkbox changed event handler. That is a very clean and clear way to do it.

The second part still does not help me. I see in the sample demo that the expanded state and pagination persist, but I do not see code that actually makes this happen. They have allowautomaticupdates=true, and in my code I cannot have this as I have complex stored procedures to save my data, so I need to rebind.  I am using the library submission to keep the expanded state persisted on rebind, but it does not keep the correct page index.

Thank you,
Laura
0
Yavor
Telerik team
answered on 07 Apr 2009, 06:42 AM
Hello Laura,

With respect to the second issue. I assume that you are rebinding the grid. In which event/where in the code, are you rebinding the grid control? I am trying to recreate the exact setup you have locally, so that I can suggest the best option.

Best wishes,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 07 Apr 2009, 03:18 PM
I am invoking an ajax request from a javascript function to make the rebind happen. I am doing it this way because I am editing the record using a custom popup rad window and when the window closes, I need to rebind. I have a onajaxrequest event set up in my radajaxpanel that will call the server side event handler. Here is my radajaxpanel:

<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1" 
             OnAjaxRequest="RadAjaxPanel1_AjaxRequest" ClientEvents-OnRequestStart="centerUpdatePanel();"

And here is the javascript function that creates the radwindow for editing:

function ShowEditFeedForm(id, rowIndex) { 
             // get username from querystring to pass to next window 
             var urlArgs = cbeGetURLArguments(); 
             var userName = urlArgs.NAME; 
             var sync = urlArgs.SYNC; 
 
             var grid = $find("<%= providersGrid.ClientID %>");   
 
             var oWnd = window.radopen("editFeedForm.aspx?feedID=" + id + "&SYNC=" + sync + "&NAME=" + userName, "feedDialog"); 
             oWnd.add_close(OnClientFeedClose); 
             var oArg = new Object(); 
 
             oArg.serviceID = id; 
 
             //set the arguments that will be needed on the way back so they are not undefined 
             oWnd.argument = oArg; 
             return false
         } 


And here is the function that is called when the window is closed:

function OnClientFeedClose(oWnd) { 
 
             $find("<%= RadAjaxPanel1.ClientID %>").ajaxRequest(); 
         } 

Which invokes the server call:
protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e) 
    { 
        setDataSources(); 
 
        if ("providerDelete" == e.Argument) 
            { 
                int deleted = deleteProvider(Convert.ToInt32(deleteProviderID.Text)); 
                providersGrid.Rebind(); 
            } 
        else if ("providerDeactivate" == e.Argument) 
            { 
                int deactivated = deactivateProvider(Convert.ToInt32(deleteProviderID.Text)); 
                providersGrid.Rebind(); 
            } 
        else if ("serviceDelete" == e.Argument) 
        { 
            int deleted = deleteService(Convert.ToInt32(deleteServiceID.Text)); 
            providersGrid.Rebind(); 
        } 
        else if ("serviceDeactivate" == e.Argument) 
        { 
            int deactivated = deactivateService(Convert.ToInt32(deleteServiceID.Text)); 
            providersGrid.Rebind(); 
        } 
        else if ("feedDelete" == e.Argument) 
        { 
            int deleted = deleteFeed(Convert.ToInt32(deleteFeedID.Text)); 
            providersGrid.Rebind(); 
        } 
        else if ("feedDeactivate" == e.Argument) 
        { 
            int deactivated = deactivateFeed(Convert.ToInt32(deleteFeedID.Text)); 
            providersGrid.Rebind(); 
        } 
        else if ("Services" == e.Argument) 
        { 
                providersGrid.Rebind(); 
        } 
        else 
            providersGrid.Rebind(); 
 
    } 
So you see that this final call is what calls the rebind after data has been saved to the database ( the data gets saved to the database on the server side inside the radwindow before it closes)   and the radwindow is closed. 

Hope this gives you a better understanding of what I am doing.

Thanks,
Laura




0
Yavor
Telerik team
answered on 10 Apr 2009, 07:50 AM
Hello Laura,

Attached to this message, is a small application, which handles a similar task.
I hope it helps.

Sincerely yours,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Laura
Top achievements
Rank 1
answered on 10 Apr 2009, 08:58 PM
Thank you for the application. I copied it and ran it, and all I see it doing is having a 3 level hierarchy grid and when you click the button it expands the 6th item down and rebinds. I don't see anywhere where there is an example of adding or editing records and then rebinding while keeping the expanded hierarchy grid expanded and on it's proper page.

How does this  help me accomplish what I need? I see you have this in the buttonclick event handler. Should this go in my ajaxrequest before I rebind, and what should I set the current page index to? I am sorry that I am not totally understanding how this solution fits in with my current needs.
0
Yavor
Telerik team
answered on 14 Apr 2009, 06:27 AM
Hi Laura,

I will prepare another sample, which matches closely your setup, and attach it to this message shortly.

Kind regards,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
0
Yavor
Telerik team
answered on 14 Apr 2009, 06:48 AM
Hi Laura,

Actually, since the setup on your end is not a trivial one, the best option, in order to make sure I do not leave anything out, would be if you open a formal support ticket, and send me a small working project, demonstrating your present implementation. I will review the setup, and get back to you with a precise suggestion.

Regards,
Yavor
the Telerik team

Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Laura
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Laura
Top achievements
Rank 1
Yavor
Telerik team
Share this question
or