繁体   English   中英

在Winforms应用程序中使用C#将数据库数据绑定到树形视图

[英]Binding the database data to tree view using c# in winforms applications

我有一个名为类别的表....带有列...

                                category_id
                                categoryname

我只想在树形视图中表示类别名称....

是否可以像这样将类别名称绑定到树视图...

                           Category 
                              categoryname 1
                              categoryname 2
                              categoryname 3

是否有可能使用C#..我正在使用Windows应用程序....

任何人都可以帮忙...

提前谢谢了..

在更多的Web表单中,您可以简单地将数据集绑定到Treeview控件,但在Win表单中,则没有任何数据。 在此示例中,我继承了System.Windows.Forms.TreeView类,并编写了自己的代码以将数据集绑定到Win Treeview控件,并且在我的应用程序中使用了3年以上,并且效果很好。

首先:您必须将类创建为Custom_Tree_View.cs并在其中添加以下代码

 public class Custom_Tree_View : System.Windows.Forms.TreeView
{
    DataSet dsMenu = new DataSet();
    public TreeNodeMouseClickEventHandler TreeNode_Click_Event;

    float FontSize = 9F;
    string FontName = "Tahoma";
    public string Data_Key_Member;
    public string Data_Key_Value;
    private ImageList imageList1;
    private System.ComponentModel.IContainer components;
    public string Data_Parent_Key;


    public bool ShowCheckBox = false;

    public Custom_Tree_View()
    {
        this.Font = new System.Drawing.Font(this.FontName, this.FontSize);          
        this.NodeMouseClick += NodeMouseClicked;
    }


    public void CreateMenu(DataSet _dsMenu)
    {
        try
        {

            this.Nodes.Clear();

            if (_dsMenu == null) return;
            this.CheckBoxes = this.ShowCheckBox;
            this.dsMenu = _dsMenu;

            string Filter = this.Data_Parent_Key + " IS NULL";
            foreach (DataRow drTemp in dsMenu.Tables[0].Select(Filter))
            {
                Create_Parent_Tree_Node(drTemp[0].ToString());
            }



        }
        catch (Exception ex)
        {
            throw ex;
        }
    }




