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

Load Detail Tables and expand

2 Answers 50 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 01 Aug 2013, 11:35 PM
I have been trying to load details tables where they exist. (fault tickets for a session)
I can expand all of the details tables, but they never contain any details.
I think that the problem is to do with the loading order due to the fact that I can expand all the deets tables but they contain no data.
I have tried setting the HeirachyLoadMode="Client" in the mastertableview and in the gridtableview.

The C#, then the asp.net are below.

I'm relatively new to this and any help would be appreciated
protected void HelpdeskGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    DataSet dataset = new DataSet();
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SiteSqlServer"].ConnectionString);
 
    int nCompany = int.Parse(UserInfo.Profile.GetPropertyValue("Company"));
 
    try
    {
        conn.Open();
 
        SqlDataAdapter adapter = new SqlDataAdapter();
        string strSql=string.Format("select top 100 s.SessionID as SessionID,co.Name as Company,cp.Name as Campaign,ca.FirstName,ca.LastName,ca.Email,ca.PhoneNo,s.Start,{1} as Status "+
                                    "from appSession s inner join appCandidate ca on s.CandidateID=ca.CandidateID "+
                                    "inner join appCampaign cp on s.CampaignID=cp.CampaignID "+
                                    "inner join appCompany co on s.CompanyID=co.CompanyID "+
                                    "where (s.CompanyID={0:d} or {0:d}=-1)",nCompany,Helpers.SqlStatusCodes("s"));
        string strWhere = " and 1=2";
        if (!string.IsNullOrWhiteSpace(txtSearch.Text))
        {
            strWhere=string.Format(" and s.CandidateID in (select CandidateID from appCandidate where Contains(ca.SearchText,'{0}'))", Helpers.FixSearchString(txtSearch.Text));
        }
        strSql += strWhere;
        strSql += " order by s.Start desc";
        adapter.SelectCommand=new SqlCommand(strSql,conn);
        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);
        DataColumn[] PrimaryKeys = new DataColumn[1];
        PrimaryKeys[0] = dataTable.Columns["SessionID"];
        dataTable.TableName = "Details";
        dataTable.PrimaryKey = PrimaryKeys;
        dataset.Tables.Add(dataTable);
 
        string strSql2 = string.Format("SELECT SessionID, TicketID, FaultCategory, Status, AssignedTo  from appTicket where SessionID in (Select top 100 s.SessionID from appSession s inner join appCandidate ca on s.CandidateID=ca.CandidateID where (s.CompanyID={0:d} or {0:d}=-1) {1} order by s.Start desc)",nCompany, strWhere);
        adapter.SelectCommand = new SqlCommand(strSql2, conn);
        dataTable = new DataTable();
        adapter.Fill(dataTable);
        DataColumn[] ForeignKeys = new DataColumn[1];
        ForeignKeys[0] = dataTable.Columns["SessionID"];
        dataTable.TableName = "Detail";
        dataset.Tables.Add(dataTable);
        DataRelation SummaryDetail = new DataRelation("SummaryDetail", PrimaryKeys, ForeignKeys);
        dataset.Relations.Add(SummaryDetail);
    }
    finally
    {
        conn.Close();
    }
    HelpdeskGrid.DataSource = dataset;
<MasterTableView HeirarchyLoadMode="client" DataKeyNames="SessionID" ClientDataKeyNames="SessionID">
        <CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>
        <RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
        </EditFormSettings>
        <DetailTables >
            <telerik:GridTableView HierarchyLoadMode="Client"  DataKeyNames="SessionID" Name="TicketDetails">
             <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="SessionID" MasterKeyField="SessionID"></telerik:GridRelationFields>
                </ParentTableRelation>
                <Columns>
                    <telerik:GridBoundColumn HeaderText="TicketID" DataField="TicketID" UniqueName="TicketID">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Category" DataField="FaultCategory" UniqueName="FaultCategoryName">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Assigned To" DataField="AssignedTo" UniqueName="AssignedTo">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Status" DataField="Status" UniqueName="StatusID">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Options" UniqueName="Option" DataField="SessionID">
                        <ItemTemplate>
                            <telerik:RadButton AutoPostBack="false" runat="server" Text="Open Ticket" onclientclicked='onOpenTicket' CommandArgument='<%# DataBinder.Eval( Container, "DataItem.TicketID") %>' >
                            </telerik:RadButton>
                            </ItemTemplate>
                        <HeaderStyle Width="200px" />
                        <ItemStyle Width="200px" />
                    </telerik:GridTemplateColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Company" DataField="Company" UniqueName="company">
                <HeaderStyle Width="200px" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Campaign" DataField="Campaign" UniqueName="Campaign">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="First Name" DataField="FirstName" UniqueName="FirstName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Last Name" DataField="LastName" UniqueName="LastName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Email" DataField="Email" UniqueName="Email">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Start" DataField="Start" UniqueName="Start">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="Status" DataField="Status" UniqueName="Status">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="SessionID" Display="false" DataField="SessionID"
                UniqueName="SessionID">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="Options" UniqueName="Option" DataField="SessionID">
                <ItemTemplate>
                    <telerik:RadButton AutoPostBack="false" ID="btn_createSession" runat="server" Text="New Ticket"
                        OnClientClicked="onCreateTicket" CommandArgument='<%# DataBinder.Eval( Container, "DataItem.SessionID") %>'>
                    </telerik:RadButton>
                    <telerik:RadButton AutoPostBack="false" ID='Open' runat="server" Text="Open" Value="TestValue"
                        CommandArgument='<%# DataBinder.Eval( Container, "DataItem.SessionID") %>' OnClientClicked="onOpen">
                    </telerik:RadButton>
                </ItemTemplate>
                <HeaderStyle Width="200px" />
                <ItemStyle Width="200px" />
            </telerik:GridTemplateColumn>
        </Columns>
    
    </MasterTableView>
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
</telerik:RadGrid>

