Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
265
Hierarchical grid needs to call .DataBind() twice in order to display +/-
posted

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?

Parents
  • 3732
    posted

    Hi,

    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();

                    row["ParentID"] = "ParentID -- " + i.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

     

Reply Children
No Data