在的DataGridViewColumn混合细胞类型 [英] Mixing cell types in a DataGridViewColumn

查看:124
本文介绍了在的DataGridViewColumn混合细胞类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能同时拥有DataGridViewComboBoxCells和DataGridViewTextBoxCells在一个单一的DataGridViewColumn?还是我绝对限制为每列有一种类型的?

Is it possible to have both DataGridViewComboBoxCells and DataGridViewTextBoxCells in a single DataGridViewColumn? Or am I absolutely restricted to having one type per column?

推荐答案

有一个奇怪的解决方案这一点。
默认情况下创建列文本框。
手柄电池点击或单元格中输入事件。
在若参数:columnIndex匹配的情况下,转换列的类型为组合框,并设置项目。
一旦从各自的列索引的单元格离开事件触发,将其转换回文本框。
不要忘记转换之前读取组合的文本,并将其设置为文本框。

There is a weird solution for this.
By default create the column as TextBox.
Handle the cell click or cell enter event.
In the event if the ColumnIndex matches, Convert the column type to ComboBox and set the items.
Once the cell leave event fires from the respective column index, convert it back to textbox.
Dont forget to read the text from Combo before converting and set it to TextBox.

我知道这是不是恰当的解决方案,但作品。
我急切地想知道如果任何人有一个更好的主意。

I know this is not apt solution but works.
I am eager to know if anyone has a better idea.

发问的编辑:

下面是code我最后写道:

Here was the code I ultimately wrote:

// Using CellClick and CellLeave in this way allows us
// to stick combo boxes in a particular row, even if the
// parent column type is different
private void dataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= FIRST_COL && e.ColumnIndex <= LAST_COL && e.RowIndex == ROW_OF_INTEREST)
    {
        object value = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        dataGrid.Columns[e.ColumnIndex].CellTemplate = new DataGridViewComboBoxCell();
        var cell = new DataGridViewComboBoxCell {Value = value};
        cell.Items.AddRange(_values);
        dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;
    }
}

private void dataGrid_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex >= FIRST_COL && e.ColumnIndex <= LAST_COL && e.RowIndex == ROW_OF_INTEREST)
    {
        object value = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
        dataGrid.Columns[e.ColumnIndex].CellTemplate = new DataGridViewTextBoxCell();
        var cell = new DataGridViewTextBoxCell {Value = value};
        dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex] = cell;
    }
}

此外,在创建列的时候,我必须以确保它是一个通用列;即不是DataGridViewTextBoxColumn:

Also, when creating the column, I had to make sure it was a generic column; i.e. not a DataGridViewTextBoxColumn:

var col = new DataGridViewColumn
              {
                  CellTemplate = new DataGridViewTextBoxCell()
              };

这样的话,我可能会在以后更改CellTemplate。

That way, I could change the CellTemplate later.

这篇关于在的DataGridViewColumn混合细胞类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