How to switch between DataGridViewTextBoxCell and DataGridViewComboBoxCell?

拥有回忆 提交于 2019-12-06 02:21:50

问题


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

I'm thinking I just need to make the second column of type DataGridViewColumn, but don't understand the mechanics of how to change the cell type on the fly.

I'm working with VB.NET in Visual Studio 2005.

Thanks in advance!

Update: One way around it, I suppose, is to make the second column as a DataGridViewComboBoxColumn, and change the attributes of the cell so that it either behaves like a drop-down list, or as an (editable) drop-down with no elements. The latter looks enough like a text box that I could live with it, and it wouldn't involve changing the type of the cell.


回答1:


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

In this example, I set up a simple DataGridView with 2 columns. The first being a DataGridViewComboBox populated with two choices: "Text" or "Combo".

The second column is initially set to DataGridViewTextBoxColumn from the designer.

I handle the CurrentCellDirtyStateChanged event on the DataGridView. I check if the cell is dirty and only check the first column (The ComboBox). You gotta call the CommitEdit to get the new value in the combo or else you will be looking at the previous value. Based on the selection in the combo box I then overwrite the cell in the 2nd column with a new cell of that type.

You would add your own logic (populate the drop downs and handle the value). You might want to store the value and then put it back into the cell or whatever.

Here is the code I used and did a quick and dirty test on:

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();
            }
        }
    }

Here is a quick VB translation, that I tested and works.

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

End Class

You will lose any value stored in that column, so you would need to save it first.

Jon




回答2:


You can create your own cell template that hosts a user control. In the user control you add a textbox and a combobox, and add a method/property to show one and hide another.

This sample creates a radio button cell, it is not hard to change the code to host a user control.




回答3:


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


来源:https://stackoverflow.com/questions/1759720/how-to-switch-between-datagridviewtextboxcell-and-datagridviewcomboboxcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!