Windows Develop Bookmark and Share   
 index > Windows Forms General > adding tree node
 

adding tree node


if we consider the nortthwind database ,so i have categories and each one have many products
so if u use TreeView to show each category and it's corresponding products
i would like to add cateogry in that tree if i right click and choose add category and also how can i delete category from this tree and finnally how can i add the new category in categories table also the delete command

plz help me by code i am using visualstudio2005 and sqlserver2000
adel atef  Saturday, October 06, 2007 5:30 PM

Hi

If you are using C#, you can try something like the following:

Code Block

TreeViewP

public partial class Form6 : Form

{

public Form6()

{

InitializeComponent();

}

DataTable dt = new DataTable();

SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=True");

private void Form6_Load(object sender, EventArgs e)

{

SqlCommand cm = new SqlCommand("select categoryId,categoryName from Categories", cn);

SqlDataAdapter da = new SqlDataAdapter(cm);

da.Fill(dt);

foreach(DataRow dr in dt.Rows)

{

TreeNode tn = new TreeNode(dr["categoryName"].ToString());

tn.Tag = dr["categoryId"].ToString();

this.treeView1.Nodes.Add(tn);

}

}

private void btnAdd_Click(object sender, EventArgs e)

{//add a Category item

SqlCommand insercommand = new SqlCommand("InsertCategory", cn);

insercommand.CommandType = CommandType.StoredProcedure;

insercommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar,15).Value = this.textBox1.Text;

SqlParameter myParm = insercommand.Parameters.Add("@Identity", SqlDbType.Int);

myParm.Direction = ParameterDirection.Output;// use to retrieve the CategoryId

cn.Open();

try

{

insercommand.ExecuteNonQuery();

TreeNode tn = new TreeNode(this.textBox1.Text);

tn.Tag = myParm.Value.ToString();

this.treeView1.Nodes.Add(tn);

}

catch

{

MessageBox.Show("Save Data Error!");

}

finally

{

cn.Close();

}

}

private void btnDelete_Click(object sender, EventArgs e)

{//delete a Category item

string id = this.treeView1.SelectedNode.Tag.ToString();

SqlCommand deletecommand = new SqlCommand("delete Categories where categoryId = '" + id + "'", cn);

cn.Open();

try

{

deletecommand.ExecuteNonQuery();

this.treeView1.SelectedNode.Remove();

}

catch

{

MessageBox.Show("Delete Data Error!");

}

finally

{

cn.Close();

}

}

}

The InsertCategory stored procedure is like:

Code Block

CREATE PROCEDURE [dbo].[InsertCategory]

@CategoryName nvarchar(15),

@Identity int OUT

AS

INSERT INTO Categories (CategoryName) VALUES(@CategoryName)

SET @Identity = SCOPE_IDENTITY()

GO

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, October 09, 2007 12:19 PM

Hi

If you are using C#, you can try something like the following:

Code Block

TreeViewP

public partial class Form6 : Form

{

public Form6()

{

InitializeComponent();

}

DataTable dt = new DataTable();

SqlConnection cn = new SqlConnection("Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=True");

private void Form6_Load(object sender, EventArgs e)

{

SqlCommand cm = new SqlCommand("select categoryId,categoryName from Categories", cn);

SqlDataAdapter da = new SqlDataAdapter(cm);

da.Fill(dt);

foreach(DataRow dr in dt.Rows)

{

TreeNode tn = new TreeNode(dr["categoryName"].ToString());

tn.Tag = dr["categoryId"].ToString();

this.treeView1.Nodes.Add(tn);

}

}

private void btnAdd_Click(object sender, EventArgs e)

{//add a Category item

SqlCommand insercommand = new SqlCommand("InsertCategory", cn);

insercommand.CommandType = CommandType.StoredProcedure;

insercommand.Parameters.Add("@CategoryName", SqlDbType.NVarChar,15).Value = this.textBox1.Text;

SqlParameter myParm = insercommand.Parameters.Add("@Identity", SqlDbType.Int);

myParm.Direction = ParameterDirection.Output;// use to retrieve the CategoryId

cn.Open();

try

{

insercommand.ExecuteNonQuery();

TreeNode tn = new TreeNode(this.textBox1.Text);

tn.Tag = myParm.Value.ToString();

this.treeView1.Nodes.Add(tn);

}

catch

{

MessageBox.Show("Save Data Error!");

}

finally

{

cn.Close();

}

}

private void btnDelete_Click(object sender, EventArgs e)

{//delete a Category item

string id = this.treeView1.SelectedNode.Tag.ToString();

SqlCommand deletecommand = new SqlCommand("delete Categories where categoryId = '" + id + "'", cn);

cn.Open();

try

{

deletecommand.ExecuteNonQuery();

this.treeView1.SelectedNode.Remove();

}

catch

{

MessageBox.Show("Delete Data Error!");

}

finally

{

cn.Close();

}

}

}

