How to show a display value for selected node in treeview in C#? - c#

I am trying to create a treeview which pulls info from a sql database. I want the text to be the name field but when you double click the name i want it to display the id field. I have look and looked but cant find any info on this?
Code tried (Added from OP's comment):
foreach (DataRow dr in Db.Table("Employee").Rows)
{
treeView1.Nodes.Add(
new TreeNode(dr["Name"].ToString(),
new TreeNode[] {new TreeNode(dr["EEID"].ToString())}));
}
var node = treeView1.SelectedNode.Nodes[0].Text;
MessageBox.Show(string.Format("You selected: {0}", node));

When you create new nodes for a TreeView you can specify a text value and a key value, like so:
TreeView tv = new TreeView();
tv.Nodes.Add(key, text); //where key is your database id value, and text the display
Then you'd simply return the key of the clicked node. Is this what you want?
EDIT: This is what happens when you speak from memory... this is wrong. 'key' is not a hidden key value, like an ID, 'key' is the name of the tree node. Please hold while I give you a proper solution.
** EDIT2 (SOLVED) ** : You can also use the Name property. Like this:
tView.Nodes.Add("Id_0001", "Mr. Dexter");
then you could retrieve the values of that node with something like this:
private void tvView_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode node = e.Node;
MessageBox.Show(node.Name + "\n" + node.Text);
}
which would yield the results: "Id_0001" and "Mr. Dexter".

foreach (DataRow dr in Db.Table("Employee").Rows)
{
TreeNode tn = new TreeNode();
tn.Tag = dr["eeid"];
tn.Text = dr["Name"].ToString();
treeView1.Nodes.Add(tn);
}
private void treeView1_DoubleClick(object sender, EventArgs e)
{
MessageBox.Show(treeView1.SelectedNode.Tag.ToString());
}

you can use Mouse Click event. when you click on a particular node (assume it's not WPF cause then it's Items) you can get its Text from SelectedNode property.
private void btnGetNodeValue_Click(object sender, EventArgs e)
{
string nodeVal= treeView1.SelectedNode.Text;
}
then you can pass this string value to database to retrieve your value,mix up with Select statement and WHERE clause so you can easily get it.

Related

How to get the name of the selected child node in a treeview list in c#?

I am using the command:
selecteddirectory = treeViewDirectory.SelectedNode.Text;
However this always gives the name of the parent node and not the child node selected.
How to go about doing this?
You can call the treeview_AfterSelect event to select the child node.
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string selectedNodeText = e.Node.Text;
MessageBox.Show(selectedNodeText);
}
Like this:
selecteddirectory = treeViewDirectory.SelectedNode.Name;

combobox event & clear method

private void ComboBox1_TextChanged(object sender, EventArgs e)
{
ComboBox1.Items.Clear();
XmlNodeList node_lst = doc["paths"].ChildNodes;
foreach (XmlNode item in node_lst)
{
if (item.InnerText.Contains(ComboBox1.Text))
{
ComboBox1.Items.Add(item.InnerText);
}
}
}
I am new in this website, i have question in this event when I clear items from combobox, I get these items from an xml file; my problem is when I enter letters in combobox text, the text is entered in inversed way, I expected that the problem in clear method but i have not idea how to do this.
Thanks.
When you delete all items from the ComboBox the cursor will be set to the first position., So after each character that you type the cursor will be shifted to the left and you get the feeling of typing from right to left.
The solution would be to set the SelectionStart after the for-loop by hand to the end of the Text:
comboBox1.SelectionStart = comboBox1.Text.Length;
I guess you want something like this:
var nodeList = node_lst.Cast<XmlNode>()
.Select(x => library.GetMemberName(int.Parse(x.InnerText)))
.ToList();
nodeList.Reverse();
More info on Reverse(): https://msdn.microsoft.com/en-us/library/b0axc2h2(v=vs.110).aspx

how to get the index of current selected node from treeview in c#

