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
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
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!
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.
In the method NeedDataSource charge data as it is in the example. Thank you very much ^ _ ^.
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.
<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)
{ }
}
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.