We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

expand root node after binding data unsuccess

Hi


I checked the current forum to help me, but without success


I am using WinForms Essential version 20.1.0.47.


I try to expand root node after binding data with the method "Binding to Self-Referencing Data".


1rst step I bind data :


My object class is :

    public partial class Category
    {
        public Int64 pkCategory { get; set; }
        public String Name { get; set; }
        public Int64 fkCategoryParent { get; set; }
        public String NumeroDelcampe { get; set; }
    }


the datasource binding configuration is :

this.treeViewAdvCategories.SelfRelationRootValue = "0";
this.treeViewAdvCategories.DisplayMember = "Name";
this.treeViewAdvCategories.ParentMember = "fkCategoryParent";
this.treeViewAdvCategories.ChildMember = "pkCategory";
this.treeViewAdvCategories.ValueMember = "pkCategory";

At this step all works right.


My current source code :

// get datatable
using (ProductsDB productsDB = new ProductsDB())
{
dataTableCategories =
productsDB.GetAll<Category>()
.OrderBy(x => x.Name)
.ToDataTable();
}


// configure "Binding to Self-Referencing Data" Synfusion TreeViewAdv
this.treeViewAdvCategories.SelfRelationRootValue = "0";
this.treeViewAdvCategories.DisplayMember = "Name";
this.treeViewAdvCategories.ParentMember = "fkCategoryParent";
this.treeViewAdvCategories.ChildMember = "pkCategory";
this.treeViewAdvCategories.ValueMember = "pkCategory";


// rename the root element
foreach (DataRow row in dataTableCategories.Rows)
{
if (row[this.treeViewAdvCategories.ParentMember].ToString() == "0")
{
row["Name"] = "<Root>";
}
}


// bind data
this.treeViewAdvCategories.DataSource = dataTableCategories;


2nd step : Now, I try to expand the root node.


My source code becomes

// get datatable
using (ProductsDB productsDB = new ProductsDB())
{
dataTableCategories =
productsDB.GetAll<Category>()
.OrderBy(x => x.Name)
.ToDataTable();
}


// configure "Binding to Self-Referencing Data" Synfusion TreeViewAdv
this.treeViewAdvCategories.SelfRelationRootValue = "0";
this.treeViewAdvCategories.DisplayMember = "Name";
this.treeViewAdvCategories.ParentMember = "fkCategoryParent";
this.treeViewAdvCategories.ChildMember = "pkCategory";
this.treeViewAdvCategories.ValueMember = "pkCategory";


// rename the root element
foreach (DataRow row in dataTableCategories.Rows)
{
if (row[this.treeViewAdvCategories.ParentMember].ToString() == "0")
{
row["Name"] = "<Root>";
}
}


// bind data
this.treeViewAdvCategories.DataSource = dataTableCategories;
this.treeViewAdvCategories.Refresh();


// expand root node
try
{
TreeNodeAdv node = this.treeViewAdvCategories.Nodes[0];
node.ExpandAll();
}
catch(Exception ex)
{
logger.Error("Expand error:");
logger.Error(ex);
}


But here, the count for this.treeViewAdvCategories.Nodes is always zero.

Maybe it normal, but i expected a non-zero value for count.


My way of expanding root node is right ?

Could you help me ?

BR

Cyrille






Attachment: Pictures_6ba1c7f.7z

2 Replies

SJ Sathiyathanam Jeyakumar Syncfusion Team November 7, 2022 02:09 PM


Hi Cyrille,


Sorry for the delay, we are checking the reported issue and will update you with further details on November 9, 2022. Appreciate your patience until then.


Regards,

Sathiyathanam





SJ Sathiyathanam Jeyakumar Syncfusion Team November 9, 2022 11:10 AM

Hi Cyrille,

You can expand all the nodes based on the root nodes with the dispatcher in DataSourceChanged event. Refer the below code snippets and the sample to get the more information.

//Event subscription

this.treeViewAdv1.DataSourceChanged += OnDataSourceChanged;

this.Load += OnLoad;

 

private void OnLoad(object sender, EventArgs e)

{

    //assign the DataSource

    treeViewAdv1.DataSource = dataTable1;            

}

 

//Event customization

private void OnDataSourceChanged(object sender, DataSourceChangedEventArgs e)

{

    //Here expand the root node

    treeViewAdv1.BeginInvoke(new Action(() =>

    {

        //here expand the node

        TreeNodeAdv node = this.treeViewAdv1.Root;

        node.ExpandAll();

    }));

}


Sample:https://www.syncfusion.com/downloads/support/directtrac/general/ze/Sample-1019009211.zip

Please check your data's count whether the data is properly fetched from db or not.

Regards

Sathiyathanam


Loader.
Live Chat Icon For mobile
Up arrow icon