I'm working on a form application in c# that includes TreeView in it. What I want to do is to attach a panel to each node so whenever user clicks on a node the panel will be updated according to the node selected.
The problem I'm facing is that when I select a node, the application does nothing but when I select another node then the application shows the content related to previously selected node. Means that App is always getting content related to last selected node not the current one. For Example If I'll select "Text" node, the label will show nothing and after that If I'll select some other node like "Appearance" the label will show "Text" which was the last selected node.
Following here is the image of my Form that contains TreeView.
For testing purpose I'm just storing the selected node's value in my label's text
Here's the code.
public partial class TextEditor_Preferences : Form
{
public TextEditor_Preferences()
{
InitializeComponent();
}
List<Panel> myPanels = new List<Panel>(); //Ignore this line of code !
private void SideBar_MouseClick(object sender, MouseEventArgs e)
{
label1.Text = SideBar.SelectedNode.ToString();
}
}
Can anobody suggest me a method?
If I'm missing something or question is not valid, Please let me know explicitly. Thanks
TreeView control has the AfterSelect event you should write your code in that handler.
public YourForm()
{
InitializeComponent();
treeView.AfterSelect += TreeViewAfterSelect;
}
private void TreeViewAfterSelect(object sender, TreeViewEventArgs e)
{
string nodeText = treeView.SelectedNode.Text;
// Update the panel here accordingly
}
Maybe try using the AfterSelect event rather than MouseClick. e.g.
private void Sidebar_AfterSelect(object sender, TreeViewEventArgs e)
{
label1.Text = e.Node.Text;
}

Finding page controls starting with some string

I have many of TextBoxes in the ASP.NET page that their ID starts with certain string like xyz(e.g: xyz1,xyz11,xyz999). I know FindControl method, but it finds only by complete ID of Control.
How can I find controls in which their ID be like that on page?
You can include the Extension Method to get all the textboxes on page mentioned in this answer and then can simply filter with the Id you need like this:-
var alltextBoxes = this.Page.FindControls<TextBox>(true).Where(x => x.ID.Contains("xyz"));
If you want all the Ids which starts with some specific text say xyz, then you can also use String.StartsWith since the textbox Id is a string:-
.Where(x => x.ID.StartsWith("xyz"));
You have to loop recursively inside the page to find the TextBox controls that match your string:
List<TextBox> _TextBoxes;
protected void Page_Load(object sender, EventArgs e)
{
_TextBoxes = new List<TextBox>();
FindTextBoxes(Page, "xyz1");
}
private void FindTextBoxes(Control parent, string startsWith)
{
if(parent.GetType()==typeof(TextBox) && parent.ID.StartsWith(startsWith))
{
_TextBoxes.Add(parent as TextBox);
}
foreach (var c in parent.Controls)
{
FindTextBoxes(c, startsWith);
}
}

C# Populate Radiobuttons in a TreeView on PageLoad with a Database Returned Value

I am using a TreeView control, and I would like to be able to use radiobuttons instead of checkboxes. I am currently creating the radiobuttons using the following code:
e.Node.ChildNodes.Add(new TreeNode(String.Format("<input type='radio' value='{0}' name='rblMain' /> {1}", value, name), value));
Now my question is how can I repopulate the node with the value that I am returned from the database to the correct node and have it be selected?
Thanks, Andrew
I found this which might be useful:
http://www.dotnetspider.com/forum/217059-radiobutton-treeview.aspx
It has the solution to show the radio button before the text and in the node selected event, we have to write the code to deselect the selected nodes and select the current one. Its around 10 to 15 lines code have to write in this event.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeNode nodeParent = new TreeNode();
nodeParent.Text = "Parent";
tvwSample.Nodes.Add(nodeParent);
TreeNode nodeChild = new TreeNode();
nodeChild.Text = "Child1";
nodeParent.ChildNodes.Add(nodeChild);
nodeChild = new TreeNode();
nodeChild.Text = "Child1";
nodeParent.ChildNodes.Add(nodeChild);
}
}
protected void tvwChecked(object sender, EventArgs e)
{
string strText = ((TreeView)sender).SelectedNode.Text;
}
In strText you get the text as "Child1", here ads checked ="true" then it comes as checked mode.
And need more information from you. After select the nodes , whats your next step. you sending these to database or based on that r u getting any.
There is also some stuff on here:
http://forums.asp.net/p/1250727/2316073.aspx
protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
{
e.Node.Text = "<input type='radio' />" + e.Node.Text;
}
Additional links on the same subject
http://forums.asp.net/p/1626167/4180796.aspx and http://www.codeproject.com/KB/tree/TreeViewRadioBox.aspx

Categories