我希望根据DataSet中的表个数 需要生成对应的dataGridView,
并将dataGridView放到Panel中。
生成的dataGridView的DataSource对应DataSet中的表
         private void CreateC1Grid(DataSet ds)
        {
            for (int i = 0; i <= ds.Tables.Count- 1; i++)
            {
                 //代码
            }
        }
//调用
CreateC1Grid(ds);

解决方案 »

  1.   


             private void CreateC1Grid(DataSet ds)
            {
                for (int i = 0; i <= ds.Tables.Count- 1; i++)
                {
                     DataGridView dgv = new DataGridView();
                     dgv.Loaction = new Point(0,0);//这里自己调整位置
                     dgv.DataSource = ds.Tables[i];
                     this.panel1.Controls.Add(dgv);
                }
            }
      

  2.   

    类似的,搜的private void button1_Click(object sender, EventArgs e)
            {
                TabPage tp1 = new TabPage(textBox1.Text);
                tp1.Name = textBox1.Text;
                tabControl1.TabPages.Add(tp1);            Type t = typeof(DataGridView);
                ConstructorInfo ci = t.GetConstructor(new Type[] { });
                object o=ci.Invoke(null);
                DataGridView dgv = o as DataGridView;
                dgv.Name = "dgv" + textBox1.Text;
                dgv.DataSource = createDataSource(textBox1.Text);
                
                tp1.Controls.Add(dgv);        }        private DataView createDataSource(string condition)
            {
                string sql = "select * from message where id='" + condition + "'";
                SqlConnection conn = new SqlConnection("server=.;Initial Catalog=test;uid=sa;pwd=**");
                conn.Open();
                SqlDataAdapter myAdapter = new SqlDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                myAdapter.Fill(ds, "message");
                return ds.Tables["message"].DefaultView;
            }
      

  3.   

            private void CreateC1Grid(DataSet ds)
            {
                DataGridView adgv = new DataGridView();
                adgv.Width = 100;
                adgv.Height = 100;
                adgv.DataSource = ds;
                panel1.Controls.Add(adgv);
            }
      

  4.   

    】同意 ,不过最好吧DataGridView dgv变量的定义放在循环外面;