Hello
How to add bands and colums to WebHierarchicalDataGrid from DataSet using C#. I have to set Auto generate bands and columns to false because I need to interact with the columns on the server.
Hi gtarek,
Please refer to the following sample illustratiing adding column dynamically to the WebDataGrid. Note that a similar principle applies to the hierarchical grid - bands are added to the grid, and columns are added to those bands:
http://samples.infragistics.com/aspnet/Samples/WebDataGrid/Data/Dynamic-Columns/Default.aspx?cn=data-grid&sid=9d376e09-cf7e-414f-a2f0-329f0606c1be
Once a band or column is defined, you can add it to the grid using something similar to:
WebHierarchicalDataGrid1.Bands.Add(newBand);WebHierarchicalDataGrid1.Bands["bandKey"].Columns.Add(newColumn);
Please contact me if you require further assistance.
Best Regards,
Petar IvanovDeveloper Support EngineerInfragistics, Inc.http://www.infragistics.com/support
Hi Petar,
I tried to add columns with this code but I'm getting an empty WebHierarchicalDataGrid, what could be the problem please ?
if (!Page.IsPostBack) {
{
); dsModele.Tables.Add(
dsModele.Tables.Add(
); dsModele.Tables[0].Columns.Add(
dsModele.Tables[0].Columns.Add(
); dsModele.Tables[0].Rows.Add(1,
dsModele.Tables[0].Rows.Add(1,
); dsModele.Tables[0].Rows.Add(2,
dsModele.Tables[0].Rows.Add(2,
); dsModele.Tables[0].Rows.Add(3,
dsModele.Tables[0].Rows.Add(3,
); dsModele.Tables[0].Rows.Add(4,
dsModele.Tables[0].Rows.Add(4,
); dsModele.Tables[0].Rows.Add(5,
dsModele.Tables[0].Rows.Add(5,
);
// Create band
(); childBand.DataKeyFields =
childBand.DataKeyFields =
;
// Set the key from the data source
childBand.Key =
; childBand.DataMember =
childBand.DataMember =
.WebHierarchicalDataGrid1.Bands.Add(childBand);
// Define column for the child band
boundField =
); boundField.Key =
boundField.Key =
; boundField.Header.Text =
boundField.Header.Text =
; boundField.DataFieldName =
boundField.DataFieldName =
].Columns.Add(boundField);
].DefaultView; dv.Sort =
dv.Sort =
//add a new table called "Categories" with the distinct Country column values from the Customers table
dsModele.Tables.Add(dv.ToTable(
));
//Set up a data relation for the Customers table with the Countries table
//using the country column values to match records in a parent-child relationship
, dsModele.Tables[
dsModele.Tables[
], dsModele.Tables[
]); dsModele.Relations.Add(rel);
dsModele.Relations.Add(rel);
// Set the table's primary key field
[] { dsModele.Tables[
] };
// Store the data in Session state
] = dsModele; }
}
// Retrieve data from Session state
.WebHierarchicalDataGrid1.DataSource = ds;
.WebHierarchicalDataGrid1.DataBind();
Please do not hesitate to contact me if you have further questions.
Hi Petar, Im having a problem with a WebHierarchicalDataGrid. When I populate the control without setting the columns, it works fine, but when I stablish wich columns my grid and its childs grids are going to show, there is my problem. It shows the grid but when I click the CollapseButton, it doesn't do anything. I've already set the DataFieldKeys (in both cases the same) but still. Is there anything else I have to do when I set the columns?
Thanks for your timeJesus
Hello Jesus,
When manually defining WHDG's columns and bands you need to set the DataMember property for each band in addition to the DataKeyField property for all the bands. An example of how the markup for a WHDG looks like may be seen in our documentation:
http://help.infragistics.com/NetAdvantage/ASPNET/2011.2/CLR4.0/?page=WebHierarchicalDataGrid_Data_Binding.html
Please let me know if this helps.
Best Regards,Petar IvanovDeveloper Support EngineerInfragistics, Inc.http://www.infragistics.com/support
Hello Petar, thanks for your reply. Let me explain you how Im populating the WHDG:
First, I use IList<myEntity>I retrieve data from my database and store it in my generic List. One of the properties of this entity is a another IList where Im storing the detail information for each record. For example:
IList<Parent>IDParent,Name,Address,IList<Children>
IList<Children>IDChild,IDParent,Name,Age
I set up the columns and bands by using the wizard of the WHDG (not in code behind as the example you provided me shows). The I program a method to fill the WHDG like this:
IList<Parent> varListHeader = methodToRetrieveAllDataFromDB()nameWHDG.DataSource = varListHeadernameWHDG.DataBind()In the aspx I have this for the WebHierarchicalDataGrid:
<ig:WebHierarchicalDataGrid ID="dgLista" runat="server" AutoGenerateColumns="False" DataKeyFields="IDParent" Height="350px" Width="836px"> <Columns> .................
I set up the DataKeyFields with the IDParent and in the Band I have something like this
<Bands> <ig:Band AutoGenerateColumns="False" DataMember="varListHeader" Key="varListHeader" DataKeyFields="IDParent"> <Columns> <ig:BoundDataField DataFieldName="IDChild" Key="IDChild"> <Header Text="Id of the Child" /> </ig:BoundDataField> .................Do I have to do anything in the postback event?
Thanks in advance.Jesus
Hi Jesus,
Thank you for your reply.
The root band of WHDG is essentially defined inside the <ig:WebHierarchicalDataGrid> tag. Hence, you should set the DataKeyFields and DataMember in that tag:
<ig:WebHierarchicalDataGrid ID="dgLista" runat="server" AutoGenerateColumns="False" DataKeyFields="IDParent" DataMember="varListHeader" Height="350px" Width="836px">
The child band hierarchy is defined inside the <ig:Band> tag. Therefore your child band definition should be similar to:
<ig:Band AutoGenerateColumns="False" DataMember="varListChild" Key="varListChild" DataKeyFields="IDChild">
Note that the columns for the root band are defined outside the Bands tag.
Please note that when binding the grid to the datasource from the code-behind, you need to ensure that the grid is rebound on each postback (if EnableDataViewState is not used). That may be done for instance on Page_Load:
WebDataGrid1.DataSource = ds; WebDataGrid1.DataBind();
WebDataGrid1.DataSource = ds;
WebDataGrid1.DataBind();
Please let me know if this is helpful.