    private void Create_Parent_Tree_Node(string strMenu)
    {
        try
        {

            TreeNode mmru = new TreeNode(GetMenu_Details_Title(strMenu));
            mmru.Name = strMenu;
            if (dsMenu.Tables[0].Select(this.Data_Parent_Key + "='" + strMenu + "'").Length > 0)
            {

                this.Nodes.Add(mmru);
                foreach (DataRow drTemp in dsMenu.Tables[0].Select(this.Data_Parent_Key + "='" + strMenu + "'"))
                {
                    Create_Child_Tree_Node(mmru, drTemp[0].ToString());
                }
            }
            else
            {
                mmru.ForeColor = System.Drawing.Color.Red;
                this.Nodes.Add(mmru);
            }


        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


    private string Create_Child_Tree_Node(TreeNode mnuItem, string strMenu)
    {
        try
        {
            if (dsMenu.Tables[0].Select(this.Data_Parent_Key + "='" + strMenu + "'").Length > 0)
            {
                TreeNode mmru = new TreeNode(GetMenu_Details_Title(strMenu));

                mmru.Name = strMenu;
                mnuItem.Nodes.Add(mmru);
                foreach (DataRow drTemp1 in dsMenu.Tables[0].Select(this.Data_Parent_Key + "='" + strMenu + "'"))
                {

                    Create_Child_Tree_Node(mmru, drTemp1[0].ToString());
                }
            }
            else
            {
                TreeNode mmru = new TreeNode(GetMenu_Details_Title(strMenu));
                mmru.Name = strMenu;
                mmru.ForeColor = System.Drawing.Color.Red;

                mnuItem.Nodes.Add(mmru);
                return strMenu;
            }
            return strMenu;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private string GetMenu_Details_Title(string TreeNode_ID)
    {

        try
        {              
            return dsMenu.Tables[0].Select(this.Data_Key_Value + "=" + TreeNode_ID + "")[0][this.Data_Key_Member].ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private bool GetMenuEnable(string TreeNode_ID)
    {
        try
        {
            if (dsMenu.Tables[0].Select(this.Data_Key_Value + "='" + TreeNode_ID + "'")[0][3].ToString() == "Y")
                return true;
            else
                return false;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }


    public TreeNode SelectedTreeNode;


    private void NodeMouseClicked(object sender, TreeNodeMouseClickEventArgs e)
    {
        try
        {                

            if (dsMenu.Tables[0].Select(this.Data_Parent_Key + "='" + e.Node.Name + "'").Length > 0)
            {
                return;
            }
            else
            {
                if (this.TreeNode_Click_Event != null)
                    TreeNode_Click_Event(sender, e);
            }


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.imageList1 = new System.Windows.Forms.ImageList(this.components);
        this.SuspendLayout();
        // 
        // imageList1
        // 
        this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
        this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
        this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
        // 
        // Custom_Tree_View
        // 
        this.Font = new System.Drawing.Font("Tahoma", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
        this.ItemHeight = 25;
        this.LineColor = System.Drawing.Color.Black;
        this.RightToLeft = System.Windows.Forms.RightToLeft.Yes;
        this.RightToLeftLayout = true;
        this.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.Custom_Tree_View_NodeMouseClick);

        this.ResumeLayout(false);

    }

    private void Custom_Tree_View_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        try
        {
            if (e.Node.Checked == true)
            {

                foreach (TreeNode Node in e.Node.Nodes)
                {

                    Node.Checked = true;
                }
            }



            if (e.Node.Checked == false)
            {

                foreach (TreeNode Node in e.Node.Nodes)
                {

                    Node.Checked = false;
                }
            }






        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);
        }
    }

}

第二:
在您的项目中,将New Control添加到窗体上,并在下面的代码中将数据集绑定到treeview控件

DataSet Menu_Ds = new DataSet();              
Menu_Ds =  GetMyDataSet();
this.custom_Tree_View_Menu.Data_Key_Member = "Menu_Details_Title";
this.custom_Tree_View_Menu.Data_Key_Value ="Menu_Details_Id";
this.custom_Tree_View_Menu.Data_Parent_Key = "Menu_Details_Parent";
this.custom_Tree_View_Menu.CreateMenu(Menu_Ds);

注意:在数据集中,您必须具有树字段“标题”,“ ID”和“ Parent_Id”字段才能正确创建树视图

我希望这篇文章对所有人都有用,对于我的英语不好,我感到抱歉

这是使用标准树视图控件在Windows窗体中无法实现的,但这是可行的。

最好的方法是从TreeView派生您自己的控件,创建DataSource属性。 分配DataSource您的控件应基于数据构建其结构。 您可能需要一些其他属性来描述具有节点文本的列以及具有ID和父ID的列。

如果要使用更完整的方法,还应该考虑向DataSource对象订阅一些其他事件,并对DataSource事件更改做出反应。

有趣的设计决策是您要一次构建整棵树还是等待用户扩展给定节点。

您还可以在CodeProject上找到至少两个工作示例:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        MenuDS ds = new MenuDS();

        CategoryTableAdapter daCategory = new CategoryTableAdapter();
        ProductTableAdapter daProduct = new ProductTableAdapter();

        daCategory.Fill(ds.Category);
        daProduct.Fill(ds.Product);

        if (ds.Tables[0].Rows.Count > 0)
        {
            TreeView1.Nodes.Clear();
            foreach (DataRow dr in ds.Category.Rows)
            {
                TreeNode mastreNode = new TreeNode(dr["cateNAme"].ToString(), dr["Id"].ToString());
                TreeView1.Nodes.Add(mastreNode);
                TreeView1.CollapseAll();

                DataTable dt = daProduct.GetDataBy(Convert.ToInt32((dr["Id"])));

                foreach (DataRow drNew in dt.Rows)
                {
                    TreeNode childNode = new TreeNode(drNew["prodName"].ToString(), drNew["Id"].ToString());
                    childNode.NavigateUrl = "~/ProductDetails.aspx?Id=" + drNew["Id"].ToString();
                    mastreNode.ChildNodes.Add(childNode);

                }
            }
        }
    }



}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM