Vb.net DatagridviewComboBoxCell如何设置选中的值

时间:2012-05-17 12:37:04

标签: vb.net datagridviewcombobox

我有一个带有ComboBoxCell的datagridview ,,, ComboBox被绑定到数据,我想用它作为传统的ComboBox,我的意思是我想在它的值的基础上显示它的Item(来自显示成员) ,,

例如

当我这样做时

Datagridview1.CurrentRow.Cells(4).value = 7'Cell 4是DatagridviewComboBoxCell

它给了我错误

“DatagridviewComboBoxCell.value无效”,

但是我希望这个组合框应该选择并显示值为7的项目

我尝试了不同的技术,但是徒劳无功

感谢,,

1 个答案:

答案 0 :(得分:0)

嗯,我知道它已经晚了,我希望我不要叫醒你。这可能有所帮助。

    For Each question As String In questions

        answerStr = 'your query or something that can be used as a datasource, eg datatable'
        Dim dgvcc As New DataGridViewComboBoxCell
        With dgvcc
            .DataSource = answerStr
            .ValueMember = "ColumnIDFromAnswerStr"
            .DisplayMember = "AnotherColumnFromAnswerStr"
        End With    

        'this is where you can set the combobox
        'assuming answerStr is a datatable (not tested code, but i think it will work)
        dgvcc.Value = answerStr.Rows(x).Item(y).Value    
        'assuming you only have one column (combobox)   
        DataGridView1.Item(0, rowIndex) = dgvcc      
        rowIndex += 1
        dgvcc.Dispose()
    Next

修改

我也找到了这个useful link