Devexpress treelist两张表父子节点设置和分页demo

效果图

Devexpress treelist两张表父子节点设置和分页demo_第1张图片

 

 

网上查了很多例子自己结合和修改了一下。最下方的分页跳转是dev的datapager控件

代码如下

public partial class EntryEquipQueryForm : Form
    {
        public EntryEquipQueryForm()
        {
            this.InitializeComponent();
            equiplist.OptionsBehavior.ReadOnly = true; //只读
            equiplist.OptionsBehavior.Editable = false; //不可编辑
        }
        //页行数
        public int pagesize = 1;
        //当前页
        public int pageIndex = 1;
        //总页数
        public int pageCount;
        /// 
        /// 绑定分页控件和Treelist数据
        /// 
        public void BindPageGridList()
        {        
            nvgtDataPager.Buttons.CustomButtons[0].Enabled = true;//datapager相关的设置可以百度一下
            nvgtDataPager.Buttons.CustomButtons[1].Enabled = true;
            nvgtDataPager.Buttons.CustomButtons[2].Enabled = true;
            nvgtDataPager.Buttons.CustomButtons[3].Enabled = true;
            //记录获取开始数
            int startIndex = (pageIndex - 1) * pagesize + 1;
            //结束数
            int endIndex = pageIndex * pagesize;
            //总行数
            int row = alldata.Rows.Count;
            //获取总页数  
            if (row % pagesize > 0)
            {
                pageCount = row / pagesize + 1;
            }
            else
            {
                pageCount = row / pagesize;
            }
            if (pageIndex == 1 || pageIndex ==0)
            {
                nvgtDataPager.Buttons.CustomButtons[0].Enabled = false;
                nvgtDataPager.Buttons.CustomButtons[1].Enabled = false; 
            }
            //最后页时获取真实记录数
            if (pageCount == pageIndex)
            {
                endIndex = row;
                nvgtDataPager.Buttons.CustomButtons[2].Enabled = false;
                nvgtDataPager.Buttons.CustomButtons[3].Enabled = false;
            }
            //分页获取数据列表
            DataTable dt = GetListByPage( startIndex, endIndex);
            Bind(dt);
            nvgtDataPager.DataSource = dt;
            nvgtDataPager.TextStringFormat = string.Format("第 {0}页, 共 {1}页", pageIndex, pageCount);
        }
        /// 
        /// 按钮点击事件
        /// 
        /// PengZhen
        /// 2013-11-5 14:24:25
        /// 
        /// 
        private void nvgtDataPager_ButtonClick(object sender, NavigatorButtonClickEventArgs e)
        {
            ShowEvent("ButtonClick", e.Button);
        }
        /// 
        /// 分页事件处理
        /// 
        /// 事件名称
        /// 按钮控件
        /// PengZhen
        /// 2013-11-5 14:25:59
        void ShowEvent(string eventString, NavigatorButtonBase button)
        {
            //string type = button.ButtonType.ToString();
            NavigatorCustomButton btn = (NavigatorCustomButton)button;
            string type = btn.Tag.ToString();
            if (type == "首页")
            {
                pageIndex = 1;
            }
            if (type == "下一页")
            {
                pageIndex++;
            }
            if (type == "末页")
            {
                pageIndex = pageCount;
            }

            if (type == "上一页")
            {
                pageIndex--;
            }
            //绑定分页控件和GridControl数据
            BindPageGridList();
        } 
        /// 
        /// 分页获取数据列表
        /// 
        public DataTable GetListByPage( int startIndex, int endIndex)
        {
            DataView dv = alldata.DefaultView;
            dv.RowFilter = " row >= "+startIndex+" and row <= "+endIndex;
            DataTable dt = dv.ToTable();
            return dt;
        }
        //数据查询方法
        public DataTable Getdata()
        {
            string connecttionString = "";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"select (显示字段),ROW_NUMBER()  over(order by (用于排序的字段) desc) as row  from  (父表) where (条件字段) Like '%" + queryname.Text.Trim() + "%' and (条件字段) Like '%" + querycode.Text.Trim() + "%'";
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(connecttionString))
            {
                cmd.Connection = conn;
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(dt);
            }
            return dt;
        }           
        //给treelist绑定数据,
        public void Bind(DataTable dt)
        {
            this.equiplist.ClearNodes();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i)
                 {
                    TreeListNode node = equiplist.AppendNode("EqpDefID", null);
                    node.SetValue(设备识别号, dt.Rows[i]["EqpDefPK"]);
                    node.SetValue(设备编码, dt.Rows[i]["EqpDefID"]);
                    node.SetValue(设备名称, dt.Rows[i]["EqpDefName"]);
                    node.SetValue(设备大类, dt.Rows[i]["TypeName"]);
                    node.SetValue(设备小类, dt.Rows[i]["ClassName"]);
                    //nodeTemp.Tag = dt.Rows[i];
                    GetChildNode( node, dt.Rows[i]["(父子节点绑定字段)"].ToString());
                }
            }
        }
        //子节点绑定
        public void GetChildNode(TreeListNode node, string parent)
        {
            string connecttionString = "";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = @"select (显示字段)  from  (子节点表)  where  (父子节点绑定字段)=" + "'"+parent+"'";
            DataTable dt = new DataTable();
            using (SqlConnection conn = new SqlConnection(connecttionString))
            {
                cmd.Connection = conn;
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(dt);
            }
            if (dt.Rows.Count >0)
              {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TreeListNode nodelist = node.TreeList.AppendNode(dt.Rows[i]["(父子节点绑定字段)"], node);
                    nodelist.SetValue((字段显示位置), dt.Rows[i]["VLabel"]);
                    nodelist.SetValue((字段显示位置), dt.Rows[i]["VLabel2"]);
                }
            }
        } 
        #region 递归算法
        /// 
        /// 获取选中的树节点 特定列的值
        /// 
        /// 起始节点
        /// 特定列
        /// 选中数据  ,号分开
        public string GetCheckedNodeValue(TreeListNode parentNode, TreeListColumn column)
        {
            string nodeValues = string.Empty;

            GetCheckedNodeValue(parentNode, column, ref nodeValues);
            nodeValues = nodeValues.Trim(',');

            return nodeValues;
        }

        private void GetCheckedNodeValue(TreeListNode parentNode, TreeListColumn column, ref string columnNode)
        {
            if (parentNode.Nodes.Count == 0)//只获取最后节点
            {

                columnNode += (parentNode.GetValue(column).ToString() + ",");

                return;//递归终止
            }
            foreach (TreeListNode node in parentNode.Nodes)
            {
                GetCheckedNodeValue(node, column, ref columnNode);
            }
        }
        #endregion

        private DataTable alldata;
        private void querybutton_Click(object sender, EventArgs e)
        {
            pageIndex = 1;
            alldata = Getdata();//获取数据
            if (alldata.Rows.Count == 0)
            {
                pageIndex = 0;
            }
            BindPageGridList();
        }
        public string EqpDefID = null;
        public string equiplevel = null;
        public string EqpDefPK = null;
        //public string EqpDefName;
        private void equiplist_DoubleClick(object sender, EventArgs e)//这里是双击提取内容有需要的可以用到
        {
            if (equiplist.FocusedNode.Selected && !equiplist.FocusedNode.HasChildren)
            {
                EqpDefPK = equiplist.FocusedNode.ParentNode.GetValue("设备识别号").ToString();
                EqpDefID = equiplist.FocusedNode.ParentNode.GetValue("设备编码").ToString();
                equiplevel = equiplist.FocusedNode.GetValue("设备编码").ToString();
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
            else if (equiplist.FocusedNode.Selected && equiplist.FocusedNode.HasChildren)
            {
                EqpDefPK = equiplist.FocusedNode.GetValue("设备识别号").ToString();
                EqpDefID = equiplist.FocusedNode.GetValue("设备编码").ToString();      
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
            else {
                return;
            }
        } 
    }

 

先写这么多有问题和建议或者哪里不明白的地方可以直接私聊我,如果觉得有帮助帮我点个赞我让我有动力继续发博客谢谢了。

 

你可能感兴趣的:(Devexpress treelist两张表父子节点设置和分页demo)