)
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. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to populate the WinForms TreeMap data values by using the DataTable?

Platform: WinForms |
Control: TreeMap |
Published Date: March 5, 2015 |
Last Revised Date: February 4, 2020

Populate the treemap data values using datatable

The following steps help you to populate the TreeMap data values by using the DataTable.

Step 1: Add the data of columns and rows to the DataTable as given in the following code. In the code, five columns are added such as Continent, Country, Growth, Population and StrPopulation and values included for the respective columns.

C#

public class PopulationViewModel
{
    public PopulationViewModel()
    {
        PopulationDetails = new DataTable();
        PopulationDetails.Columns.Add("Continent", typeof(string));
        PopulationDetails.Columns.Add("Country", typeof(string));
        PopulationDetails.Columns.Add("Growth", typeof(double));
        PopulationDetails.Columns.Add("Population", typeof(double));
        PopulationDetails.Columns.Add("StrPopulation", typeof(string));
        PopulationDetails.Rows.Add("Asia", "Indonesia", 3, 237641326, "237.6 M"); 
    }
    public DataTable PopulationDetails
    {
        get;
        set;
    }      
}

Step 2:  Assign data values to the ItemsSource property in order to populate the provided TreeMap items.

C#

public partial class Form1 : Form
{     
    TreeMap treeMap1 = new TreeMap(); 
    public Form1()
    {
        PopulationViewModel data = new PopulationViewModel();
        treeMap1.ItemsSource = data.PopulationDetails;
        this.Controls.Add(treeMap1);          
    }
}

In the following code example, DataTable has been created under the PopulationViewModel() by adding the necessary columns and rows. Column data denotes TreeMap header and row data denotes corresponding values for the TreeMap header. Assign the PopulationDetails data to the ItemsSource of the TreeMap in order to render the TreeMap control.

C#

public partial class Form1 : Form
{
    TreeMap treeMap1 = new TreeMap(); 
    public Form1()
    {
        PopulationViewModel data = new PopulationViewModel();
        treeMap1.ItemsSource = data.PopulationDetails;
        treeMap1.WeightValuePath = "Population";
        treeMap1.ColorValuePath = "Growth";
        this.Controls.Add(treeMap1);       
    }
}
public class PopulationViewModel
{
    public PopulationViewModel()
    {
        PopulationDetails = new DataTable();
        PopulationDetails.Columns.Add("Continent", typeof(string));
        PopulationDetails.Columns.Add("Country", typeof(string));
        PopulationDetails.Columns.Add("Growth", typeof(double));
        PopulationDetails.Columns.Add("Population", typeof(double));
        PopulationDetails.Columns.Add("StrPopulation", typeof(string));
        PopulationDetails.Rows.Add("Asia", "Indonesia", 3, 237641326, "237.6 M");
        PopulationDetails.Rows.Add("Asia", "Russia", 2, 152518015, "152.5 M");
        PopulationDetails.Rows.Add("Asia", "Malaysia", 1, 29672000, "29.7 M");
        PopulationDetails.Rows.Add("North America", "United States", 4, 315645000, "315.6 M");
        PopulationDetails.Rows.Add("North America", "Mexico", 2, 112336538, "112.3 M");
        PopulationDetails.Rows.Add("North America", "Canada", 1, 35056064, "35.1 M");
        PopulationDetails.Rows.Add("South America", "Colombia", 1, 47000000, "47 M");
        PopulationDetails.Rows.Add("South America", "Brazil", 3, 193946886, "193.9 M");
        PopulationDetails.Rows.Add("Africa", "Nigeria", 2, 170901000, "170.9 M");
        PopulationDetails.Rows.Add("Africa", "Egypt", 1, 83661000, "83 M");
        PopulationDetails.Rows.Add("Europe", "Germany", 1, 81993000, "82 M");
        PopulationDetails.Rows.Add("Europe", "France", 1, 65605000, "65.6 M");
        PopulationDetails.Rows.Add("Europe", "UK", 1, 63181775, "63.2 M");
    }
    public DataTable PopulationDetails
    {
        get;
        set;
    }
}

The following screenshot displays the TreeMap items populated by using the DataTable.

Populating treemap items by using datatable

Figure 1: Populating TreeMap items by using DataTable

Sample: CustomColorMapping.zip

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon