2012-10-17 37 views
1

我有一個從數據集填充的datagridview。如何將datagridview單元格樣式從默認文本框更改爲vb.net中的組合框?

填充後,如果用戶單擊一行,最後一列應該從文本框更改爲組合框。

我使用vb.net 2010

在Datagridview1 CellClick事件:

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 

但是,這將導致一個錯誤:如果情況是不可行的

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

,我希望在填充數據集中的數據時最後一列是組合框的類型。

DataGridView1.DataSource = myDataSet 

在此先感謝。

+0

我的價值是什麼? – WozzeC

+0

對不起,我沒有看到。我= Datagridview1.CurrentRow.Index,感謝您指出。 –

回答

2

好的,這是我從一些測試中得出的結論。 只要單元格的值與其中一個下拉選項相同,您的代碼就會工作。但是,如果您將組合框的單元格值設置爲「D」(cmbbox只有「A」,「B」和「C」),您將收到此錯誤消息。

因此,無論您將當前值作爲組合框中的選項放入,請確保該單元格只能具有A,B或C值,或者只需將其值設置爲「」來清除單元格即可。然後這將工作。 :)

+0

你是對的,組合框的值應該在其列表中。我正在計算這一個,所以我只是用值A,B,C填充組合框。再次感謝。 –

相關問題