Skip to content

Replies

0
Gafsi
Gafsi answered on Oct 3, 2011 2:17 PM

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 = new DataSet("Model"

);

dsModele.Tables.Add(

 

 

"Model"

);

dsModele.Tables[0].Columns.Add(

 

 

"ID"

);

dsModele.Tables[0].Columns.Add(

 

 

"Category"

);

dsModele.Tables[0].Columns.Add(

 

 

"Titre"

);

dsModele.Tables[0].Columns.Add(

 

 

"valeur"

);

dsModele.Tables[0].Rows.Add(1,

 

 

"Category1", "Titre11", "Valeur11"

);

dsModele.Tables[0].Rows.Add(2,

 

 

"Category1", "Titre12", "Valeur12"

);

dsModele.Tables[0].Rows.Add(3,

 

 

"Category2", "Titre21", "Valeur21"

);

dsModele.Tables[0].Rows.Add(4,

 

 

"Category3", "Titre31", "Valeur31"

);

dsModele.Tables[0].Rows.Add(5,

 

 

"Category3", "Titre32", "Valeur32"

);

 

 

 

 

// Create band

 

 

 

Band childBand = new Band

();

childBand.DataKeyFields =

 

 

"ID"

;

 

 

 

// Set the key from the data source

childBand.Key =

 

 

"Model"

;

childBand.DataMember =

 

 

"Model"

;

 

 

 

this

.WebHierarchicalDataGrid1.Bands.Add(childBand);

 

 

 

 

BoundDataField boundField = new BoundDataField(true

);

 

 

 

// Define column for the child band

boundField =

 

 

new BoundDataField(true

);

boundField.Key =

 

 

"ID"

;

boundField.Header.Text =

 

 

"ID"

;

boundField.DataFieldName =

 

 

"ID"

;

 

 

 

this.WebHierarchicalDataGrid1.Bands["Model"

].Columns.Add(boundField);

 

 

 

// Define column for the child band

boundField =

 

 

new BoundDataField(true

);

boundField.Key =

 

 

"Category"

;

boundField.Header.Text =

 

 

"Category"

;

boundField.DataFieldName =

 

 

"Category"

;

 

 

 

this.WebHierarchicalDataGrid1.Bands["Model"

].Columns.Add(boundField);

 

 

 

// Define column for the child band

boundField =

 

 

new BoundDataField(true

);

boundField.Key =

 

 

"Titre"

;

boundField.Header.Text =

 

 

"Titre"

;

boundField.DataFieldName =

 

 

"Titre"

;

 

 

 

this.WebHierarchicalDataGrid1.Bands["Model"

].Columns.Add(boundField);

 

 

 

// Define column for the child band

boundField =

 

 

new BoundDataField(true

);

boundField.Key =

 

 

"valeur"

;

boundField.Header.Text =

 

 

"valeur"

;

boundField.DataFieldName =

 

 

"valeur"

;

 

 

 

this.WebHierarchicalDataGrid1.Bands["Model"

].Columns.Add(boundField);

 

 

 

 

DataView dv = dsModele.Tables["Model"

].DefaultView;

dv.Sort =

 

 

"Category ASC"

;

 

 

 

//add a new table called "Categories" with the distinct Country column values from the Customers table

dsModele.Tables.Add(dv.ToTable(

 

 

"Categories", true, "Category"

));

 

 

 

//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

 

 

 

DataRelation rel = new DataRelation("CategoryModel"

,

dsModele.Tables[

 

 

"Categories"].Columns["Category"

],

dsModele.Tables[

 

 

"Model"].Columns["Category"

]);

dsModele.Relations.Add(rel);

 

 

 

// Set the table's primary key field

dsModele.Tables[

 

 

"Categories"].PrimaryKey = new DataColumn

[] {

dsModele.Tables[

 

 

"Categories"].Columns["Category"

] };

 

 

 

// Store the data in Session state

 

 

 

this.Session["CategoryModel"

] = dsModele;

}

 

 

 

// Retrieve data from Session state

 

 

 

DataSet ds = this.Session["CategoryModel"] as DataSet

;

 

 

 

this

.WebHierarchicalDataGrid1.DataSource = ds;

 

 

 

this.WebHierarchicalDataGrid1.DataMember = "Categories"

;

 

 

 

this.WebHierarchicalDataGrid1.DataKeyFields = "Category"

;

 

 

 

this

.WebHierarchicalDataGrid1.DataBind();