Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#windows应用程序的DataGridView单元格中动态添加表?_C#_Winforms_Datagridview_Windows Applications - Fatal编程技术网

如何在C#windows应用程序的DataGridView单元格中动态添加表?

如何在C#windows应用程序的DataGridView单元格中动态添加表?,c#,winforms,datagridview,windows-applications,C#,Winforms,Datagridview,Windows Applications,我正在用C#开发一个windows应用程序,我有一个DataGridView,当前显示一个数据源的值。我想添加一个列,其中每一行都有一个表。现在有什么方法可以动态地这样做吗?因为每行的表结构都会有所不同 提前谢谢 编辑:-我的意思是,我想在单元格中动态插入一个表 当我尝试在每一行中动态添加TableLayoutPanel时,您可以看到实体详细信息列显示了这一点。本文: 在 DataGridView控件提供了几种列类型,使用户能够以多种方式输入和编辑值。 但是,如果这些列类型不满足您的数据输入需

我正在用C#开发一个windows应用程序,我有一个
DataGridView
,当前显示一个数据源的值。我想添加一个列,其中每一行都有一个表。现在有什么方法可以动态地这样做吗?因为每行的表结构都会有所不同

提前谢谢

编辑:-我的意思是,我想在单元格中动态插入一个表

当我尝试在每一行中动态添加
TableLayoutPanel
时,您可以看到
实体详细信息列显示了这一点。

本文:

DataGridView控件提供了几种列类型,使用户能够以多种方式输入和编辑值。 但是,如果这些列类型不满足您的数据输入需要,您可以使用承载您选择的控件的单元格创建自己的列类型

为此,必须定义派生自DataGridViewColumn和DataGridViewCell的类

还必须定义从控件派生的类,并实现IDataGridViewEditingControl接口

所以我建议,首先让您的表成为具有属性或方法的独立控件 然后使用该控件为每一行显示—在执行数据绑定时,让每一行为TableControl设置数据

另一种方法可以是这样的(假设您没有在网格中编辑)-您可以创建一个类似usercontrol的单行(创建一个usercontrol,其中所有文本框或标签都像datagridrow样式一样排列在一起,使其看起来像一行),然后将它们添加到带有滚动条的面板中

我认为这不可能(容易)用微软提供的现成控件实现

如果你有预算,也许可以看看Telerik的控制系统

他们有一个gridview控件作为.NET WinForm控件的一部分,看起来它可以做您希望它做的事情

否则,是否有可能更改您的体系结构并采用基于ASP.NET web的方法来解决此问题

希望有帮助。祝你好运。

1)。试用MSDN的Mark Ridout,并阅读他的使用说明

还可以看看使用MarkRidout的TreeGridView是否有用

2) 。如果免费控制不起作用,请查看第三方(我与这些公司无关):







3) 。我敢肯定,将TablePanelLayout添加到网格单元格中是而不是您想要的,下面是代码,您可以看到它对您自己来说有多狡猾:

DataTable dt = new DataTable();
dt.Columns.Add("name");
for (int j = 0; j < 10; j++)
{
    dt.Rows.Add("");
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[0].Width = 200;
//add tableLayoutPanel1 into the control collection of the DataGridView
this.dataGridView1.Controls.Add(tableLayoutPanel1);
//resize the row
this.dataGridView1.Rows[1].Height = 100;
//set its location and size to fit the cell
tableLayoutPanel1.Location = this.dataGridView1.GetCellDisplayRectangle(0,1, true).Location;
tableLayoutPanel1.Size = this.dataGridView1.GetCellDisplayRectangle(0, 1, true).Size;
DataTable dt=newdatatable();
dt.列。添加(“名称”);
对于(int j=0;j<10;j++)
{
dt.行。添加(“”);
}
this.dataGridView1.DataSource=dt;
this.dataGridView1.Columns[0].Width=200;
//将tableLayoutPanel1添加到DataGridView的控件集合中
this.dataGridView1.Controls.Add(tableLayoutPanel1);
//调整行的大小
this.dataGridView1.Rows[1].Height=100;
//设置其位置和大小以适合单元格
tableLayoutPanel1.Location=this.dataGridView1.GetCellDisplayRectangle(0,1,true).Location;
tableLayoutPanel1.Size=this.dataGridView1.GetCellDisplayRectangle(0,1,true).Size;

根据我的说法,在
DataGridView中添加包含表格的列是不可能的。。

解决您的问题的最佳替代方法是自行创建一个动态表,并重复行以显示您的数据,在该行中,您可以将一个包含表的单元格放在其中。

您可以使用RowDatabound事件将表添加到cells控件集合中。类似于此示例的内容(抱歉-与您正在做的不同,但可能与您想要的类似,也是在VB中,而不是在C#中)


你需要提供一个你想要的例子。目前很难理解您想要什么…如果您有任何问题或顾虑,请告诉我们?干杯:)
我们希望答案能得到事实、参考资料或特定专业知识的支持。
我刚刚展示了这是可能的。。。
Private Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    Dim i As Integer

    Dim myTable As Table
    Dim myRow As TableRow
    Dim mycell As TableCell
    If e.Row.RowType = DataControlRowType.DataRow Then
        myTable = New Table
        myRow = New TableRow
        For i = 1 To 3
            mycell = New TableCell
            mycell.Text = i.ToString
            mycell.BorderStyle = BorderStyle.Solid
            mycell.BorderWidth = 0.2
            mycell.Width = 15
            If i = 1 Then mycell.BackColor = Drawing.Color.Beige
            If i = 2 Then mycell.BackColor = Drawing.Color.Yellow
            If i = 3 Then mycell.BackColor = Drawing.Color.Green
            myRow.Cells.Add(mycell)
        Next
        myTable.Rows.Add(myRow)
        e.Row.Cells(12).Controls.Add(myTable)
    End If
End Sub