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

[Solved] Master Detail using NeedDataSource

1 Answer 219 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 02 Mar 2010, 04:16 PM

I having problems getting my Master/Detail grids to work. I have a page with two separate grids. Grid1 is the master and uses NeedDataSource to get it's data.   Grid2 is the detail grid and will also use the NeedDataSource to get its data.  Similar to this example:http://demos.telerik.com/aspnet-ajax/grid/examples/programming/selectedvalue/defaultcs.aspx.  However, the example doesn't use NeedDataSource it uses client side declared datasources.

Grid1 (Master) is working fine(Retrieve data / Add/Update/Delete).  But I'm not sure how to connect the Master grid to the Detail.  I have turned on postbackonrowclick in Grid1.   I was thinking I could call Grid2.Rebind() (Detail) in Grid1_NeedDataSource (Master) and pick up the Grid1.SelectedValue (Master's selected value) in Grid2_NeedDataSource (Detail) and pass it to my datalayer and everything will be good.  But this doesn't work. I'm getting an object reference not found on Grid1.SelectedValue on page load.  I'm guessing that's because nothing is selected on page load. ( I figured if nothing was selected it would return 0).   What's the best way to get this to work??
Grids:

                        <telerik:RadGrid ID="Grid1" runat="server" Width="95%" GridLines="None"   
                            AutoGenerateColumns="False" PageSize="13" AllowPaging="True" AllowSorting="True"  AllowMultiRowSelection="false" AllowMultiRowEdit="false"   
                            OnUpdateCommand="ULNGrid_UpdateCommand" OnNeedDataSource="ULNGrid_NeedDataSource" OnItemDataBound="ULNGrid_ItemDataBound" 
                            ShowStatusBar="true" ShowGroupPanel="false" OnInsertCommand="ULNGrid_InsertCommand" OnDeleteCommand="ULNGrid_DeleteCommand" Skin="Outlook" Visible="true" > 
                            <MasterTableView DataKeyNames="Userlistnameid" Width="100%" CommandItemDisplay="TopAndBottom" EditMode="InPlace">  
 
                                <Columns> 
                                    <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete"  />                                  
                                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" MaxLength="200" UniqueName="Name">  
                                    <ItemStyle Width="400px" />                                    
                                    </telerik:GridBoundColumn> 
                                    <telerik:GridDropDownColumn HeaderText="User List Type" UniqueName="UserListTypeddl" ListTextField="Name" ListValueField="Userlisttypeid" DataField="Userlisttypeid" 
                                 SortExpression="Userlisttypeid" DropDownControlType="DropDownList" FooterText="RadComboBox column footer" DataSourceID="SqlDsUserListTypeIdAll">  
                                    </telerik:GridDropDownColumn>    
                            <telerik:GridEditCommandColumn UpdateText="Update" UniqueName="EditCommandColumn" CancelText="Cancel" EditText="Edit">  
                                <HeaderStyle Width="85px"></HeaderStyle> 
                            </telerik:GridEditCommandColumn>                                                                        
 
                                </Columns> 
                            </MasterTableView> 
                            <ClientSettings EnablePostBackOnRowClick="true" > 
                                <Selecting AllowRowSelect="True"  />                               
                            </ClientSettings> 
                        </telerik:RadGrid> 
//******************************************************************************  
 
 
                        <telerik:RadGrid ID="Grid2" runat="server" Width="95%" GridLines="None"   
                            AutoGenerateColumns="False" PageSize="13" AllowPaging="True" AllowSorting="True"  AllowMultiRowSelection="false" AllowMultiRowEdit="false"   
                            OnUpdateCommand="ULCGrid_UpdateCommand" OnNeedDataSource="ULCGrid_NeedDataSource" OnItemDataBound="ULCGrid_ItemDataBound" 
                            ShowStatusBar="true" ShowGroupPanel="false" OnInsertCommand="ULCGrid_InsertCommand" OnDeleteCommand="ULCGrid_DeleteCommand" Skin="Outlook" Visible="true" > 
                            <MasterTableView DataKeyNames="Userlistcodeid" Width="100%" CommandItemDisplay="TopAndBottom" EditMode="InPlace">  
 
                                <Columns> 
                                    <telerik:GridButtonColumn UniqueName="DeleteColumn" Text="Delete" CommandName="Delete"  />                                  
                                    <telerik:GridBoundColumn DataField="Code" HeaderText="Code" MaxLength="32" UniqueName="Code">  
                                    <ItemStyle Width="400px" />                                    
                                    </telerik:GridBoundColumn> 
                                     <telerik:GridEditCommandColumn UpdateText="Update" UniqueName="EditCommandColumn" CancelText="Cancel" EditText="Edit">  
                                        <HeaderStyle Width="85px"></HeaderStyle> 
                                    </telerik:GridEditCommandColumn>                                                                        
 
                                </Columns> 
                            </MasterTableView> 
                            <ClientSettings > 
                                <Selecting AllowRowSelect="True"  />                               
                            </ClientSettings> 
                        </telerik:RadGrid> 

Code:

Grid1:  
        public List<UserListName> GetULNList()  
        {  
            List<UserListName> ulnlist = UserListName.getAll();  
            return ulnlist;  
        }  
 
        protected void Grid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            Grid1.DataSource = GetULNList();  
            Grid2.Rebind()  
        }  
 
 
 
//*******************************************************************************  
        public List<UserListCode> GetULCList()  
        {  
            string strulnid = Grid1.SelectedValue.ToString();  
            int ulnid = 0;  
            Int32.TryParse(strulnid, out ulnid);  
 
 
 
 
            List<UserListCode> ulclist = UserListCode.getbyKey(ulnid);  
            return ulclist;  
        }  
 
        protected void Grid2_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)  
        {  
            Grid2.DataSource = GetULCList();  
        } 

Thanks

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Mar 2010, 05:44 AM
Hi,

You  can fire the ItemCommand event   and check for the CommandName  "RowClick" . You can access the SelectedValue here and then rebind your second grid.

C#
 protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (e.CommandName == "RowClick"
        { 
           string ProductID= RadGrid1.SelectedValue.ToString(); 
         } 
    } 


Thanks,
Princy
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or