Click here to Skip to main content
12,511,882 members (51,144 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0 C#3.5 C#5
Suppose that we have treeview with checkbox including node1 and sub node 1.1,1.2,1.3,1.4.
i want to do this that when a user select all of the sub nodes the main node selected automatically and if user select some of the sub nodes the main node selected in other way.like a filled square.
how can i do this??????
Posted 24-Jul-11 22:14pm
Updated 8-Aug-16 1:55am
Comments
keerth516 4-Jul-14 2:57am
   
I have done the same process in WPF.do you require any help please let me know.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

sir, you need to loop through every node and check if all the childs are selected and if yes you need to programitically check the parent true.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Hi,

I think this is the default functionality present in Telerik RadTreeView.
http://demos.telerik.com/aspnet-ajax/treeview/examples/overview/defaultcs.aspx[^]

It may also be present in asp TreeView. Try and search and you will know.

Regards,
Praneet
  Permalink  
Comments
CHill(Hawkeye)60 8-May-14 7:08am
   
The question is nearly 3 years old !! (Nor did the OP mention ASP.net)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Desing code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
 
        function postBackByObject() {
            var o = window.event.srcElement;
            if (o.tagName == "INPUT" && o.type == "checkbox") {
                __doPostBack("", "");
            }
        }
   </script>
 

</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All"
 
            AutoGenerateDataBindings="False" ExpandDepth="0"
               EnableClientScript="true"
            ontreenodepopulate="TreeView1_TreeNodePopulate"
            ontreenodecheckchanged="TreeView1_TreeNodeCheckChanged"
            onselectednodechanged="TreeView1_SelectedNodeChanged" >
 
        </asp:TreeView>
 

     <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
 

 

    </div>
    </form>
</body>
</html>


Cs code :



public partial class Default2 : System.Web.UI.Page
{
    DataTable dt = new DataTable();
    private SqlConnection con;
    protected void Page_Load(object sender, EventArgs e)
    {
            TreeView1.Attributes.Add("onclick", "postBackByObject()");
 
            if (!IsPostBack)
            {
                string cs = "server=sql2005 ;database=dbName; uid=sa;pwd=123";
                con = new SqlConnection(cs);
                con.Open();
                SqlCommand cme = new SqlCommand("select * from tbName where parenttab=0 and roleid=1", con);
                //cme.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cme);
                DataSet ds = new DataSet();
                da.Fill(ds, "T");
 
                dt = ds.Tables["T"];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeNode no = new TreeNode();
                    no.Text = dt.Rows[i]["LinkName"].ToString();
                    // no.SelectAction = TreeNodeSelectAction.SelectExpand;

                    no.Value = dt.Rows[i]["LinkId"].ToString();
                    int a = Convert.ToInt32(dt.Rows[i]["LinkId"]);
 
                    // int a = Convert.ToInt32(dt.Rows[i]["LinkId"]);
                    AddTrem(no,a);
                    this.TreeView1.Nodes.Add(no);
 
                }
            }
    }
 
    public void AddTrem(TreeNode no,int a)
    {
 
        SqlCommand cme = new SqlCommand("select * from tbName where parenttab="+a, con);
        SqlDataAdapter da = new SqlDataAdapter(cme);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable dt = new DataTable();
        dt = ds.Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            TreeNode childnode = new TreeNode();
            childnode.Text = dt.Rows[i]["LinkName"].ToString();
            childnode.Value = dt.Rows[i]["linkid"].ToString();
           // AddSubject(childnode);
            no.ChildNodes.Add(childnode);
        }
    }
}
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

May this help you.

[^]
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

Do Not Rely On The ListView for Processing

UserControls are strictly designed to be interfaces, In such a case I would have to recommend creating a data structure to work with a class that has inherited a collection interface. I suggest this, because custom events can be fired to replace for loops and that makes the application less process intensive.

Here is an example: [C#] Data Structures with Interface Example File - Pastebin.com[^]

Then, all we have to do is update the collection from the PrimaryKey's, which in the class I provided I am assuming that the PrimaryKey is a Unique Identifier.

This Becomes MyClass[listView.selectedIndex[0]].Column = value;

Which, whenever changed should fire an event, containing the new and old information to find it in the listview.

What now? Out of for loops! Which can get extremely process intensive. Especially if the database is commercial.
  Permalink  
Comments
Richard Deeming 8-Aug-16 9:24am
   
This question was asked, answered, and solved over FIVE YEARS AGO.

Your "solution" has nothing to do with the question.
kbhtech 15-Aug-16 13:26pm
   
Yeah, I wasn't paying full attention that day. I had finals going on and was just scanning for content here while publishing content.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160929.1 | Last Updated 8 Aug 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100