How to change a datagridview cell style from the d

2020-04-21 04:22发布

I have a datagridview which is populated from a dataset.

Once it is populated, if the user clicks on a row, the last column should change from textbox to combobox.

I am using vb.net 2010.

On the Datagridview1 CellClick event:

    With DataGridView1
        If .Rows.Count = 0 Then Exit Sub
        i = Datagridview1.currentrow.index

        Dim gridComboBox As New DataGridViewComboBoxCell
        gridComboBox.Items.Add("A") 'Populate the Combobox
        gridComboBox.Items.Add("B") 'Populate the Combobox
        gridComboBox.Items.Add("C") 'Populate the Combobox
        .Item(8, i) = gridComboBox
    End With

But this results in an error:

The following exception occurred in DataGridView:
System.Argument.Exception: DataGridViewComboBoxCell value is not valid.
To replace this default dialog please handle the DataError event.

If the situation is not feasible, I want the last column to be of type combobox upon populate of data from the dataset.

DataGridView1.DataSource = myDataSet

Thanks in advance.

1条回答
Anthone
2楼-- · 2020-04-21 04:40

Alright, this is my conclusion from a bit of testing. Your code works as long as the value of the cell is the same as one of the drop down alternatives. But if the cell value where you put the combobox is "D" (cmbbox only has "A", "B" and "C") you will get this error message.

So either you put in the current value as an option in your combobox, make sure that the cell only can have A,B or C as value or simply just clear the cell by setting its value to "". Then this will work. :)

查看更多
登录 后发表回答