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

Problems with relationship between master table and detail table.

9 Answers 172 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Carlos Rodriguez
Top achievements
Rank 1
Carlos Rodriguez asked on 08 Apr 2011, 03:21 PM
Need to establish the relationship between the master table and detail tables. I am using "GridRelationFields" but because ajax error. When not using the "GridRelationFields" I click to expand the detail of a record and I get all the detailed tables of all records.

I mention that each detail table differ in the number of columns and their names, each table are aggregated in a programmatic way.
How I can successfully establish the relationship between the master table and detail table?

I noted the examples online and do not help me.
Annex the image of the error in AJAX when using "GridRelationFields. "

This is my code to build the tables in detail:

DataTable datTabDatosDetalle = new DataTable();
                    if (ViewState["OrdCompraProdDetallesSub" + IdOrdenCompra + Skucode] == null)
                    {
                        datTabDatosDetalle = CCSMovimientoProductos.BuscarProdDetOrdenCompra_CEDIS(IdOrdenCompra, Skucode).Tables[0];
                        ViewState.Add("OrdCompraProdDetallesSub" + IdOrdenCompra + Skucode, datTabDatosDetalle);
                    }
                    else
                        datTabDatosDetalle = (DataTable)ViewState["OrdCompraProdDetallesSub" + IdOrdenCompra + Skucode];
  
                    Telerik.Web.UI.GridTableView detailTable = new Telerik.Web.UI.GridTableView(radGriOrdComDetalles);
  
                    Telerik.Web.UI.GridRelationFields relationFields_Id = new Telerik.Web.UI.GridRelationFields();
                    relationFields_Id.MasterKeyField = "Id";
                    relationFields_Id.DetailKeyField = "Id";
                    detailTable.ParentTableRelation.Add(relationFields_Id);
  
                    Telerik.Web.UI.GridRelationFields relationFields_Skucode = new Telerik.Web.UI.GridRelationFields();
                    detailTable.ParentTableRelation.Add(relationFields_Skucode);
                    relationFields_Skucode.MasterKeyField = "prodSkucode";
                    relationFields_Skucode.DetailKeyField = "prodSkucode";
                      
                    radGriOrdComDetalles.MasterTableView.DetailTables.Add(detailTable);
  
                    detailTable.Name = "tabDetail" + IdOrdenCompra + Skucode;
                    detailTable.DataKeyNames = new string[] {"prodSkucode" };
                    detailTable.EnableViewState = true;
                    detailTable.NoDetailRecordsText = "";
                    detailTable.Caption = "Detalle del producto Sku " + Skucode;
                    detailTable.EditMode = Telerik.Web.UI.GridEditMode.InPlace;
  
                    foreach (DataColumn dc in datTabDatosDetalle.Columns)
                    {
                        Telerik.Web.UI.GridBoundColumn campo = new Telerik.Web.UI.GridBoundColumn();
                        detailTable.Columns.Add(campo);
                        campo.UniqueName = "col" + dc.ColumnName;
                        campo.HeaderText = "[ " + dc.ColumnName + " ]";
                        campo.HeaderStyle.Font.Bold = true;
                        campo.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                        campo.DataField = dc.ColumnName;
  
                        if (dc.ColumnName == "Id")
                        {
                            campo.Visible = false;
                        }
                        if (dc.ColumnName == "prodSkucode")
                        {
                            campo.Visible = false;
                        }
                        if (dc.ColumnName == "Talla")
                        {
                            campo.ReadOnly = true;
                        }
                    }
  
                    Telerik.Web.UI.GridEditCommandColumn campoEdit = new Telerik.Web.UI.GridEditCommandColumn();
                    detailTable.Columns.Add(campoEdit);
                    campoEdit.UniqueName = "colEdit";
                    campoEdit.ButtonType = Telerik.Web.UI.GridButtonColumnType.LinkButton;
                    campoEdit.EditText = "Editar";
                    campoEdit.UpdateText = "Actualizar";
                    campoEdit.CancelText = "Cancelar";
  
                    detailTable.DataSource = datTabDatosDetalle;
                    detailTable.Rebind();
                    radGriOrdComDetalles.Rebind();

