How to bind data base value to Treeview control using C#


How to bind data base value to Treeview control using C#

How to bind data base value to Treeview control using C#
Just drag and drop one tree view control to windows form.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("USER=userid;PASSWORD=password;SERVER=servername;DATABASE=database");
string SQLQuery;
SQLQuery = "select tablename,displayname from Tablename";
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(SQLQuery,con);
da.Fill(ds);
foreach ( DataRow Row in ds.Tables[0].Rows)
{
TreeNode Tnode = new TreeNode("ParentNode");
treeView1.Nodes.Add(Tnode);
TreeNode CNode1 = new TreeNode(Row["tablename"].ToString());
Tnode.Nodes.Add(CNode1);
TreeNode CNode2 = new TreeNode(Row["displayname"].ToString());
Tnode.Nodes.Add(CNode2);
}


}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: