如何通过名称值而不是ID值将DataGriedView中的相关行记录显示到组合框 [英] How to Display related row records in DataGriedView to combobox by name value instead ID value
问题描述
我有这个代码将数据从datagriedview填充到组合框
但是你看到的这个代码给了我对组合框的ID我希望能用ID来显示组合框中的名字。
这里的每个组合框都是外键。
所以当我在记录之间移动时,点击DGV行显示这个组合框上的记录,但显示名称不是值
当我将datagriedview中的数据加载到组合框时,如何按ID值显示名称?
Sub FILL_DATA_FROM_DGV()
Dim pos 作为 整数 = 我 .DGView1.CurrentRow.Index
我 .TB1.Text = DGView1.Rows(pos).Cells( 0 )。值
我 .ComboBox1.Text = DGView1.Rows(pos).Cells( 22 )。Value.ToString
Me .ComboBox2.Text = DGView1.Rows(pos).Cells ( 20 )。Value.ToString
Me .ComboBox3.Text = DGView1.Rows(pos )。细胞( 29 )。Value.ToString
Me .ComboBox4.Text = DGView1。行(pos)。细胞( 14 )。Value.ToString
Me .ComboBox5.Text = DGView1.Rows(pos).Cells( 13 )。Value.ToString
Me .ComboBox6.Text = DGView1.Rows(pos).Cells( 9 )。值。 ToString
Me .ComboBox7.Text = DGView1.Rows(pos).Cells( 10 ) .Value.ToString
Me .ComboBox8.Text = DGView1.Rows(pos).Cells( 26 )。Value.ToString
Me .ComboBox9.Text = DGView1.Rows(pos).Cells( 15 )。Value.ToString
Me .ComboBox10.Text = DGView1.Rows(pos).Cells( 18 )。Value.ToString
Me .ComboBox11.Text = DGView1.Rows(pos).Cells( 23 )。Value.ToString
结束 sub
此图片解释更多:
http://im55.gulfup.com/iuDiKt。 png [ ^ ]
首先,您必须将数据绑定到组合框才能获得相关值。
使用 我 .ComboBoxColor
.DataSource = DataTable1
.DisplayMember = ColorName
.ValueMember = ColorId
结束 使用
' 然后
我 .ComboBoxColor.SelectedValue = 1
' 组合框现在显示相关颜色,例如红色
有关详细信息,请参阅:
ComboBox.DataSource [ ^ ]
ComboBox.DisplayMember [ ^ ]
ComboBox.ValueMember [ ^ ]
ComboBox。 SelectedValue [ ^ ]
感谢每一个人重播
我解决了这个问题
我希望帮助任何人寻找同一问题的解决方案
Sub FILL_DATA_FROM_DGV()
Dim pos As 整数 = 我 .DGView1.CurrentRow.Index
connection.Open()
cmd = 新 SqlCom mand( 来自TABLE_NAME的SELECT FIELD_NAME WHERE ID = @ ITEMID,connection)
cmd。 Parameters.AddWithValue( @ ITEMID,DGView1.Rows(pos).Cells( 22 )。Value.ToString)
Dim dr 作为 SqlDataReader = cmd.ExecuteReader
如果(dr.Read())那么
ComboBox1.Text = dr( 0 )。ToString
结束 < span class =code-keyword>如果
connection.Close()
结束 Sub
私有 Sub DGView1_Click(发件人正如 对象,e As EventArgs)句柄 DGView1.Click
尝试
FILL_DATA_FROM_DGV()
Catch ex As 例外
结束 尝试
结束 Sub < /跨度>
I have this code to Fill data from datagriedview to combobox
but this code as you see give me the id's to combobox I wanna a way to show names in combobox using id's.
every combobox here is foreign key .
so I want when I move between records by click on DGV row display this record on comboboxes but with display names not values
How to Display the name by ID value when I load the data from datagriedview to combobox?
Sub FILL_DATA_FROM_DGV()
Dim pos As Integer = Me.DGView1.CurrentRow.Index
Me.TB1.Text = DGView1.Rows(pos).Cells(0).Value
Me.ComboBox1.Text = DGView1.Rows(pos).Cells(22).Value.ToString
Me.ComboBox2.Text = DGView1.Rows(pos).Cells(20).Value.ToString
Me.ComboBox3.Text = DGView1.Rows(pos).Cells(29).Value.ToString
Me.ComboBox4.Text = DGView1.Rows(pos).Cells(14).Value.ToString
Me.ComboBox5.Text = DGView1.Rows(pos).Cells(13).Value.ToString
Me.ComboBox6.Text = DGView1.Rows(pos).Cells(9).Value.ToString
Me.ComboBox7.Text = DGView1.Rows(pos).Cells(10).Value.ToString
Me.ComboBox8.Text = DGView1.Rows(pos).Cells(26).Value.ToString
Me.ComboBox9.Text = DGView1.Rows(pos).Cells(15).Value.ToString
Me.ComboBox10.Text = DGView1.Rows(pos).Cells(18).Value.ToString
Me.ComboBox11.Text = DGView1.Rows(pos).Cells(23).Value.ToString
End sub
this image to explain more :
http://im55.gulfup.com/iuDiKt.png[^]
First of all, you have to bind data to comboboxes to be able to get related values.
With Me.ComboBoxColor .DataSource = DataTable1 .DisplayMember = "ColorName" .ValueMember = "ColorId" End With 'then Me.ComboBoxColor.SelectedValue = 1 'combobox displays now related color, for example red
For further information, please see:
ComboBox.DataSource[^]
ComboBox.DisplayMember[^]
ComboBox.ValueMember[^]
ComboBox.SelectedValue[^]
thanks every one for replay
I solved that
I hope that help any one search for solution for the same problem
Sub FILL_DATA_FROM_DGV() Dim pos As Integer = Me.DGView1.CurrentRow.Index connection.Open() cmd = New SqlCommand("SELECT FIELD_NAME from TABLE_NAME WHERE ID=@ITEMID", connection) cmd.Parameters.AddWithValue("@ITEMID", DGView1.Rows(pos).Cells(22).Value.ToString) Dim dr As SqlDataReader = cmd.ExecuteReader If (dr.Read()) Then ComboBox1.Text = dr(0).ToString End If connection.Close() End Sub Private Sub DGView1_Click(sender As Object, e As EventArgs) Handles DGView1.Click Try FILL_DATA_FROM_DGV() Catch ex As Exception End Try End Sub
这篇关于如何通过名称值而不是ID值将DataGriedView中的相关行记录显示到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!