Click here to Skip to main content
15,902,276 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
This is my code for treeview from database... I want when I click on parent node to show rows with Id- from 0 to 9, when i click on first child for example 0, Id from 00 to 09, on child 1 Id from 10 to 19....When I click on second child for example 00 to show id from 000 to 009 ......from the same datatable from database.

C#
protected void Page_Load(object sender, EventArgs e)
  
      {
            if (!IsPostBack)
            {
                GetTreeViewItems();

            }
        }
        private void GetTreeViewItems()
        {
            string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            SqlDataAdapter da = new SqlDataAdapter("spGetTreeViewItems", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ШифраНаКонто"], ds.Tables[0].Columns["ParentId"]);


            foreach (DataRow level1DataRow in ds.Tables[0].Rows)
            {

                if (string.IsNullOrEmpty(level1DataRow["ParentId"].ToString()))
                {

                    TreeNode parentTreeNode = new TreeNode();
                    parentTreeNode.Text = level1DataRow["ШифраНаКонто"].ToString() + level1DataRow["НазивКонто"].ToString();

                    GetChildNodes(level1DataRow, parentTreeNode);
                    TreeView1.Nodes.Add(parentTreeNode);
                }
            }
        }
        private void GetChildNodes(DataRow dataRow, TreeNode treeNode)
        {

            DataRow[] childRows = dataRow.GetChildRows("ChildRows");
            foreach (DataRow childRow in childRows)
            {
                TreeNode childTreeNode = new TreeNode();
                childTreeNode.Text = childRow["ШифраНаКонто"].ToString() + childRow["НазивКонто"].ToString();
                treeNode.ChildNodes.Add(childTreeNode);
                if (childRow.GetChildRows("ChildRows").Length > 0)
                {
                    GetChildNodes(childRow, childTreeNode);
                }
            }

        }
        protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            Loadgr();
        }
        void Loadgr()
        {
            string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);

            SqlCommand com = new SqlCommand("select * from КонтенПлан where ШифраНаКонто = @ШифраНаКонто", con);
            SqlDataAdapter adap = new SqlDataAdapter(com);
            com.Parameters.Add("@ШифраНаКонто", SqlDbType.Int).Value = this.TreeView1.SelectedNode.Text;
            DataTable tab = new DataTable();
            adap.Fill(tab);
            GridView1.DataSource = tab;
            GridView1.DataBind();


        }
        protected void txtnaziv_TextChanged(object sender, EventArgs e)
        {

            TextBox tsifra = (TextBox)GridView1.FooterRow.Cells[0].FindControl("txtsifra");
            TextBox tnaziv = (TextBox)GridView1.FooterRow.Cells[1].FindControl("txtnaziv");
            TextBox sifra = (TextBox)GridView1.FindControl("lblsifra");
            TextBox naziv = (TextBox)GridView1.FindControl("lblnaziv");

            tnaziv.TextChanged += new EventHandler(txtnaziv_TextChanged);
            string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            con.Open();
          
            SqlCommand cmd = new SqlCommand("insert into КонтенПлан(ШифраНаКонто,НазивКонто) values('" + tsifra.Text + "','" + tnaziv.Text + "')", con);
            int result = cmd.ExecuteNonQuery();

          
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM КонтенПлан Where ШифраНаКонто BETWEEN 0 AND 9", con);
            DataSet ds = new DataSet();

            ad.Fill(ds, "КонтенПлан");

            GridView1.DataSource = ds;


            con.Close();
            if (result == 1)
            {
                GridView1.DataBind();

            }
              cmd.Connection = con;
              cmd.CommandText = "Update КонтенПлан set ШифраНаКонто='" + sifra.Text + "',НазивКонто='" + naziv.Text + "' where ШифраНаКонто='" + sifra.Text + "'";

        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string cs = ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(cs);
                con.Open();
                
                con.Close();
                GridView1.EditIndex = -1;
                 
            }
        }


[edit by="nirav prabtani"]
Set language codeblock and remove extra spaces
[/edit]
Posted
Updated 15-Jul-14 20:27pm
v5
Comments
KaushalJB 15-Jul-14 9:41am    
Have you applied any code ? Please update your question in some relevant way, so that viewers can understand what are you expecting to the point.. Don't just post sentences. Read FAQ
Maciej Los 15-Jul-14 10:32am    
Not enough information...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900