9 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 08 Apr 2011, 04:05 PM
Hi Carlos,

In order to achieve this functionality I suggest you to examine the following help article and demo:
Hierarchical data-binding using DetailTableDataBind event
Hierarchy with DetailTableDataBind event demo

I hope this gets you started properly.

All the best,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Carlos Rodriguez
Top achievements
Rank 1
answered on 08 Apr 2011, 05:43 PM
Pavlina Hello!
Thank you very much for your response.

I changed the code, now I'm using the DetailTableDataBind event in the ASPX code, I have declared a detail table, which, through the event DetailTableDataBind change its structure.

It works well, but by expanding the detail, it shows the correct structure until the second time it expands, because the first time, shows the previous structure.

Any suggestions to show the first time the new structure?
Here you annex the aspx code and C #.

==== ASPX ==================================================================================================
<Telerik:RadGrid ID="radGriOrdComDetalles" runat="server" Width="980" AutoGenerateColumns="false"
                             AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="false">
                                <MasterTableView DataKeyNames="Id, prodSkucode" CommandItemDisplay="None"
                                 EditMode="InPlace" NoDetailRecordsText =" " NoMasterRecordsText="">
                                    <Columns>
                                        <Telerik:GridBoundColumn UniqueName="colIdOrdCompra" HeaderText="Id orden de compra" DataField="Id" HeaderStyle-Width="130px"/>
                                        <Telerik:GridBoundColumn UniqueName="colSku" HeaderText="SKU" DataField="prodSkucode" HeaderStyle-Width="160px"/>
                                        <Telerik:GridBoundColumn UniqueName="colDescripcion" HeaderText="Descripcion" DataField="prodDescripcion" HeaderStyle-Width="300px"/>                               
                                        <Telerik:GridBoundColumn UniqueName="colDepartamento" HeaderText="Departamento" DataField="depaDescripcion" HeaderStyle-Width="180px"/>                     
                                    </Columns>
                                    <DetailTables>
                                        <Telerik:GridTableView Name="gridProductoDetalles" DataKeyNames="Id, prodSkucode" EditMode="InPlace" NoDetailRecordsText="" NoMasterRecordsText=""
                                          GridLines="Vertical">
                                        </Telerik:GridTableView>
                                    </DetailTables>
                                </MasterTableView>
                            </Telerik:RadGrid>


==== CODE BEHIND ===========================================================================================
void radGriOrdComDetalles_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            try
            {
                if (e.DetailTableView.Name == "gridProductoDetalles")
                {
                    Telerik.Web.UI.GridDataItem parentItem = e.DetailTableView.ParentItem;
                    strSkucode = parentItem.Cells[3].Text;
                    strIdOrdCompra = parentItem.Cells[2].Text;
  
                    DataTable datTabDatosDetalle = new DataTable();
                    if (ViewState["OrdCompraProdDetallesSub" + strIdOrdCompra + strSkucode] == null)
                        datTabDatosDetalle = CCSMovimientoProductos.BuscarProdDetOrdenCompra_CEDIS(strIdOrdCompra, strSkucode).Tables[0];
                    else
                        datTabDatosDetalle = (DataTable)ViewState["OrdCompraProdDetallesSub" + strIdOrdCompra + strSkucode];
                        //e.DetailTableView.DataSource = datTabDatosDetalle;
  
                    // SE AGREGA EN EL VIEWSTATE LA REGLETA CORRESPONDIENTE AL PRODUCTO
  
                    if (ViewState["OrdCompraProdDetallesSub" + strIdOrdCompra + strSkucode] == null)
                        ViewState.Add("OrdCompraProdDetallesSub" + strIdOrdCompra + strSkucode, datTabDatosDetalle);
  
                    // SE AGREGAN LAS COLUMNAS CORRESPONDIENTES.
  
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].Columns.Clear();
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].Rebind();
  
                    Telerik.Web.UI.GridBoundColumn campo;
                    foreach (DataColumn dc in datTabDatosDetalle.Columns)
                    {
                        campo = new Telerik.Web.UI.GridBoundColumn();
                        radGriOrdComDetalles.MasterTableView.DetailTables[0].Columns.Add(campo);
  
                        campo.UniqueName = "col" + dc.ColumnName;
                        campo.HeaderText = "[ " + dc.ColumnName + " ]";
                        campo.HeaderStyle.Font.Bold = true;
                        campo.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                        campo.DataField = dc.ColumnName;
  
                        if (dc.ColumnName == "Talla")
                            campo.ReadOnly = true;
                    }
  
                    Telerik.Web.UI.GridEditCommandColumn campoEdit = new Telerik.Web.UI.GridEditCommandColumn();
                    campoEdit.UniqueName = "colEdit";
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].Columns.Add(campoEdit);
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].DataSource = datTabDatosDetalle;
  
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].Columns[0].Visible = false;
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].Columns[1].Visible = false;
  
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].CommandItemDisplay = Telerik.Web.UI.GridCommandItemDisplay.Top;
                    radGriOrdComDetalles.MasterTableView.DetailTables[0].EditMode = Telerik.Web.UI.GridEditMode.InPlace;
                }
            }
            catch (Exception ex)
            {
            }
        }


