2013-08-27 47 views
0

我有一個簡單的DataGridView DataGridViewComboBoxColumn和DataGridViewTextBoxColumn。組合框填充如下:(其中PNcombo.text是一個標準的ComboBox)如何獲取DataGridViewComboBoxColumn中的單元格的值

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的文本框中。

從這裏我不知道如何獲得我從組合框中選擇的單元格值。

+0

如果你正在尋找從你的組合框中選擇任何值,這應該做'DataGridView1.SelectedRows(0).Cells(「Description」)。Value' – Alex

+0

@Alex - 這就是我一直在尋找。我結束了與DataGridView1.CurrentRow.Cells(「CollectionPntId」)。價值工作正常。 – ctmcklowe96

回答

1

我經常引用的單元格的.Value屬性,但你需要將它轉換爲正確的類型(即在你的例子字符串),因爲.value的是Object類型:

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

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

或'DataGridView1.SelectedRows(0).Cells(「Description」)。Value.ToString' – OSKM

相關問題