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

Pass Data Key Value of Parent Grid in DetaiTableDataBind

3 Answers 296 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Farhan
Top achievements
Rank 1
Farhan asked on 07 Nov 2011, 04:03 PM

Hi,
   I am working on a form that requires a detail grid view for each Grid object. I have made a main OrderView Grid which just shows the dates for the orders and a child grid that should get the OrderDate from the Parent grid and show orders just for the selected Date in the parent grid. the OrderDate is common between both grids, but when I try to pass in the OrderDate to the child grid as a select param for the Object data source it always comes out something like '1/1'0001' and does not pass in the correct date from the parent grid. I am attaching my sample project as a file along with, I looked at the example at http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx
to do this. Can someone please show me how to do this with an example in code?  Your helps appreciated thanks.

  

ASPX PAGE:
  
  
  
        <telerik:RadGrid runat="server" ID="grdOrderDates" Width="750px" Height="100%" AllowPaging="true"
            AllowSorting="true" EnableLinqExpressions="false"
            ShowFooter="true" ondetailtabledatabind="grdOrderDates_DetailTableDataBind">
            <PagerStyle Mode="NextPrevAndNumeric" />
            <MasterTableView AutoGenerateColumns="false" AllowAutomaticDeletes="true" AllowAutomaticInserts="true"
                AllowAutomaticUpdates="true" InsertItemPageIndexAction="ShowItemOnCurrentPage"
                DataKeyNames="OrderDate" CommandItemDisplay="Top" EditMode="PopUp" AllowMultiColumnSorting="true"
                OverrideDataSourceControlSorting="true" AllowFilteringByColumn="true" FilterItemStyle-HorizontalAlign="Left"
                ShowFooter="true" ShowHeader="true" HierarchyLoadMode="ServerOnDemand">
          
                <CommandItemSettings AddNewRecordText="" />
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                </RowIndicatorColumn>
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                </ExpandCollapseColumn>
                   
  
                       <DetailTables>
                          <telerik:GridTableView AutoGenerateColumns="false" DataKeyNames="OrderDate" DataSourceID="ODSOrders">
                                
  
                              <ParentTableRelation>
                                 <telerik:GridRelationFields DetailKeyField="Date" MasterKeyField="OrderDate" />
                              </ParentTableRelation>
                              <Columns>
                                 <telerik:GridBoundColumn DataField="Date" HeaderText="OrderDate" ></telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="OrderID" HeaderText="OrderID" ></telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="Customer" HeaderText="Customer" ></telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="Location" HeaderText="Location" ></telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="Destination" HeaderText="Destination" ></telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="Quantity" HeaderText="Quantity" ></telerik:GridBoundColumn>
                                 <telerik:GridBoundColumn DataField="QuantityRemaining" HeaderText="Quantity Remaining" ></telerik:GridBoundColumn>
                              </Columns>
                          </telerik:GridTableView>
                       </DetailTables>
  
                <Columns>
                    <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <HeaderStyle></HeaderStyle>
                    </telerik:GridEditCommandColumn>
                    <telerik:GridBoundColumn DataField="OrderDate" UniqueName="OrderDate" HeaderText="Order Date" />
  
                </Columns>
                  
              
            </MasterTableView>
            <GroupingSettings CaseSensitive="False" />
              
        </telerik:RadGrid>
    </div>
  
     
    <asp:ObjectDataSource ID="ODSOrderList" runat="server" TypeName="DAL.OrderRepository"
        DataObjectTypeName="DAL.OrderView" SelectMethod="getOrderDatesinRange" 
    onselecting="ODSOrderList_Selecting">
       <SelectParameters>
             <asp:Parameter Name="StartDate" DbType="DateTime" />
             <asp:Parameter Name="EndDate" DbType="DateTime" />
       </SelectParameters>
    </asp:ObjectDataSource>
      
    <asp:ObjectDataSource ID="ODSOrders" runat="server" TypeName="DAL.OrderRepository"
        DataObjectTypeName="DAL.Order" SelectMethod="getOrderListinRange" >
      <SelectParameters>
             <asp:SessionParameter Name="orderDate" SessionField="OrderDate" DbType="DateTime" />
       </SelectParameters>
    </asp:ObjectDataSource>
      
   </asp:Content>
  
  
C# Code :
  
 protected void Page_Load(object sender, EventArgs e)
        {
  
        }
  
        public void LoadOrdersByDate()
        {
            //Loads Parent Grid when date range for Orders is given and binds Parent grid
            grdOrderDates.DataSourceID = "ODSOrderList";
            grdOrderDates.DataBind();
        }
  
        protected void ODSOrderList_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
            e.InputParameters["StartDate"] = CalStartDate.SelectedDate;
            e.InputParameters["EndDate"] = CalEndDate.SelectedDate;
        }
  
        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            //Loads Parent Grid when date range for Orders is given and binds Parent grid
            LoadOrdersByDate();
        }
  
  
        protected void grdOrderDates_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            try
            {
  
                GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
                DateTime orderDate = Convert.ToDateTime(dataItem.GetDataKeyValue("OrderDate"));
  
                e.DetailTableView.DataSourceID = "ODSOrders";
  
                //Pass in the OrderDate value from the Parent grid so the child grid only shows Orders for the selected Date
                ODSOrders.SelectParameters["orderDate"].DefaultValue = orderDate.ToString();
  
                e.DetailTableView.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

3 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 07 Nov 2011, 05:04 PM
Hello,

<telerik:GridBoundColumn DataField="Date" HeaderText="OrderDate" UniqueName="OrderDate" ></telerik:GridBoundColumn>

protected void grdOrderDates_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            try
            {
   
                GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
                DateTime orderDate = Convert.ToDateTime(dataItem.GetDataKeyValue("OrderDate"));
    //  or
                DateTime orderDate = Convert.ToDateTime(dataItem["OrderDate"].Text);
 
..............
.............
...........

Let me know its works or not?

Thanks,
Jayesh Goyani
0
Farhan
Top achievements
Rank 1
answered on 07 Nov 2011, 08:47 PM
Hi Jayesh thanks for replying,

 ok so it seems like its getting the correct date from the Parent Grid but I cant seem to bind the Detail rows to Object Data source ODS orders by setting the select Parameter "orderDate" value to the orderDate I get from the Parent Grid. Im getting the error "

An unhandled exception of type 'System.StackOverflowException' occurred in mscordb.dll

" . I was also setting the Select Parameter of the ODS as a session Parameter which was wrong so now I have it set to <asp:Parameter>. Here is the method for binding DetailView can you point out how could I pass in the value of orderDate to the Select parameter for the Object data source?

protected void grdOrderDates_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
       {
           try
           {
               GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
              // orderDate = Convert.ToDateTime(dataItem.GetDataKeyValue("OrderDate"));
               DateTime orderDate = Convert.ToDateTime(dataItem["OrderDate"].Text);
               //Setting the select Parameter value throws the error An unhandled exception of 
              // type 'System.StackOverflowException' occurred in mscordb.dll
               ODSOrders.SelectParameters["orderDate"].DefaultValue = orderDate.ToString();
               e.DetailTableView.DataSourceID = "ODSOrders";
               e.DetailTableView.DataBind();
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
0
Jayesh Goyani
Top achievements
Rank 2
answered on 08 Nov 2011, 07:25 AM
Hello Farah,

try with this link.

Note : below line of code is needed in  event DetailTableDataBind.

 "e.DetailTableView.DataBind(); "

Thanks,
Jayesh Goyani
Tags
Grid
Asked by
Farhan
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Farhan
Top achievements
Rank 1
Share this question
or