Greetings!
0
Accepted
Pavlina
Telerik team
answered on 13 Apr 2011, 04:57 PM
Hi Carlos,

If you want to build your hierarchy presentation from related tables in dataset, you should wire the NeedDataSource event to assign the source for the master table and the DetailTableDataBind event to build your detail tables. Review the online example of RadGrid linked below for further info:

http://www.telerik.com/demos/aspnet/Grid/Examples/Programming/DetailTableDataBind/DefaultCS.aspx

Regards,
Pavlina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Carlos Rodriguez
Top achievements
Rank 1
answered on 13 Apr 2011, 06:56 PM
Pavlina thank you very much!

In the method NeedDataSource charge data as it is in the example. Thank you very much ^ _ ^.
0
Carlos Rodriguez
Top achievements
Rank 1
answered on 13 Apr 2011, 09:50 PM
I have a question. I made the code as it is in the example, but I added a column of type "GridEditCommandColumn", when clicking on "Edit" disappears every detail, so I set: e.DetailTableView.EditMode = Telerik.Web. UI.GridEditMode.InPlace, but it is disappearing.
0
Pavlina
Telerik team
answered on 14 Apr 2011, 09:58 PM
Hi Carlos,

Can you please send me your grid declaration/programmatic binding code so I could have a better look and see what could have gone wrong.

Kind regards,
Pavlina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

0
Carlos Rodriguez
Top achievements
Rank 1
answered on 14 Apr 2011, 10:40 PM
ASPX CODE ===========================

<Telerik:RadGrid ID="radGriOrdComDetalles" runat="server" Width="980" AutoGenerateColumns="false"
                             AllowAutomaticDeletes="false" AllowAutomaticInserts="false" AllowAutomaticUpdates="false">
                                <MasterTableView DataKeyNames="Id, prodSkucode" CommandItemDisplay="None"
                                 EditMode="InPlace" NoDetailRecordsText =" " NoMasterRecordsText="" CommandItemSettings-ShowRefreshButton="false"
                                 CommandItemSettings-ShowAddNewRecordButton="false" >
                                    <DetailTables>
                                        <Telerik:GridTableView Name="gridProductoDetalles" DataKeyNames="Id, prodSkucode" EditMode="InPlace" NoDetailRecordsText="Sin detalle" NoMasterRecordsText="Sin registros"
                                          GridLines="Vertical">
                                        </Telerik:GridTableView>
                                    </DetailTables>
                                    <Columns>
                                        <Telerik:GridBoundColumn UniqueName="colIdOrdCompra" HeaderText="Id orden de compra" DataField="Id" HeaderStyle-Width="130px"/>
                                        <Telerik:GridBoundColumn UniqueName="colSku" HeaderText="SKU" DataField="prodSkucode" HeaderStyle-Width="160px"/>
                                        <Telerik:GridBoundColumn UniqueName="colDescripcion" HeaderText="Descripcion" DataField="prodDescripcion" HeaderStyle-Width="300px"/>                               
                                        <Telerik:GridBoundColumn UniqueName="colDepartamento" HeaderText="Departamento" DataField="depaDescripcion" HeaderStyle-Width="180px"/>                     
                                    </Columns>
                                </MasterTableView>   
                            </Telerik:RadGrid>


