【问题标题】:How to switch between DataGridViewTextBoxCell and DataGridViewComboBoxCell?如何在 DataGridViewTextBoxCell 和 DataGridViewComboBoxCell 之间切换?
【发布时间】:2009-11-18 22:52:48
【问题描述】:

我想要一个有两列的 DataGridView。第一列将始终为 DataGridViewComboBoxColumn 类型。根据该列中的选择,我希望能够将第二列中的相应单元格更改为 DataGridViewComboBoxCell 或 DataGridViewTextBoxCell。

我想我只需要制作 DataGridViewColumn 类型的第二列,但不了解如何动态更改单元格类型的机制。

我在 Visual Studio 2005 中使用 VB.NET。

提前致谢!

更新: 我想,一种解决方法是将第二列设为 DataGridViewComboBoxColumn,并更改单元格的属性,使其行为类似于下拉列表,或者作为没有元素的(可编辑的)下拉菜单。后者看起来很像我可以使用的文本框,而且它不涉及更改单元格的类型。

【问题讨论】:

    标签: vb.net visual-studio-2005 datagridview datagridviewcomboboxcell datagridviewtextboxcell


    【解决方案1】:

    我没有 VB.Net 版本,但希望这个快速的 C# sn-p 可以帮助您或为您指明正确的方向。

    在这个例子中,我设置了一个简单的 DataGridView,它有 2 列。第一个是填充有两个选项的 DataGridViewComboBox:“文本”或“组合”。

    第二列最初由设计器设置为 DataGridViewTextBoxColumn。

    我处理 DataGridView 上的 CurrentCellDirtyStateChanged 事件。我检查单元格是否脏,只检查第一列(组合框)。您必须调用 CommitEdit 以获取组合中的新值,否则您将查看以前的值。然后,根据组合框中的选择,我用该类型的新单元格覆盖第二列中的单元格。

    您将添加自己的逻辑(填充下拉菜单并处理值)。您可能想要存储该值,然后将其放回单元格或其他任何内容中。

    这是我使用的代码,并对其进行了快速而肮脏的测试:

    private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty == false)
            {
                return;
            }
    
            dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    
            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {               
                if (((string)dataGridView1.CurrentCell.Value) == "Text")
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] = new DataGridViewTextBoxCell();
                }
                else if (((string)dataGridView1.CurrentCell.Value) == "Combo")
                {
                    dataGridView1.Rows[dataGridView1.CurrentCell.RowIndex].Cells[1] = new DataGridViewComboBoxCell();
                }
            }
        }
    

    这是一个快速的 VB 翻译,我测试过并且可以使用。

    Public Class Form1
    
    Private Sub DataGridView1_CurrentCellDirtyStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
    
        If DataGridView1.IsCurrentCellDirty = False Then
            Return
        End If
    
        DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    
        If DataGridView1.CurrentCell.ColumnIndex = 0 Then
    
            If CStr(DataGridView1.CurrentCell.Value) = "Text" Then
                DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1) = New DataGridViewTextBoxCell
    
            ElseIf CStr(DataGridView1.CurrentCell.Value) = "Combo" Then
                DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).Cells(1) = New DataGridViewComboBoxCell
            End If
    
        End If
    
    
    End Sub
    

    结束类

    您将丢失该列中存储的任何值,因此您需要先保存它。

    乔恩

    【讨论】:

    • 谢谢。这很有帮助。我不是真正的 VB 或 .NET 专家,所以原则上我知道你在做什么,但是在 VB 中分配新的 DataGridViewTextBoxCell 或新的 DataGridViewComboBoxCell 的机制是我仍然需要弄清楚的事情。无论如何,我很感谢您花时间回答问题,我会在今天下午尝试。
    【解决方案2】:

    您可以创建自己的单元格模板来承载用户控件。在用户控件中添加一个文本框和一个组合框,并添加一个方法/属性来显示和隐藏另一个。

    This sample 创建一个单选按钮单元格,更改代码以托管用户控件并不难。

    【讨论】:

      【解决方案3】:
      dgvCell = new DataGridViewTextBoxCell();         // code to remove checkbox
              dgvCell.Value = string.Empty;
              dgv_modi_del_trans.Rows[1].Cells[0] = dgvCell;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-05
        • 1970-01-01
        • 2020-02-07
        • 2011-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-20
        相关资源
        最近更新 更多