Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Tree diagram and views--- Windows Application
 

Tree diagram and views--- Windows Application

Hi

I have a query.

I have to create some views in Sql 2005. Once i create these views , the view names have to be binded to a tree diagram.

Its as follows.

Let the views created are view 1 , view 2, view 3, view 4

The tree diagram is as follows.

Header 1 is the parent node1.

Header 2 is theparent node2.

View1, View 2 should be child nodes to Parent node1.

View 3, view 4 should be child nodes for parent node 2.

How should i bind the view names to the tree as child nodes?


later when i click a view , then its columns should reflecct in a list box

sairam120  Tuesday, August 12, 2008 9:41 PM

Hi Sairam,

Thanks for your feedback and sorry for that misunderstanding. If you only want to add the name of the view to the TreeView as tree node, you don’t need to use data binding; you can add the tree node manually. When adding the tree node, you can set the corresponding DataTable to the Tag property of the tree node. Then to show all columns in the ListBox, you can handle the AfterSelect event of the TreeView and add columns to the ListBox. Here is a sample for your information.

Code Snippet

public partial class Form6 : Form

{

public Form6()

{

InitializeComponent();

}

DataTable dt1 = new DataTable();

DataTable dt2 = new DataTable();

void PopulataData()

{

// you can get the data from database here

dt1.Columns.Add("dt1col1");

dt1.Columns.Add("dt1col2");

dt1.Columns.Add("dt1col3");

dt1.Columns.Add("dt1col4");

dt1.Columns.Add("dt1col5");

dt1.Columns.Add("dt1col6");

dt2.Columns.Add("dt2Col1");

dt2.Columns.Add("dt2Col2");

dt2.Columns.Add("dt2Col3");

}

private void Form6_Load(object sender, EventArgs e)

{

PopulataData();

TreeNode mainNode1 = new TreeNode();

mainNode1.Name = "prntcontract";

mainNode1.Text = "Contract Header";

this.treeView1.Nodes.Add(mainNode1);

TreeNode node1 = new TreeNode("view1");

node1.Tag = dt1;

mainNode1.Nodes.Add(node1);

TreeNode node2 = new TreeNode("view2");

node2.Tag = dt2;

mainNode1.Nodes.Add(node2);

this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

}

void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

{

this.listBox1.Items.Clear();

if (e.Node.Tag is DataTable)

{

DataTable dt = (DataTable)e.Node.Tag;

foreach (DataColumn dc in dt.Columns)

this.listBox1.Items.Add(dc.ColumnName);

}

}

}

Thanks.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs
Rong-Chun Zhang  Friday, August 22, 2008 3:40 AM

Hi sairam,

Based on my understanding, what you want to do is to binding these views in SQL Server database to the TreeView control in a windows form application, right? If so, by default, the TreeView doesn’t supporting data bindings. But you can and all the rows into the TreeView control manually and refresh it when the rows are changed. Here is a sample to add rows to a TreeView control.

To show the columns in a ListBox when click a node, we can store the key word (like id of table name) in the Tag property and handle the AfterSelect event to show the columns based on the key word.

Another choice is we can make our own TreeView to enable Data Bindings, here is a sample for your information.

Best regards.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Rong-Chun Zhang  Thursday, August 14, 2008 8:23 AM

Hi Rong-chun zhang

i am struck at data binding.

I have created a tree with nodes as the code below.

i also created two views . I am able to extract the viewnames from the data base.

for ex: there are 2 views in the DB. lets call them as view1,view2.

my data set gives me output as view1 and view2 i.e only the view names.

i ahve to bind these two names to a parent node as child nodes.

later when i click the child nodes i.e the view names then the column names in those views are to be displayed in a list box below.

My questions are :

1. How to bind the view names(list of views i.e View1 and view 2) to the first parent node called Contract Header

2. Once i select a child node, its column names shud reflect in a list box.

Code i have used to create parent and child nodes

TreeNode mainNode1 = new TreeNode();

mainNode1.Name = "prntcontract";

mainNode1.Text = "Contract Header";

this.treeView1.Nodes.Add(mainNode1);

mainNode1.Nodes.Add("View1");

mainNode1.Nodes.Add("view2");

TreeNode mainNode2 = new TreeNode();

mainNode2.Name = "prntinsurnance";

mainNode2.Text = "Insurance Info";

this.treeView1.Nodes.Add(mainNode2);

mainNode2.Nodes.Add("view3");

mainNode2.Nodes.Add("view4");

My Ado.Net code to retrieve data from database

SqlConnection cn = new SqlConnection("User id=cmslogin;password=cmsunlock;database=CMS;Data source=primasqltst\\qa");

cn.Open();

SqlDataAdapter da = new SqlDataAdapter("SELECT VIEW_NAME FROM information_schema.view_table_usage WHERE table_name = 'tblcontractclaims')", cn);

DataSet ds = new DataSet();

da.Fill(ds);

Thanks

Sairam

sairam120  Thursday, August 21, 2008 7:17 PM

Hi Sairam,

Thanks for your feedback and sorry for that misunderstanding. If you only want to add the name of the view to the TreeView as tree node, you don’t need to use data binding; you can add the tree node manually. When adding the tree node, you can set the corresponding DataTable to the Tag property of the tree node. Then to show all columns in the ListBox, you can handle the AfterSelect event of the TreeView and add columns to the ListBox. Here is a sample for your information.

Code Snippet

public partial class Form6 : Form

{

public Form6()

{

InitializeComponent();

}

DataTable dt1 = new DataTable();

DataTable dt2 = new DataTable();

void PopulataData()

{

// you can get the data from database here

dt1.Columns.Add("dt1col1");

dt1.Columns.Add("dt1col2");

dt1.Columns.Add("dt1col3");

dt1.Columns.Add("dt1col4");

dt1.Columns.Add("dt1col5");

dt1.Columns.Add("dt1col6");

dt2.Columns.Add("dt2Col1");

dt2.Columns.Add("dt2Col2");

dt2.Columns.Add("dt2Col3");

}

private void Form6_Load(object sender, EventArgs e)

{

PopulataData();

TreeNode mainNode1 = new TreeNode();

mainNode1.Name = "prntcontract";

mainNode1.Text = "Contract Header";

this.treeView1.Nodes.Add(mainNode1);

TreeNode node1 = new TreeNode("view1");

node1.Tag = dt1;

mainNode1.Nodes.Add(node1);

TreeNode node2 = new TreeNode("view2");

node2.Tag = dt2;

mainNode1.Nodes.Add(node2);

this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);

}

void treeView1_AfterSelect(object sender, TreeViewEventArgs e)

{

this.listBox1.Items.Clear();

if (e.Node.Tag is DataTable)

{

DataTable dt = (DataTable)e.Node.Tag;

foreach (DataColumn dc in dt.Columns)

this.listBox1.Items.Add(dc.ColumnName);

}

}

}

Thanks.
Rong-Chun Zhang

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs
Rong-Chun Zhang  Friday, August 22, 2008 3:40 AM

You can use google to search for other answers

Custom Search

More Threads

• AutoComplete Using Simple DropDownStyle ComboBox (Windows Forms 2.0)
• TextBox bound to varchar still getting padded with spaces
• How to detect "form dirty" and enable Save button
• updating the database
• selecting records with a null value
• Find DataRow with matching data in a DataTable
• update,inser,delete row from the datagrid
• Displaying a memo field in a Datagridview
• Problem with combobox in datagridview
• Multiple Tables in one DataGridView