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

grid with three level details

2 Answers 125 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sofiene
Top achievements
Rank 1
Sofiene asked on 05 Apr 2012, 08:35 AM
hi ,
I have a Radgrid that work correctly with a 2 level details ,i have to add a "publipersonne" detail table to my  personne  table detail ()case n° 2 ) , the first case for first level is OK and the detail table is showed , the second case worked too and the datasource is affected ( i can see it in debugging ) but the web page show an error  :

There was a problem extracting DataKeyValues from the DataSource. Please ensure that DataKeyNames are specified correctly and all fields specified exist in the DataSource.
my DataKeyNames is in the datasource and affected .
thank's for reply .



 

 

protected void GridPJ_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)

 

{

 

 

GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;

 

 

 

switch (e.DetailTableView.Name)

 

{

 

 

 

case "Personne":

 

{

 

 

 

string numcompte = dataItem.GetDataKeyValue("NumCompte").ToString();

 

 

 

//

 

e.DetailTableView.DataSource =

 

IHMProxy.SelectPersonneUSData(new CSSearchPersonne

 

{

NumCompte = numcompte

});

 

 

 

break;

 


}



 

 

case "publiPersonne" :

 

 

 

string numPersonne = dataItem.GetDataKeyValue("NUM_PERS").ToString();

 

 

 

//

 

 

 

int i = e.DetailTableView.DetailTables.Count;

 

 

e.DetailTableView.DataSource=

 

IHMProxy.selectPubliPersonne(numPersonne);

 

 

 

 

 

break;

 


}

2 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 05 Apr 2012, 10:06 AM
Hi Sofiene,

I tried the three level hierarchy Programmatically and is working fine at my end. Make sure that you are Specifying DataKeyNames for all the three levels.Please take a look into the following code.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False"
   AllowMultiRowSelection="False"   OnDetailTableDataBind="RadGrid1_DetailTableDataBind"
    OnNeedDataSource="RadGrid1_NeedDataSource" OnPreRender="RadGrid1_PreRender">
    <MasterTableView  DataKeyNames="CustomerID" AllowMultiColumnSorting="True">
        <DetailTables>
            <telerik:GridTableView DataKeyNames="OrderID" Name="Orders" Width="100%" >
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="OrderID" Name="OrderDetails" Width="100%">
                        <Columns>
                            <telerik:GridBoundColumn SortExpression="UnitPrice" HeaderText="Unit Price" HeaderButtonType="TextButton"
                                DataField="UnitPrice">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="Quantity" HeaderText="Quantity" HeaderButtonType="TextButton"
                                DataField="Quantity">
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn SortExpression="Discount" HeaderText="Discount" HeaderButtonType="TextButton"
                                DataField="Discount">
                            </telerik:GridBoundColumn>
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                       <Columns>
                           <telerik:GridBoundColumn SortExpression="OrderID" HeaderText="OrderID" HeaderButtonType="TextButton"
                               DataField="OrderID">
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn SortExpression="OrderDate" HeaderText="Date Ordered" HeaderButtonType="TextButton"
                               DataField="OrderDate" UniqueName="OrderDate" DataFormatString="{0:D}">
                           </telerik:GridBoundColumn>
                           <telerik:GridBoundColumn SortExpression="Freight" HeaderText="Freight" HeaderButtonType="TextButton"
                            DataField="Freight" UniqueName="Freight">
                       </telerik:GridBoundColumn>
                       </Columns>
                     </telerik:GridTableView>
                </DetailTables>
                       <Columns>
                       <telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" HeaderButtonType="TextButton"
                        DataField="CustomerID">
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn SortExpression="ContactName" HeaderText="Contact Name" HeaderButtonType="TextButton"
                        DataField="ContactName">
                       </telerik:GridBoundColumn>
                       <telerik:GridBoundColumn SortExpression="CompanyName" HeaderText="Company" HeaderButtonType="TextButton"
                        DataField="CompanyName">
                       </telerik:GridBoundColumn>
                </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    if (!e.IsFromDetailTable)
    {
        RadGrid1.DataSource = GetDataTable("SELECT * FROM Customers");
    }
}
 
protected void RadGrid1_DetailTableDataBind(object source, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
{
    GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
    switch (e.DetailTableView.Name)
    {
        case "Orders":
            {
                string CustomerID = dataItem.GetDataKeyValue("CustomerID").ToString();
                e.DetailTableView.DataSource = GetDataTable("SELECT * FROM Orders WHERE CustomerID = '" + CustomerID + "'");
                break;
            }
 
        case "OrderDetails":
            {
                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["NorthwindConnectionString3"].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;
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
       RadGrid1.MasterTableView.Items[0].Expanded = true;
       RadGrid1.MasterTableView.Items[0].ChildItem.NestedTableViews[0].Items[0].Expanded = true;
    }
}

Thanks,
Shinu.
0
Sofiene
Top achievements
Rank 1
answered on 05 Apr 2012, 01:56 PM
hi Shinu
thank you for your reply .
we have the same code structure, but it don't worked in my case because telerik is not able to read a datakey propriety from datasource object if get;set is not defined .
try with a data source like this
public class Order
{
    public int OrderId;
  
}
after adding get;set to my class ( datasource object ) , the grid is being working  .


PS : i now that my english is not good , if you not anderstand my post , say it and i will use an traductor .

@+
Tags
Grid
Asked by
Sofiene
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Sofiene
Top achievements
Rank 1
Share this question
or