You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.net中如何通过行索引获取DataGridViewComboBox的SelectedValue

在VB.NET中获取DataGridViewComboBox的SelectedValue(按行索引)

需求:在Button Click事件中,通过指定行索引获取DataGridView中ComboBox列(已绑定DisplayMember和ValueMember)的选中项对应ValueMember值,目前仅能在EditingControlShowing事件中实时获取,需实现按钮点击场景下的取值逻辑。

解决方案

DataGridViewComboBox列的选中值会直接同步到对应单元格的Value属性中,该属性存储的就是你设置的ValueMember(即示例中的ID),无需依赖编辑控件即可直接读取。

按钮点击事件代码示例

Private Sub btnGetSelectedValue_Click(sender As Object, e As EventArgs) Handles btnGetSelectedValue.Click
    ' 替换为你需要获取的目标行索引
    Dim targetRowIndex As Integer = 0
    
    ' 获取目标行的Product_Name列单元格
    Dim comboCell As DataGridViewComboBoxCell = CType(dgvSampleList.Rows(targetRowIndex).Cells("Product_Name"), DataGridViewComboBoxCell)
    
    ' 读取选中的ValueMember值
    If comboCell.Value IsNot Nothing Then
        MsgBox("ProgramID: " & comboCell.Value.ToString())
    Else
        MsgBox("该单元格未选择任何项")
    End If
End Sub

说明

  1. 若要获取当前选中行的ComboBox值,可将targetRowIndex替换为dgvSampleList.CurrentRow.Index
  2. 你原有EditingControlShowing事件代码用于实时监听编辑时的选中变化,按钮点击场景直接读取单元格Value属性更高效——用户选择后该值已同步到单元格中

内容的提问来源于stack exchange,提问作者rdet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 08:22:11