Hi,
I'm programmatically using the web grid to bind to a datasource that has 2 tables that have a relationship to create a hierarchical grid. This works fine however I need to call DataBind() twice otherwise the second band is null. Is this a bug?
I don't think there is any reason to call the DataBind() twice for the Grid to display hierarchical data. How and where you are defining the data source of the WebGrid? I'm not sure what kind of UltraWebGrid events are you handling, but I would suggest you to do it as follows.
1) Write a method which returns a DataSet, similar to this:
Private DataSet GetDataSet()
{
DataSet parentChildSet = new DataSet ();
DataTable parentTable = new DataTable();
DataTable childTable = new DataTable();
DataRow row;
parentTable.Columns.Add("ParentID");
parentTable.Columns.Add("SampleFirstName_Parent");
parentTable.Columns.Add("SampleLastName_Parent");
childTable.Columns.Add("ChildID" );
childTable.Columns.Add("SampleFirstName_Child");
childTable.Columns.Add("SampleLastName_Child");
childTable.Columns.Add("ParentID");
for (inti = 0; i < 10; i++)
row = parentTable.NewRow();
row["ParentID"] = "ParentID -- " + i.ToString();
row["SampleFirstName_Parent"] = "First Name_Parent -- " + i.ToString();
row["SampleLastName_Parent"] = "Last Name_Parent -- " + i.ToString();
parentTable.Rows.Add(row);
for (int j = 0; j < 5; j++)
row = childTable.NewRow();
row["ChildID"] = "ChildID -- " + j.ToString();
row["SampleFirstName_Child"] = "First Name _ Child -- " + j.ToString();
row["SampleLastName_Child"] = "Last Name _ Child -- " + j.ToString();
childTable.Rows.Add(row);
}
parentChildSet.Tables.Add(parentTable);
parentChildSet.Tables.Add(childTable);
parentChildSet.Relations.Add(parentChildSet.Tables[0].Columns["ParentID"], parentChildSet.Tables[1].Columns["ParentID"]);
return parentChildSet;
2) Handle the InitializeDataSource event and set the data source of the Grid inside this event:
protected void UltraWebGrid1_InitializeDataSource(object sender, Infragistics.WebUI.UltraWebGrid.UltraGridEventArgs e)
this.UltraWebGrid1.DataSource = GetDataSet();
This way you don't have to call the DataBind method explicitly. Handling this event takes care of it.
If you define the DataSource anywhere else than the InitializeDataSource, then you have to call the DataBind method.
3) Handle the InitializeLayout event and set the layout of the Grid
protected void UltraWebGrid1_InitializeLayout(object sender, Infragistics.WebUI.UltraWebGrid.LayoutEventArgs
e)
e.Layout.AddNewRowDefault.Visible = Infragistics.WebUI.UltraWebGrid.
AddNewRowVisible
.Yes;
This InitializeLayout event gets fired after InitializeDataSource event and whenever DataBind method is called.
I hope this helps.
Thanks
Here is my code below, i replicated the bug with your code on my machine, testing against Infragistics.Web.Ultragrid.v8.3.dll
partial class WebForm1 : System.Web.UI.Page
);
e) {
form1.Controls.Add(_webGrid);
_webGrid.InitializeDataSource +=
(_webGrid_InitializeDataSource);
_webGrid.InitializeLayout +=
(_webGrid_InitializeLayout);
// You didnt put this line on in your example hence it would have never displayed Hierarchy.
e.Layout.ViewType =
.Hierarchical;
_webGrid.DataSource = GetDataSet();
// Need to call this line below otherwise grid doesnt display in Hierarchy.
// Note: this will still display the grid just without the +/- buttons.
// According to what you said, i should never have to call this line, this is where the bug lies.
_webGrid.DataBind();
GetDataSet() {
;
) {
parentChildSet =
();
row;
parentTable.Columns.Add(
childTable.Columns.Add(
i = 0; i < 10; i++) {
row[
+ i.ToString();
j = 0; j < 5; j++) {
+ j.ToString();
parentChildSet.Relations.Add(parentChildSet.Tables[0].Columns[
]);
parentChildSet = Session[
parentChildSet;