How to switch between DataGridViewTextBoxCell and DataGridViewComboBoxCell?

I want to have a DataGridView that has two columns. The first column will always be of type DataGridViewComboBoxColumn. Based on the selection in this column, I would like to be able to change the corresponding cell in the second column to DataGridViewComboBoxCell or DataGridViewTextBoxCell.

I think I just need to make a second column of type DataGridViewColumn, but I don’t understand the mechanics of changing a cell type on the fly.

I am working with VB.NET in Visual Studio 2005.

Thanks in advance!

Update: . In my opinion, one way is to make the second column as a DataGridViewComboBoxColumn and change the attributes of the cell so that it behaves either as a drop-down list or as a (editable) drop-down menu without items. The latter looks quite like a text box in which I could live with it, and this is not connected with changing the type of cell.

+5
source share
3 answers

I don't have a version of VB.Net, but hopefully this quick C # snippet will help you or guide you in the right direction.

DataGridView . - DataGridViewComboBox, : "" "".

DataGridViewTextBoxColumn .

CurrentCellDirtyStateChanged DataGridView. , (ComboBox). 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

, , .

+3

, . /, .

, .

+2
dgvCell = new DataGridViewTextBoxCell();         // code to remove checkbox
        dgvCell.Value = string.Empty;
        dgv_modi_del_trans.Rows[1].Cells[0] = dgvCell;
0

All Articles