Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Binding data from a database to a DGV combo box.
 

Binding data from a database to a DGV combo box.

Hi All,

I am able to select to display data in a datagridview. My code:
Public Sub ShowData()
If conn.State = ConnectionState.Closed Then conn.Open()

cmd = New SqlCommand("SELECT * FROM ClientPlan", conn)
myDA = New SqlDataAdapter(cmd)
myDataSet = New DataSet()
myDA.Fill(myDataSet, "ClientPlan")
DGV.DataSource = myDataSet.Tables("clientplan").DefaultView
End Sub
This code works perfectly fine for displaying my data in the DGV.

However , i would now like to have a combo box column in my DGV.
I want to populate my DGV combo box with data from a table in the database.
(I used cb1.item.add for normal/non DGV combo boxes, but i don't know how to implement it for DGV combo boxes.)
For example -
Column Name : Plans
Items in the Combobox : Plan A, Plan B, Plan C (All in the database)
Can anyone tell me how i can get this to work?

I am coding in Visual Studio 2005(VB.NET exclusive) and working with MS SQL database.

Thanks,
gnxc
gnxc  Thursday, August 06, 2009 8:58 AM
Have a look to this sample code may be this can help you:

 With DataGridView1  
 
  
 
 ' Set DataGridView Combo Column for CarID field   
 
 Dim ColumnCar As New DataGridViewComboColumn  
 
 ' DataGridView Combo ValueMember field has name "CarID"  
 
 ' DataGridView Combo DisplayMember field has name "Car"  
 
 With ColumnCar  
 
     .DataPropertyName = "CarID"  
 
     .HeaderText = "Car Name"  
 
     .Width = 80  
 
     ' Bind ColumnCar to Cars table  
 
     .box.DataSource = ds.Tables("Cars")  
 
     .box.ValueMember = "CarID"  
 
     .box.DisplayMember = "Car"  
 
 End With  
 
 .Columns.Add(ColumnCar)  
  
 End With  

Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
  • Marked As Answer bygnxc Friday, August 07, 2009 2:28 AM
  •  
_SuDhiR_  Thursday, August 06, 2009 9:08 AM
Have a look to this sample code may be this can help you:

 With DataGridView1  
 
  
 
 ' Set DataGridView Combo Column for CarID field   
 
 Dim ColumnCar As New DataGridViewComboColumn  
 
 ' DataGridView Combo ValueMember field has name "CarID"  
 
 ' DataGridView Combo DisplayMember field has name "Car"  
 
 With ColumnCar  
 
     .DataPropertyName = "CarID"  
 
     .HeaderText = "Car Name"  
 
     .Width = 80  
 
     ' Bind ColumnCar to Cars table  
 
     .box.DataSource = ds.Tables("Cars")  
 
     .box.ValueMember = "CarID"  
 
     .box.DisplayMember = "Car"  
 
 End With  
 
 .Columns.Add(ColumnCar)  
  
 End With  

Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
  • Marked As Answer bygnxc Friday, August 07, 2009 2:28 AM
  •  
_SuDhiR_  Thursday, August 06, 2009 9:08 AM
It worked! Thank you so much!
Now to find out how to fill the DGV AND the combo box at the same time =P Thanks again!
gnxc  Friday, August 07, 2009 2:28 AM
Hm,
I actually want the items to be from the Plans table, and the selected item to be from the Policy table, how does it work?
  With ColumnCode
                    .DataPropertyName = "PlanCode"
                    .HeaderText = "Plan Code"
                    .Width = 80
                    ' Bind ColumnCar to Cars table  
                    .DataSource = myDataSet2.Tables("plans")
                    .ValueMember = "planCode"
                    .DisplayMember = "planCode"
That's my code atm, i don't know how to set the selected to a different table or how to get it to show though, can anyone help please?
Thanks!
gnxc  Wednesday, August 12, 2009 7:17 AM

You can use google to search for other answers

Custom Search

More Threads

• BUG: Inheritance using custom controls
• Can't place the ToolStrip underneath the MenuStrip
• How to Avoid the Scroll Bar Flickering in Panels
• Unwanted changes to designer file
• Container Control
• Customizing Binding Navigator
• Potential Bug: Form Scroll Bar + Build
• Incorrect positions of controls on form with Menu (VS2003)
• Hi ! Please Help on Combo Box as soon as possible.
• DrawGrid and SnapToGrid