点击加号后是不是DataTable里嵌套了一个TreeView?这是如何实现的?

解决方案 »

  1.   

    treeview就能实现啊!多加几个根节点而已嘛!
      

  2.   

    DataGrid可以浏览DataSet中的关系。
    1.DataGrid通过SetDataBinding()方法设置含有关系的DataSet数据;
    2.通过每个记录左边的“+”号展开关系列表,点击链接打开子表,在子表中点击“箭头”返回父表;
    3.VS2005的工具箱中使用DataGridView替换了DataGrid,需要手动添加或者直接使用代码创建。实例:
    TestDataGrid:Form1.cs:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.Configuration;namespace TestDataGridViewDataSet
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                string select1 = "Select CategoryID, CategoryName FROM Categories";
                string select2 = "Select ProductID, ProductName, CategoryID, Quantity FROM Products";            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["MGC"].ConnectionString))
                {
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter(select1, conn);
                    DataSet ds = new DataSet();
                    da.Fill(ds, "Categories");
                    da = new SqlDataAdapter(select2, conn);
                    da.Fill(ds, "Products");                //建立关系
                    ds.Relations.Add("CategoriesProducts", ds.Tables["Categories"].Columns["CategoryID"], ds.Tables["Products"].Columns["CategoryID"]);
                    //将含有关系的DataSet数据添加到DataGrid中
                    dataGrid1.SetDataBinding(ds, "Categories");
                        
                    conn.Close();
                }
            }
        }
    }
    Form1.Designer.cs:namespace TestDataGridViewDataSet
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.dataGrid1 = new System.Windows.Forms.DataGrid();
                ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(244, 319);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 1;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // dataGrid1
                // 
                this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                            | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));
                this.dataGrid1.DataMember = "";
                this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
                this.dataGrid1.Location = new System.Drawing.Point(-2, 0);
                this.dataGrid1.Name = "dataGrid1";
                this.dataGrid1.Size = new System.Drawing.Size(568, 313);
                this.dataGrid1.TabIndex = 2;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(565, 354);
                this.Controls.Add(this.button1);
                this.Controls.Add(this.dataGrid1);
                this.Name = "Form1";
                this.Text = "Form1";
                ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;
            private System.Windows.Forms.DataGrid dataGrid1;
        }
    }
    App.config:<?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <connectionStrings>
        <add name="MGC" connectionString="server=.\sqlexpress; integrated security=SSPI; database=MGC"/>
      </connectionStrings>
    </configuration>