How to populate and save to and from Datatable

5 posts, 0 answers
  1. Suren
    Suren avatar
    12 posts
    Member since:
    Jul 2012

    Posted 24 Jul 2012 Link to this post

    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.




  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 27 Jul 2012 Link to this post

    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 >>
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Suren
    Suren avatar
    12 posts
    Member since:
    Jul 2012

    Posted 27 Jul 2012 Link to this post

    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
  5. Suren
    Suren avatar
    12 posts
    Member since:
    Jul 2012

    Posted 29 Jul 2012 Link to this post

    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 ?
  6. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 01 Aug 2012 Link to this post

    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 >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready