如何在treeview中的不同节点上绑定gridview中不同列与数据库中的不同列? [英] How to bind different columns in gridview from datatable from database on differenet nodes in treeview ?

查看:79
本文介绍了如何在treeview中的不同节点上绑定gridview中不同列与数据库中的不同列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据库树视图代码...我想点击父节点显示Id-从0到9的行,当我点击第一个孩子,例如0,Id从00到09,on child 1 Id从10到19 ....当我点击第二个孩子例如00时,显示从000到009的id ......来自数据库的相同数据表。



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.

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]



[edit by="nirav prabtani"]
Set language codeblock and remove extra spaces
[/edit]

推荐答案

这篇关于如何在treeview中的不同节点上绑定gridview中不同列与数据库中的不同列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