【问题标题】:How to add a Table in a cell of DataGridView in C# windows application dynamically?如何在 C# windows 应用程序的 DataGridView 单元格中动态添加表格?
【发布时间】:2013-04-20 19:03:28
【问题描述】:

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

提前致谢。

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

当我尝试在每一行中动态添加TableLayoutPanel 时,您可以看到Entity Details 列显示了这一点。

【问题讨论】:

  • 你需要提供一个你想要的例子。就目前而言,很难理解你想要什么......
  • 如果您有任何问题或疑虑,请告诉我们?干杯:)

标签: c# winforms datagridview windows-applications


【解决方案1】:

有这篇文章:How to: Host Controls in Windows Forms DataGridView Cells

MSDN

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

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

您还必须定义一个派生自 Control 的类并实现 IDataGridViewEditingControl 接口。

所以我建议,首先使用属性或方法将您的表设为独立控件 然后使用该控件为每一行显示 - 当您进行数据绑定时,让每一行为您的 TableControl 设置数据。

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

【讨论】:

    【解决方案2】:

    我认为这对于 Microsoft 提供的开箱即用控件来说(很容易)是不可能的。

    如果您有预算,不妨看看 Telerik 的控件。

    http://www.telerik.com/products/winforms/gridview.aspx

    他们有一个 gridview 控件作为他们的 .NET WinForm 控件的一部分,看起来它会做你想做的事。

    否则,是否可以更改您的体系结构并使用基于 ASP.NET Web 的方法来解决此问题?

    希望对您有所帮助。祝你好运。

    【讨论】:

      【解决方案3】:

      1)。试用 MSDN 的 Mark Ridout 的 TreeGridView 并阅读他的 article 了解如何使用它。

      还要看看这个使用 Mark Ridout 的 TreeGridView 的 CodeProject DataGridView with Hierarchical Data Binding 是否有用。

      2)。如果免费控制不起作用,请查看第 3 方(我不隶属于这些公司):

      Devexpress XtraGrid
      Telerik Gridview
      Infragistics Grid
      VIBlend DataGridView for WinForms
      Janus Grid
      xceed's Grid


      3)。我很确定将 TablePanelLayout 添加到 Grids 单元格中不是你想要的,这里是代码,所以你可以看到它对你自己有多么狡猾:

      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;
      

      【讨论】:

        【解决方案4】:

        根据我的说法,在 DataGridView 中添加包含表格的列是不可能的。 您的问题的最佳替代方案是自行创建一个动态表格并重复行以显示您的数据,并且在该行中您当然可以在其中放置一个带有表格的单元格。

        【讨论】:

        • We expect answers to be supported by facts, references, or specific expertise. 我只是表明这是可能的......
        【解决方案5】:

        您可以使用 RowDatabound 事件将表格添加到单元格控件集合。类似这个例子的东西(对不起 - 与你正在做的不同,但可能与你想要的相似,也在 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
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-16
          • 1970-01-01
          • 2020-08-13
          • 1970-01-01
          • 2016-10-22
          • 1970-01-01
          • 2017-11-15
          • 1970-01-01
          相关资源
          最近更新 更多