想要在现有datagridview的选定行中添加新的datagridview

本文关键字:datagridview 添加 | 更新日期: 2023-09-27 17:54:33

我有一个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;
    }

想要在现有datagridview的选定行中添加新的datagridview

我在另一个堆栈溢出问题中发现了这个例子,我认为这可能对你有帮助,'谢谢你。

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();
        }
    }
}