Click here to Skip to main content
15,884,893 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

VB
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[^]
Posted
Updated 10-May-15 22:35pm
v5
Comments
Michael_Davies 10-May-15 4:17am    
Not sure what you are asking, the image seems to show what you expect. Can you clarify?
Muradooz 10-May-15 6:06am    
yeah the image show what I except but I want to show names in combobox not the id value but I don't know How ?
for example I wanna show in color filed "RED" instead the ID "5".
Kornfeld Eliyahu Peter 10-May-15 7:42am    
And your combo box contains an item that has the value of 5 and the text of RED?
Muradooz 11-May-15 2:59am    
yes it is. ,every combobox is Foreign key here.
Maciej Los 10-May-15 8:57am    
Does comboboxes are binded with datasource? How? Show your code...

First of all, you have to bind data to comboboxes to be able to get related values.

VB
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[^]
 
Share this answer
 
thanks every one for replay
I solved that
I hope that help any one search for solution for the same problem

VB
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
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900