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

Pass Data Key Value of Parent Grid in DetaiTableDataBind

1 Answer 240 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Farhan
Top achievements
Rank 1
Farhan asked on 11 Nov 2011, 09:02 PM

Posted on Nov 7, 2011 (permalink)

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  I am getting the this error "An unhandled exception of type 'System.StackOverflowException' occurred in System.dll"
, 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.

*Note I posted a similar question earlier and I kept getting responses similar to what I had already posted here in the codebehind,please take a look at the code behind I have posted a comment in 

 

grdOrderDates_DetailTableDataBind event

 

 sample here and any relevant suggestions would be helpful?  Sorry about all the posted code Im dissapointed you cant attach a project file as an attachment :( and wanted to have the other person look at the same thing im looking at, thanks!

ASPX:
  
  
         <telerik:RadAjaxManagerProxy ID="OrderAM" runat="server">
             <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="AjaxOrderDates">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="grdOrderDates" LoadingPanelID="RadAjaxLoadingPanel" />
                    </UpdatedControls>                  
                </telerik:AjaxSetting>
           </AjaxSettings>
         </telerik:RadAjaxManagerProxy>
  
    <div style="margin:20px 100px 20px 400px">
    <h3> Orders By Date</h3>
    <br />
    <br />
  
    <table>
       <tr>
         <td>
            Start Date:
         </td>
         <td align="left">
            <telerik:RadDatePicker runat="server" ID="CalStartDate"></telerik:RadDatePicker>
         </td>
       </tr>
  
       <tr>
         <td>
           End Date:
         </td>
         <td align="left">
            <telerik:RadDatePicker runat="server" ID="CalEndDate"></telerik:RadDatePicker>
         </td>
       </tr>
       <tr style="margin:20px 0">
         <td colspan="2" align="center">
            <asp:Button runat="server" ID="BtnSubmit" Text="Submit" 
                 onclick="BtnSubmit_Click" />
         </td>
       </tr>
  
  
    </table>
  
  
  
        <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:Parameter Name="orderDate" Type="DateTime" />
       </SelectParameters>
    </asp:ObjectDataSource>

And here is the Codebehind file and the C# class and repository im using with some sample data so you can try this as well:

C# code Behind
  
 public partial class OrderView_Screen : System.Web.UI.Page
    {
        DateTime orderDate;
  
        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;
  
                // This line throws error An unhandled exception of type 'System.StackOverflowException' occurred in System.dll 
                DateTime orderDate = Convert.ToDateTime(dataItem["OrderDate"].Text);
  
                e.DetailTableView.DataSourceID = "ODSOrders";
                e.DetailTableView.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
  
        protected void ODSOrders_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {
          e.InputParameters["orderDate"] = orderDate;
        }
    }

C# DAL Classes and Repository Class

public class Order
    {
        public Int16 OrderID { get; set; }
        public string Customer { get; set; }
        public string Destination { get; set; }
        public Int16 Quantity { get; set; }
        public Int16 QuantityRemaining { get; set; }
        public DateTime Date { get; set; }
    }
  
public class OrderView
    {
        public DateTime OrderDate { get; set; }
        public Int16 OrderID { get; set; }
  
    }
  
  
  public class OrderRepository
    {
  
       public IEnumerable<OrderView> getOrderDatesinRange(DateTime startdate, DateTime enddate)
       {
      
            List<OrderView> orderViewList = new List<OrderView>();
  
            IEnumerable<OrderView> filterList = getOrderRange(startdate, enddate);
  
          
  
            var oList = (from o in filterList
                         where o.OrderDate >= startdate
                             && o.OrderDate < enddate
                         select o).GroupBy(i => i.OrderDate, (key, group) => group.First()).ToList();
  
  
            orderViewList = oList.Cast<OrderView>().ToList();
         
  
            
  
            return orderViewList;
        }
  
  
        public IEnumerable<Order> getOrderListinRange(DateTime orderDate)
        {
            IEnumerable<Order> ordersList = new List<Order>();
  
            IEnumerable<Order> filterList = getOrders(orderDate);
  
  
            ordersList = (from o in filterList
                          where o.Date == orderDate
                          select o).ToList();
  
            return ordersList;
        }
  
  
  
        public IEnumerable<OrderView> getOrderRange(DateTime startdate, DateTime enddate)
        {
            List<OrderView> getList = new List<OrderView>();
            
            getList.Add(new OrderView {OrderDate = new DateTime(2011,10,1) , OrderID = 1});
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 2), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 3), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 4), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 6), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 7), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 8), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 9), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 10), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 11), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 12), OrderID = 1 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 1), OrderID = 2 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 2), OrderID = 2 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 3), OrderID = 2 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 4), OrderID = 2 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 6), OrderID = 2 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 7), OrderID = 2 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 8), OrderID = 3 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 9), OrderID = 3 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 10), OrderID = 3 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 11), OrderID = 3 });
            getList.Add(new OrderView { OrderDate = new DateTime(2011, 10, 12), OrderID = 3 });
            return getList;
        }
  
  
        public IEnumerable<Order> getOrders(DateTime orderDate)
        {
            List<Order> getOrderList = new List<Order>();
  
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 1), OrderID = 1, Customer = "Evraz", Destination = "Houston", Quantity = 70, QuantityRemaining = 55 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 2), OrderID = 1, Customer = "Evraz", Destination = "Houston", Quantity = 70, QuantityRemaining = 35 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 3), OrderID = 1, Customer = "Evraz", Destination = "Houston", Quantity = 70, QuantityRemaining = 20 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 1), OrderID = 2, Customer = "TCIM", Destination = "Orlando", Quantity = 100, QuantityRemaining = 80 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 2), OrderID = 2, Customer = "TCIM", Destination = "Orlando", Quantity = 70, QuantityRemaining = 60 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 3), OrderID = 2, Customer = "TCIM", Destination = "Orlando", Quantity = 70, QuantityRemaining = 30 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 1), OrderID = 3, Customer = "PT", Destination = "Atlanta", Quantity = 80, QuantityRemaining = 70 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 2), OrderID = 3, Customer = "PT", Destination = "Atlanta", Quantity = 80, QuantityRemaining = 40 });
            getOrderList.Add(new Order { Date = new DateTime(2011, 10, 3), OrderID = 3, Customer = "PT", Destination = "Atlanta", Quantity = 80, QuantityRemaining = 20 });
              
             
            return getOrderList;
        }
  
    }

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Nov 2011, 04:58 AM
Hello Farhan,

Here is the sample code to access the datakeyvalue in DetailTableBind event which worked as expected.
C#:
protected void RadGrid2_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
{
  if (e.DetailTableView.Name == "DetalTable1")
  {
    GridTableView detailtabl1 = (GridTableView)e.DetailTableView;
    GridDataItem parentItem = (GridDataItem)detailtabl1.ParentItem;
    string Id = parentItem.GetDataKeyValue("OrderDate").ToString();
    Convert.ToDateTime(Id);
  }
}

-Shinu.
Tags
Grid
Asked by
Farhan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or