簡體   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