Problem:
I have 5 nodes in Treeview list including child nodes. Whenever I select one node it should display the name of the node in the combo box automatically without the need of pressing a button.
When I use ONselectednodechanged event it only fires once. So when I click on the node for the first time it fires the event but it does not do anything after that.
Could someone please give me some suggestions on what to do. I want it to automatically display the name of the node in the combo box when I click on it.
c#, ASP.net
Here is what I wrote so far:
protected void nav_tree_items_SelectedNodeChanged(object sender, EventArgs e)
{
if (nav_view_add.Visible == true)
{
panel_helper.GroupingText = "Add";
nav_multi.SetActiveView(nav_view_add); //set active view
nav_btn_save_add.CausesValidation = true;
DataRow[] row1 = TableClass.MainTable.Select("ItemId = " + nav_tree_items.SelectedValue);
nav_dd_parent.SelectedValue = row1[0]["ItemId"].ToString().Trim();
}
When the program is executed it does not even make the panel visible.
<asp:TreeView ID="nav_tree_items" runat="server" Height="100%" ShowLines="True"
Width="123%" onselectednodechanged="nav_tree_items_SelectedNodeChanged">
<HoverNodeStyle CssClass="hoverTreeItem" />
When I work with TreeView, I use the "AfterSelect" event. The stub can be easily generated by double clicking the TreeView control in [Design] mode. You can then do something like the following, using the data however you wish (MessageBox is just an example).
private void naviTree_AfterSelect(object sender, TreeViewEventArgs e)
{
MessageBox.Show("The node you just selected is [" + e.Node.ToString() + "]");
}
Hope this helps at all.
Related
I have a C# project (VS 2017) that uses a Data Grid View to show data in an object list. Using a contextMenuStrip I want to be able to right click on a row and be able to remove it from the datagridview and the underlying datasource.
I have the contextMenuStrip set in the Properties of the Datagridview with one item with the following to methods to handle the events.
private void dgv_Test_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
var hti = dgv_Test.HitTest(e.X, e.Y);
dgv_Test.ClearSelection();
dgv_Test.Rows[hti.RowIndex].Selected = true;
}
}
private void cms_DGV_Remove_Click(object sender, EventArgs e)
{
MessageBox.Show("Content Menu Clicked on Remove Option");
PersonModel temp = (PersonModel)dgv_Test.CurrentRow.DataBoundItem;
string msg = $"The index for the selected Person is {temp.Id}.";
MessageBox.Show(msg);
}
I expect this to sent the current row to the row that is right clicked on. This is not happening as the CurrentRow is staying on the top row. It does work if I first Left click on the row then right click the same row.
The problem you are describing is coming from the cms_DGV_Remove_Click event. When the user right-clicks on the grid, this is NOT going to make the cell/row underneath the cursor the grid’s CurrentRow. Even though the code in the dgv_Test_MouseDown method sets the row to “selected”…. it isn’t necessarily going to be the “current” row. The grids CurrentRow property is read only and you cannot directly set it from your code.
Given this, it is clear that getting the mouse coordinates “in relation to the grid” FROM the context menu will take a little effort since its coordinates are global. You have apparently noticed this from wiring up of the grids MouseDown event. This event makes it easy to capture the mouse position in relation to the grid. Problem is… you are NOT saving this info. By the time the context menu fires, that info is LOST.
Solution: make the DataGridView.HitTest info global. Then, set it every time the user right clicks into the grid. With this global variable set, when the context menu fires it will know which row the cursor is under.
DataGridView.HitTestInfo HT_Info; // <- Global variable
private void dgv_Test_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Right) {
HT_Info = dgv_Test.HitTest(e.X, e.Y);
if (HT_Info.RowIndex >= 0) {
dgv_Test.ClearSelection();
dgv_Test.Rows[HT_Info.RowIndex].Selected = true;
}
}
}
It does not appear that the posted code is actually removing the row, however below is how the context menu “remove” event may look…
Below should work for a non-data bound grid and also a grid with a data bound DataTable.
private void cms_DGV_Remove_Click(object sender, EventArgs e) {
if (HT_Info.RowIndex >= 0) {
dgv_Test.Rows.Remove(dgv_Test.Rows[HT_Info.RowIndex]);
}
}
If you are using a List<T>, the method to remove may look something like below...
private void cms_DGV_Remove_Click(object sender, EventArgs e) {
if (HT_Info.RowIndex >= 0) {
PersonModel targetPerson = (PersonModel)dgv_Test.Rows[HT_Info.RowIndex].DataBoundItem;
AllPersons.Remove(targetPerson);
dgv_Test.DataSource = null;
dgv_Test.DataSource = AllPersons;
}
}
I am guessing this is the behavior you are looking for. The user right-clicks into the grid, the row under the cursor is selected and the context menu “remove” pops up. The user can either “select” remove to remove the row or click away from the context menu to cancel the remove.
Hope that makes sense.
In my Windows Form, there are two User Controls, placed one on top of the other.
I also have a TreeView Structure (TreeView1) that has a root node (with two child nodes itself, with check boxes).
Basically, I wish to make only one User Control visible when the Tree View Node corresponding to that User Control is checked.
This is the code that I've written to respond to the checking event:
private void TreeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
string Case;
Case = e.Node.Name;
switch (Case)
{
case "Call_UC1": //Name of the first node of TreeView Structure
UC1.BringToFront(); //UC1 - object of the User Control 1
UC1.Visible = true;
break;
case "Call_UC2": //Name of the second node TreeView Structure
UC2.BringToFront(); //UC2 - object of the User Control 2
UC2.Visible = true;
break;
default:
break;
}
}
Problem is, the User Controls are not responding when I check any of the check boxes of either node. Nothing is happening. I'm guessing that my implementation of the TreeView Event Handler was not proper. Could anyone help me out?
AfterCheck() also fires when a node is UNCHECKED...you need to check for this. Also, do you have code in place that prevents both of the boxes from being checked at the same time? Otherwise, which one should be in front? Whatever one was last checked?
...and what happens if both are not checked after previously having been checked? Should the UserControls be invisible?
So many questions...
This ~might~ be what you're after:
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Checked)
{
if(e.Node.Name == "Call_UC1")
{
UC1.Visible = true;
UC1.BringToFront();
}
else if (e.Node.Name == "Call_UC2")
{
UC2.Visible = true;
UC2.BringToFront();
}
}
}
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;
}
I have a RadGrid within which one of my columns is a GridTemplateColumn, which has a RadComboBox loading some items (the Edit mode is set to 'PopUp').
What I want is, if while searching for an item in the RadComboBox, no item is found, then give the user an option to add a new item. Currently, just for testing purposes, I want to be able to show a message if no item is found. This is what I have tried till now.
My RadComboBox within the RadGrid is defined as follows:
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="Product_PKRadComboBox"
ShowDropDownOnTextboxClick="false" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
EnableLoadOnDemand="true" EnableAutomaticLoadOnDemand="true" ItemsPerRequest="10"
OnItemsRequested="Product_PKRadComboBox_ItemsRequested" AllowCustomText="true"
Filter="StartsWith" DataSourceID="SqlProducts" DataTextField="ProductCode"
DataValueField="Product_PK"></telerik:RadComboBox>
</EditItemTemplate>
So I am checking my logic in the 'OnItemsRequested' event as follows:
protected void Product_PKRadComboBox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
//RadComboBox combo = (RadComboBox)sender;
if (e.EndOfItems && e.NumberOfItems==0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "testMessage", "alert('Product Not Found. Do you want to add a Custom Product?');", true);
//Page.ClientScript.RegisterStartupScript(typeof(Page), "some_name", "if(confirm('here the message')==false)return false;");
}
}
I tried both lines of code within the IF Statement (which is checking if what the user typed in the RadComboBox exists or not, if it doesn't return any items, then show a message), but none of them seem to work. I tried the same in debug mode and set a Breakpoint on the line within the IF statement, it actually DOES execute but I cannot see the alert.
I have just done something similar to this and my solution seems to be working pretty well.
Basically in ItemsRequested, once I know that no matches are found, I add a RadComboBoxItem manually. Give it a meaningful text such as "Add a new product..." and give it a distinct style as well to differentiate it from actual results.
Something like this:
protected void Product_PKRadComboBox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
if (e.EndOfItems && e.NumberOfItems==0)
{
var addItem = new RadComboBoxItem("Add a new product...", "addnewproduct");
addItem.ToolTip = "Click to create a new product...";
addItem.CssClass = "UseSpecialCSSStyling";
Product_PKRadComboBox.Items.Add(addItem);
}
}
You then need to handle the SelectedIndexChanged event when the "addnewproduct" item is selected. Make sure to set the combobox's AutoPostBack="true".
protected void Product_PKRadComboBox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value) && e.Value.Equals("addnewproduct"))
{
// do whatever you have to do to add a new product
}
}
You can use a RadWindow to show a confirmation box that says "Are you sure you want to add a new product?" with Yes and Cancel buttons. Go a step further by displaying the search text that the user typed in in a textbox within the RadWindow. This way the user can modify the text before adding the new item.
For example the user may type "water bottle" to search for a product. No results found so the user click on the "Add a new product..." item. The confirmation box shows up, then the user can change "water bottle" to "Green Durable Water Bottle 600ml" before clicking Yes to actually add the product.
So, I have a GridView on my MasterPage with an onclick Event which returns the ID (there are device components in this grid and I need their ID from the database) from that row.
The Onclick event which is bound in the RowDataBound event
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(this.selectionGridView, "Select$" + e.Row.RowIndex);
When I click a row the SelectedIndexChanged Event gets fired
protected void selectionGridView_SelectedIndexChanged(object sender, EventArgs e)
{
GridView selectedGridView = sender as GridView;
//Saves the Database ID of the selected data
Session["selectedDevID"] = selectedGridView.SelectedRow.Cells[0].Text;
_Auswahl.FillTree();
}
And this piece of code calls a method from the page I want to show a Treeview with more stuff etc.
_Auswahl.FillTree();
Everthing is fine, all methods are called correctly, but on the _Auswahl Page, I can't add things to the TreeView (or to the Page at all). All my controls return a NullReferenceExeption, and if I initialize them, then this stuff won't show up
I tried everything, I guess I miss some little thing or forgot something, but I can't get this thing right ._.
Edit: I don't want to Redirect to that Page again, I want to "refresh" the treeview (which is located in an UpdatePanel)
Edit2:
This is my ___FillTree()_ Methode (Excluded the whole Linq stuff etc etc und left just some basic stuff)
public void FillTree()
{
//Add child node value
TreeNode child = new TreeNode("Manager");
//Add parent value
TreeNode parent = new TreeNode("Development");
//Add child to parent
parent .ChildNodes.Add(child);
//Add parent to tree
TreeView1 = new TreeView();
TreeView1.Nodes.Add(parent);
TreeView1.DataBind();
TreeView1.ExpandAll();
}