The InsertCategory stored procedure is like:

Code Block

CREATE PROCEDURE [dbo].[InsertCategory]

@CategoryName nvarchar(15),

@Identity int OUT

AS

INSERT INTO Categories (CategoryName) VALUES(@CategoryName)

SET @Identity = SCOPE_IDENTITY()

GO

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, October 09, 2007 12:19 PM

Hello I would like to ask how to filter data represented by listView items?

I am using Visual C++ 2005 Express Edition.

I need to use somekind of data filtering in my application BUT I don't use a database.

My application consists of a treeView and a listView.

The treeView consists of 3 levels (0-2)

Level 0 is root, level 1 is Company Nameand level 2 is Employee Name(ONLY NAME).

The listView consists of Company Name, Employee Name, Age, Gender, Address, City, ZIP and Phone Number in Detailed View so it form a table.

Example of treeView:

-ALL level 0

-Microsoft level 1

-Robert level 2

-Andrew level 2

-Google level 1

-Wayne level 2

So it works like this (3 CONDITIONS):

When I click the COMPANY NAME on treeView, the listView will FILTER ONLY that company'semployee data will be displayed.

When I clickthe employee name on the treeView, the listViewwill only display that person data.

When I click the root level, there is no FILTERING.

I have triedby comparing the item and node but if I found thatthose 2 items are different, I will remove themfrom my listview and they cannot be returned.

WhatI want is only to hide the item that is not equalswith the treeView node so that there is no problem if I click the root.

1. Is it possible to make the listView items not visible or is there another way that I don't know? So it works like a SQL which is selecting some data base on a condition like this:

select company_name, employee_name from company where employee_name = treeView1->SelectedNode->Text.

I wonder if it can works like this.

2. I have tried to add a new node but it always error on runtime. What is wrong? Here is my code:

I was confused why the error is always dealing with selecting the node.

Flag is a bool variable which I set false when the form loaded and when the node is selected I set it to true.

Thank you very much.

//on event node After Select

private: System::Void treeView1_AfterSelect(System:Surprisebject^ sender, System::Windows::Forms::TreeViewEventArgs^ e) {
if (treeView1->Nodes->Count != 0) {
dipilih = e->Node->Text;
trinode = e->Node;
//MessageBox:Tongue Tiedhow(dipilih);
flag = true;
//tn = treeView1->SelectedNode;
}
}

//on event Button click <which is adding the node>

private: System::Void button1_Click(System:Surprisebject^ sender, System::EventArgs^ e) {
if (!String::IsNullOrEmpty(textBox1->Text)) {
TreeNode ^treenode = gcnew TreeNode(textBox1->Text);
if (treeView1->Nodes->Count == 0)
treeView1->Nodes->Add(treenode);
else
if (treeView1->Nodes->Count != 0) {// && flag)
treeView1->SelectedNode->Nodes->Add(treenode);
if (flag)
treeView1->Nodes->Add(treenode);
}
}
treeView1->Refresh();
}

Thankyou very much.

Christiawan  Tuesday, November 27, 2007 5:35 AM

You can use google to search for other answers

Custom Search

More Threads

• Grouping in datagrid
• Pen thickness
• Issues displaying information from a string in a multiline textbox
• How to set normal link to visited on WebBrowser control?
• DropDown Tree Control
• Textbox, enter key playing "doink" sound..
• Painting problem . That's Weird !!!!!
• Basic doubt about the class and instantiation details!
• How to pass VBA collection to VB Com Component?
• GUI Drawing