C# CODE ================================

void radGriOrdComDetalles_DetailTableDataBind(object sender, Telerik.Web.UI.GridDetailTableDataBindEventArgs e)
        {
            try
            {
                Telerik.Web.UI.GridDataItem parentItem = e.DetailTableView.ParentItem;
                strSkucode = parentItem.Cells[3].Text;
                strIdOrdCompra = parentItem.Cells[2].Text;
  
                DataTable datTabDatosDetalle = new DataTable();
                if (ViewState["OrdCompraProdDetallesSub" + strIdOrdCompra + strSkucode] == null)
                    datTabDatosDetalle = CCSMovimientoProductos.BuscarProdDetOrdenCompra_CEDIS(strIdOrdCompra, strSkucode).Tables[0];
                else
                    datTabDatosDetalle = (DataTable)ViewState["OrdCompraProdDetallesSub" + strIdOrdCompra + strSkucode];
  
                // SE AGREGAN LAS COLUMNAS CORRESPONDIENTES.
  
                Telerik.Web.UI.GridBoundColumn campo;
                foreach (DataColumn dc in datTabDatosDetalle.Columns)
                {
                    campo = new Telerik.Web.UI.GridBoundColumn();
                    e.DetailTableView.Columns.Add(campo);
  
                    campo.UniqueName = "col" + dc.ColumnName;
                    campo.HeaderText = "[ " + dc.ColumnName + " ]";
                    campo.HeaderStyle.Font.Bold = true;
                    campo.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                    campo.DataField = dc.ColumnName;
  
                    if (dc.ColumnName == "Talla")
                        campo.ReadOnly = true;
                }
  
                Telerik.Web.UI.GridEditCommandColumn campoEdit = new Telerik.Web.UI.GridEditCommandColumn();
                campoEdit.UniqueName = "colEdit";
                campoEdit.ButtonType = Telerik.Web.UI.GridButtonColumnType.LinkButton;
                campoEdit.EditText = "Editar";
                campoEdit.UpdateText = "Actualizar";
                campoEdit.CancelText = "Cancelar";
                e.DetailTableView.Columns.Add(campoEdit);  
  
                // SE AGREGAN LOS DATOS FINALES AL SOURCE DEL GRID.
  
                e.DetailTableView.Columns[0].Visible = false;
                e.DetailTableView.Columns[1].Visible = false;
                e.DetailTableView.EditMode = Telerik.Web.UI.GridEditMode.InPlace;
                e.DetailTableView.CommandItemDisplay = Telerik.Web.UI.GridCommandItemDisplay.Top;  
                e.DetailTableView.DataSource = datTabDatosDetalle;
                                    }
                                    catch(Exception ex)
                                    {    }
                       }    
0
Carlos Rodriguez
Top achievements
Rank 1
answered on 15 Apr 2011, 04:12 PM
I've been debugging the code, I click Edit and run only the methods Page_Load Page_Init, and PreRender, end of run and disappear data detail table.

Could be a clue?

Annex images

0
Pavlina
Telerik team
answered on 18 Apr 2011, 12:09 PM
Hi Carlos,

I have noticed that my colleague Veli have already answered to the other forum thread that you have opened on the same topic. To avoid duplicate posts I would suggest that you continue your communication there.

Kind regards,
Pavlina
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
Carlos Rodriguez
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Carlos Rodriguez
Top achievements
Rank 1
Share this question
or