This is a migrated thread and some comments may be shown as answers.

How to populate and save to and from Datatable

4 Answers 168 Views
Treeview
This is a migrated thread and some comments may be shown as answers.
Suren
Top achievements
Rank 1
Suren asked on 24 Jul 2012, 09:13 PM
Hi guys,
sorry if this has been asked before but so far i couldn't find such topic for C#. so again asking the experts to help me.
so i'm using VC# 2010 SP1, RAD Q2 2012.

so i have the following datatable and dataset. code below

DataSet SelPreDS = new DataSet();
    DataTable SelPreDT;
 
SelPreDT = SelPreDS.Tables.Add();
 
    SelPreDT.Columns.Add("ID", typeof(Int32));
    SelPreDT.Columns.Add("Code", typeof(string));
    DataColumn dc = new DataColumn("ParentCode", typeof(string));
    dc.AllowDBNull = true;
    SelPreDT.Columns.Add(dc);
    SelPreDT.Columns.Add("Name", typeof(string));
    SelPreDT.Columns.Add("Type", typeof(string));
      SelPreDT.Columns.Add("isCheck", typeof(Boolean));
     SelPreDT.Columns.Add("Price"typeof(Int32)); 

and i add values to the datatable like this

DataRow dr = SelPreDT.NewRow();
 
                dr[0] = id;
                dr[1] = code;
                dr[2] = ParentNoteID;
                dr[3] = Name;
                dr[4] = "Process";
      dr[5] = true;
dr[6] = 6.66;
                SelPreDT.Rows.Add(dr);


so my datatable is something like below when values are added.

0 | IT01 |          | name1 |  Process0  | true  | 5.55
1 | IT02 | IT01  | name2 | Process0   | True | 2.66
2 | IT03 | IT02 |  name3 | Process0 | True  | 6.46

so my question is now how can  i use the datatable above  to populate the radTreeview and then
when user edit the price value in radtreeview it should get updated in the datatable as well as
 the checkbox state.

Could you please include an example. 

thank you for your help.

suren.




4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 27 Jul 2012, 01:01 PM
Hello Suren,

Thank you for writing.

To achieve your requirement you should set both the DisplayMember and the ValueMember properties of RadTreeView to the "Price" field, and then when you change a value you can set the "isCheck" field value. Here is an example: 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
 
        DataTable SelPreDT;
        DataSet SelPreDS = new DataSet();
 
        SelPreDT = SelPreDS.Tables.Add();
 
        SelPreDT.Columns.Add("ID", typeof(Int32));
        SelPreDT.Columns.Add("Code", typeof(string));
        DataColumn dc = new DataColumn("ParentCode", typeof(string));
        dc.AllowDBNull = true;
        SelPreDT.Columns.Add(dc);
        SelPreDT.Columns.Add("Name", typeof(string));
        SelPreDT.Columns.Add("Type", typeof(string));
        SelPreDT.Columns.Add("isCheck", typeof(Boolean));
        SelPreDT.Columns.Add("Price", typeof(double));
 
        for (int i = 0; i < 20; i++)
        {
            DataRow dr = SelPreDT.NewRow();
            dr[0] = i;
            dr[1] = i*10;
            dr[2] = i;
            dr[3] = "name " + i;
            dr[4] = "Process";
            dr[5] = true;
            dr[6] = i + 6.66;
            SelPreDT.Rows.Add(dr);
        }
 
        radTreeView1.DataSource = SelPreDT;
        radTreeView1.DisplayMember = "Price";
        radTreeView1.ValueMember = "Price";
 
        radTreeView1.AllowEdit = true;
        radTreeView1.ValueChanged += new Telerik.WinControls.UI.TreeNodeValueChangedEventHandler(radTreeView1_ValueChanged);
    }
 
    void radTreeView1_ValueChanged(object sender, Telerik.WinControls.UI.TreeNodeValueChangedEventArgs e)
    {
        DataRowView rowView = e.Node.DataBoundItem as DataRowView;
        rowView.Row["isCheck"] = false;
    }
}

I hope this helps.
 
Greetings,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
0
Suren
Top achievements
Rank 1
answered on 27 Jul 2012, 09:22 PM
thank you very much but this not exactly what i want,
using the above code it would display like this (ASCII Art)

|
1
|
2
|
3

i want something like this

|
1
+++2
|
+++3

i want it in a treeview, thats why i have the code and parentcode fields 
as a child or in () the price should be displayed,

so is there anyway to achieve this, or something similar  
thank you,
again
0
Suren
Top achievements
Rank 1
answered on 30 Jul 2012, 02:13 AM
Hello,

radTreeView1.ChildMember = "Code";
radTreeView1.ParentMember = "ParentCode";
radTreeView1.DisplayMember = "Name";
radTreeView1.DataSource = SelPreDT;


this code is doing what i need to a certain extent, but it does not sync checkbox state and the price part.

 isn't there a way to make use of (PropertyHelper) to do what i'm trying to do ?
0
Stefan
Telerik team
answered on 01 Aug 2012, 11:54 AM
Hi Suren,

Thank you for the clarification.

In your code you are setting the DisplayMember to be the "Name" field and as far as I understand you want to allow the user to edit the price. If so, set the ValueMember to "Price", thus, when a user edits a node, he will modify the price, not the name.

As to the "isCheck" field, in my previous post I showed how to modify it too.

I hope this helps.

Kind regards,
Stefan
the Telerik team
RadControls for WinForms Q2'12 release is now live! Check out what's new or download a free trial >>
Tags
Treeview
Asked by
Suren
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Suren
Top achievements
Rank 1
Share this question
or