如何在DataGridViewComboBoxColumn中获取单元格的值

时间:2013-08-27 15:47:19

标签: vb.net visual-studio-2010 datagridview

我有一个带DataGridViewComboBoxColumn的简单数据网格视图和一个DataGridViewTextBoxColumn。 Combobox填充如下:(其中PNcombo.text是标准组合框)

  Dim objConn As New SqlConnection(DatabaseConnection.FISSQLConnectionString)
    Dim str1 As New StringBuilder
    str1.Append("SELECT cp.Description, cp.CollectionPointId ")
    str1.Append("FROM CollectionPoints cp INNER JOIN ")
    str1.Append("   Products p ON cp.ProductIdValue = p.ProductId ")
    str1.Append("WHERE p.PartNumber = N'" & Trim(PNcombo.Text) & "'")
    Dim objCommand As SqlCommand = New SqlCommand(str1.ToString, objConn)
    objConn.Open()
    Dim dt As New DataTable
    dt.Load(objCommand.ExecuteReader)

   Dim colDesription As New DataGridViewComboBoxColumn
    With colDesription
        .DataSource = dt
        .DisplayMember = "Description"
        .ValueMember = "Description"
    End With
    Me.DataGridView1.Columns.Add(colDesription)

  Dim colCollectionPoint As New DataGridViewTextBoxColumn
    With colCollectionPoint
        .Name = "CollectioPntId"
        .DataPropertyName = "CollectionPointId"
    End With
    DataGridView1.Columns.Add(colCollectionPoint)

dgv中的combox正确填充,但我需要获取关联的“CollectionPointID”并将其放在dgv中的文本框中。

从这里我不知道如何获取我从组合框中选择的单元格值。

1 个答案:

答案 0 :(得分:1)

我通常引用单元格的.Value属性,但是您需要将其强制转换为正确的类型(即示例中的String),因为.Value的类型为Object

DirectCast(DataGridView1.SelectedRows(0).Cells("Description").Value, String)

CStr(DataGridView1.SelectedRows(0).Cells("Description").Value)