2014-03-28 67 views
0

我有一个datagridview,而双击一行,它应该增加其高度,并显示该行内的另一个datagridview。如何插入另一个DataGridView。 我的代码如下想要添加新的datagridview现有datagridview选定行内

private void Form1_Load(object sender, EventArgs e) 
    { 
     DataGridViewColumn col = new DataGridViewTextBoxColumn(); 
     col.HeaderText = "Name"; 
     dataGridView1.Columns.Add(col); 
     DataGridViewColumn col1 = new DataGridViewTextBoxColumn(); 
     col1.HeaderText = "Address"; 
     dataGridView1.Columns.Add(col1); 
     DataGridViewColumn col2 = new DataGridViewTextBoxColumn(); 
     col2.HeaderText = "Age"; 
     dataGridView1.Columns.Add(col2); 
     DataGridViewColumn col3 = new DataGridViewTextBoxColumn(); 
     col3.HeaderText = "Education"; 
     dataGridView1.Columns.Add(col3); 
     for (i = 0; i < 5; i++) 
     { 
      dataGridView1.Rows.Add(); 
      dataGridView1.Rows[i].Cells[0].Value = "Name"; 
      dataGridView1.Rows[i].Cells[1].Value = "Address"; 
      dataGridView1.Rows[i].Cells[2].Value = "Age"; 
      dataGridView1.Rows[i].Cells[3].Value = "Education"; 
     } 
    } 

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e) 
    { 
     this.dataGridView1.Rows[dataGridView1.CurrentRow.Index].Height = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Height + 40; 
    } 

回答

0

我发现在另一个堆栈溢出问题这个例子,我想这可能帮助你,\ 谢谢。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

public partial class CS : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      gvCustomers.DataSource = GetData("select top 10 * from Customers"); 
      gvCustomers.DataBind(); 
     } 
    } 

    private static DataTable GetData(string query) 
    { 
     string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; 
     using (SqlConnection con = new SqlConnection(strConnString)) 
     { 
      using (SqlCommand cmd = new SqlCommand()) 
      { 
       cmd.CommandText = query; 
       using (SqlDataAdapter sda = new SqlDataAdapter()) 
       { 
        cmd.Connection = con; 
        sda.SelectCommand = cmd; 
        using (DataSet ds = new DataSet()) 
        { 
         DataTable dt = new DataTable(); 
         sda.Fill(dt); 
         return dt; 
        } 
       } 
      } 
     } 
    } 

    protected void OnRowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.DataRow) 
     { 
      string customerId = gvCustomers.DataKeys[e.Row.RowIndex].Value.ToString(); 
      GridView gvOrders = e.Row.FindControl("gvOrders") as GridView; 
      gvOrders.DataSource = GetData(string.Format("select top 3 * from Orders where CustomerId='{0}'", customerId)); 
      gvOrders.DataBind(); 
     } 
    } 
}