Fill a treeview with a datatable c #

0

I have this datatable defined:

DataTable datos = new DataTable("Coordenadas");
        datos.Columns.Add("ID", typeof(int));
        datos.Columns.Add("Ciudad", typeof(string));
        datos.Columns.Add("Latitud", typeof(double));
        datos.Columns.Add("Longitud", typeof(double));

        datos.Rows.Add(new object[] { 1, "Ciudad1", 19.0437118169261, -98.1981825828552 });
        datos.Rows.Add(new object[] { 2, "Cuidad2", 18.6026185823842, -98.4658348560333 });
        datos.Rows.Add(new object[] { 3, "Ciudad3", 18.1989507909863, -98.0486440658569 });

and I would like to create a treeview that can be displayed like this:

Cities

| --- City1

| --- City2

| --- Ciudad3

How could it be done? thanks

    
asked by Juan Cruz Guzman 31.08.2017 в 02:00
source

1 answer

0

Try this way:

        DataTable datos = new DataTable("Coordenadas");
        datos.Columns.Add("ID", typeof(int));
        datos.Columns.Add("Ciudad", typeof(string));
        datos.Columns.Add("Latitud", typeof(double));
        datos.Columns.Add("Longitud", typeof(double));

        datos.Rows.Add(new object[] { 1, "Ciudad1", 19.0437118169261, -98.1981825828552 });
        datos.Rows.Add(new object[] { 2, "Cuidad2", 18.6026185823842, -98.4658348560333 });
        datos.Rows.Add(new object[] { 3, "Ciudad3", 18.1989507909863, -98.0486440658569 });

        foreach (DataRow r in datos.Rows)
        {
            var node = treeView1.Nodes.Add(r["Ciudad"].ToString());
            node.Nodes.Add(r["Latitud"].ToString());
            node.Nodes.Add(r["Longitud"].ToString());
        }

Greetings,

    
answered by 31.08.2017 / 13:54
source