2 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 02 Aug 2013, 06:40 AM
Hi Aron,

I see that you are binding the data for detail table in NeedDataSource Event,please bind the detail table in DetailTableDataBind event.
Please see the below sample code snippet for example.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    AllowSorting="true" OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnNeedDataSource="RadGrid1_NeedDataSource">
    <MasterTableView DataKeyNames="OrderID" ClientDataKeyNames="OrderID" HierarchyLoadMode="Client">
        <DetailTables>
            <telerik:GridTableView HierarchyLoadMode="Client" DataKeyNames="OrderID" Name="TicketDetails">
                <ParentTableRelation>
                    <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID"></telerik:GridRelationFields>
                </ParentTableRelation>
                <Columns>
                    <telerik:GridBoundColumn HeaderText="UnitPrice" DataField="UnitPrice" UniqueName="UnitPrice">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn HeaderText="Quantity" DataField="Quantity" UniqueName="Quantity">
                    </telerik:GridBoundColumn>
                </Columns>
            </telerik:GridTableView>
        </DetailTables>
        <Columns>
            <telerik:GridBoundColumn HeaderText="OrderID" DataField="OrderID" UniqueName="OrderID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="ShipCity" DataField="ShipCity" UniqueName="ShipCity">
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
  {
      if (!e.IsFromDetailTable)
      {
          RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");
      }
  }
  
  protected void RadGrid1_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
  {
      GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
      switch (e.DetailTableView.Name)
      {
          case "TicketDetails":
              {
                  string OrderID = dataItem.GetDataKeyValue("OrderID").ToString();
                  e.DetailTableView.DataSource = GetDataTable("SELECT * FROM [Order Details] WHERE OrderID = '" + OrderID + "'");
                  break;
              }
      }
  }
  public DataTable GetDataTable(string query)
  {
      String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
      SqlConnection conn = new SqlConnection(ConnString);
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = new SqlCommand(query, conn);
      DataTable myDataTable = new DataTable();
      conn.Open();
      try
      {
          adapter.Fill(myDataTable);
      }
      finally
      {
          conn.Close();
      }
      return myDataTable;
  }

Have a look at this documentation on Hierarchical data-binding using DetailTableDataBind event.

Thanks,
Princy
0
Aaron
Top achievements
Rank 1
answered on 04 Aug 2013, 09:19 PM
Hey Princy

Thanks for your prompt reply. The particular solution that you offered would have meant that a query would have been required for each details table, and we desired to do them all in one swoop.

We made the following alterations in the ascx file
// altered this
    <MasterTableView DataKeyNames="SessionID" TableLayout="Fixed">
// altered this
 <telerik:GridTableView   BorderStyle="None" runat="server" DataMember="Details" DataKeyNames="TicketID" Name="TicketDetails" AllowPaging="false" ShowHeader="false" ShowFooter="false">

in the ascx.cs file
//after this line
HelpdeskGrid.DataSource = dataset;)
HelpdeskGrid.MasterTableView.HierarchyLoadMode = Telerik.Web.UI.GridChildLoadMode.Client; // I added this one

Making these alterations ensured that the details tables were populated and loaded returned to the web browser with the parent table.

Tags
Grid
Asked by
Aaron
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Aaron
Top achievements
Rank 1
Share this question
or