如何获取DataGridViewComboBoxColumn中单元格的值 [英] How to get the value of a cell in a DataGridViewComboBoxColumn
问题描述
我有一个带有DataGridViewComboBoxColumn和DataGridViewTextBoxColumn的简单datagridview。组合框的填充如下:(其中PNcombo.text是标准组合框)
I have a simple datagridview with a DataGridViewComboBoxColumn, and a DataGridViewTextBoxColumn. The Combobox is populated as follows: (where PNcombo.text is a standard combobox)
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正确填充,但是我需要获取关联的 CollectionPointID并将其放入dgv内的文本框中。
The combox in the dgv populates correctly, but I need to get the associated "CollectionPointID" and put it in the textbox within the dgv.
在这里,我不知道如何获取从组合框中选择的单元格值。
From here I don't know how to get the cell value that I select from the combobox.
推荐答案
我通常引用单元格的.Value属性,但是您需要将其强制转换为正确的类型(例如,示例中的String),因为.Value类型为 Object
:
I usually reference the .Value property of the cell, but you'll need to cast it to the correct type (i.e. String in your example) because .Value is of type Object
:
DirectCast(DataGridView1.SelectedRows(0).Cells( Description)。Value,字符串)
或
CStr(DataGridView1.SelectedRows(0).Cells("Description").Value)
这篇关于如何获取DataGridViewComboBoxColumn中单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!