【问题标题】:how do I host a control in a DataGridViewCell for displaying as well as editing?如何在 DataGridViewCell 中托管控件以进行显示和编辑?
【发布时间】:2010-09-18 13:34:25
【问题描述】:

我见过How to: Host Controls in Windows Forms DataGridView Cells,它解释了如何在 DataGridView 中托管用于编辑单元格的控件。但是如何托管用于显示单元格的控件?

我需要在同一个单元格中显示一个文件名和一个按钮。我们的 UI 设计师是图形设计师而不是程序员,所以我必须将代码与他所绘制的内容相匹配,无论这是否可能 - 或明智 - 与否。我们正在使用 VS2008 并用 C# 编写 .NET 3.5,如果这会有所不同的话。

更新:'net 建议创建一个自定义 DataGridViewCell 作为第一步,它承载一个面板;有人做过吗?

【问题讨论】:

  • 我不满意。我有一个单元格类,它将控件添加到 DataGridView 的控件中,并尝试将其显示在正确的位置,但它远非完美。

标签: c# .net datagridview user-interface


【解决方案1】:

根据您的“更新”,创建自定义 DataGridViewCell 是完成此操作的方式。我已经完成了,它不需要对 MSDN 提供的示例代码进行太多修改。就我而言,我需要一堆自定义编辑控件,所以我最终继承了DataGridViewTextBoxCellDataGridViewColumn。我在我的类(继承自DataGridViewTextBoxCell)中插入了一个新的自定义控件,它实现了IDataGridViewEditingControl,并且一切正常。

我想在您的情况下,您可以编写一个PanelDataGridViewCell,其中包含一个控件MyPanelControl,该控件将从Panel 继承并实现IDataGridViewEditingControl

【讨论】:

  • IDataGridViewEditingControl 方法仅适用于编辑。也用于展示呢?
  • 实际上,就在您编辑之前,它已经向您显示了内容。所以我想它也适用于展示!
  • 不,它根本不起作用。我不想显示单元格的格式化值,我想显示控件。
【解决方案2】:

与其使用datagridview,不如使用TableLayoutPanel。创建具有标签、按钮和事件的控件,并用它们填充布局面板。可以这么说,你的控制变成了细胞。如果这是您想要的布局样式,则无需花费太多时间即可使表格布局面板看起来像 datagridview。

【讨论】:

  • 不仅仅是布局,我还想要 DataGridView 的行为。
  • @Simon - 你终于明白了吗?
  • 恐怕我几年前换了工作。我不认为我们曾经解决过这个问题 - 抱歉。
  • 人们得癌症的真正原因是微软的wimforms bug,难以自定义控件。
【解决方案3】:

有两种方法可以做到这一点:

1)。将 DataGridViewCell 转换为存在的特定单元格类型。例如,将 DataGridViewTextBoxCell 转换为 DataGridViewComboBoxCell 类型。

2)。创建一个控件并将其添加到 DataGridView 的控件集合中,设置其位置和大小以适合要托管的单元格。

查看下面叶志新的示例代码,其中说明了这些技巧:

private void Form_Load(object sender, EventArgs e)
{
    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;

    /*
    * First method : Convert to an existed cell type such ComboBox cell,etc
    */

    DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();
    ComboBoxCell.Items.AddRange(new string[] { "aaa","bbb","ccc" });
    this.dataGridView1[0, 0] = ComboBoxCell;
    this.dataGridView1[0, 0].Value = "bbb";

    DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();
    this.dataGridView1[0, 1] = TextBoxCell;
    this.dataGridView1[0, 1].Value = "some text";

    DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();
    CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
    this.dataGridView1[0, 2] = CheckBoxCell;
    this.dataGridView1[0, 2].Value = true;

    /*
    * Second method : Add control to the host in the cell
    */
    DateTimePicker dtp = new DateTimePicker();
    dtp.Value = DateTime.Now.AddDays(-10);
    //add DateTimePicker into the control collection of the DataGridView
    this.dataGridView1.Controls.Add(dtp);
    //set its location and size to fit the cell
    dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Location;
    dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3,true).Size;
}

MSDN Reference : how to host different controls in the same column in DataGridView control

使用第一种方法如下:

使用第二种方法如下:

其他信息:Controls in the same DataGridView column dont render while initializing grid

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 2018-03-19
    相关资源
    最近更新 更多