I have a query about arraylist.
i have some child nodes in a treecontrol. when i select a node and click a button the tree node gets added in richtextbox. similarly i select few more nodes and add them in richtextbox. till here i did it. Everytime i add a node into richtextbox, the node must be captured by a arraylist. i.e. for ex if i add node1,node2,node3 to richtext box, i want these nodes in a arraylist with a delimiter "," (comma) and this arraylist must be saved in db.
So how to create an arraylist
How to add nodes in it with a delimiter comma during the button click event.
How to save the arraylist in DB
Please Help me,
Satish KathiPosted Dec 4, 2008, 5:08 PM
private void button3_Click(object sender, EventArgs e)
{
//To hold coma seperate string value of nodes of selected node
StringBuilder sb = new StringBuilder();
//To hold list of of nodes of selected node
List<string> lstStr = new List<string>();
//Iterate through each node of selected node
foreach (TreeNode ctn in treeView1.SelectedNode.Nodes)
{
//Add ',' seperated string(node)
sb.Append(ctn.Text);
sb.Append(",");
//insert values in List
lstStr.Add(ctn.Text);
}
// You can store string s in database
string s = sb.ToString().Trim().TrimEnd(new char[]{','});
}
HOPE THIS HELPS
sairamPosted Dec 4, 2008, 1:14 PM
I did not understand it.
what i need is when i select anode and click a button, the node is inserted in richtextbox and also it has to be a part of list. and so on. finally the string to be inserted in DB should be node1,node2,node3,node4.......
so how should i capture these node1,node2,node3
thanks
Dusan TkacPosted Dec 4, 2008, 12:39 PM
Hi,
I suppose that strings added to RichTextBox are some unique identifiers which allow you to recreate (identify) selected nodes in tree when you will try to use values stored in database.
There is probably no pre-defined method which will create a comma-separated list of strings so you will have to iterate through your ArrayList (btw. don't you want to use List instead?) like this:
StringBuilder sb = new StringBuilder;
for(int i = 0; i < list.Count; i++)
{
sb.Append((string)list[i]);
if(i < list.Count - 1)
sb.Append(",");
}
string storeToDB = sb.ToString();
Then just store "storeToDB" to DB.
You can recreate you list of identifiers using String.Split method.