Articles in this section
Category / Section

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

2 mins read

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

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied