• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

dataviewgrid combobox double binding

can someone please show me code, not a link, that will bind the rows of a grid to a dataset with one colum being a combobox that is bound to another table in the same dataset?

so I want a datagrid with customers, and a combobox in the grid to show a list of states.  The state will be from the states table and the customers will be in the customers table in the same dataset.  The state will be a property of the customer, but the pulldown will be made from the states table.
0
jackjohnson44
Asked:
jackjohnson44
  • 2
1 Solution
 
levyukCommented:
You should be able to set the datasource of the combobox to be the different table in your dataset. Don't have code sorry but it should give you an idea
0
 
jackjohnson44Author Commented:
thanks, I am trying to do that, but it doesn't seem to update the dataset when I edit a row on the datagrid.
I was hoping someone would have some code.  It seems pretty common.
0
 
SanclerCommented:
There's two ways to do this.

If you are using the wizards (dragging and dropping a datasource onto your form to create the datagridview) you can edit the column concerned.  It will initially appear as a textbox column.  If you right click on the grid you can then select Edit Columns.  Select the column you want to alter and change the column type to DataGridViewComboBoxColumn.  Then change the DataSource to the other table, the DisplayMember to the field you want displayng from that and the ValueMember to the field that provides the link between the tables: that is, the PK in the new table which will be the FK in the other one.

Or, if you want to do it in code, here's a demo I've just put together using some data I already had.  My data has a Customer table, with a CustCountry field which is a Foreign Key to a Country table.  The PK in that is CountryID and its CountryName field contains the full name of the country.  I've commented it so you should be able to translate it easily for your data.

Public Class Form1

    Friend WithEvents DGVCountryCombo As System.Windows.Forms.DataGridViewComboBoxColumn

    Private Sub TblCustomerBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TblCustomerBindingNavigatorSaveItem.Click

        Me.Validate()
        Me.TblCustomerBindingSource.EndEdit()
        Me.TblCustomerTableAdapter.Update(Me.TestDataSet.tblCustomer)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Load the two tables
        Me.TblCountryTableAdapter.Fill(Me.TestDataSet.tblCountry)
        Me.TblCustomerTableAdapter.Fill(Me.TestDataSet.tblCustomer)

        'make a new combo column
        Me.DGVCountryCombo = New System.Windows.Forms.DataGridViewComboBoxColumn
        'datapropertyname is from the Customer table
        Me.DGVCountryCombo.DataPropertyName = "CustCountry"
        'datasource is the Country table
        Me.DGVCountryCombo.DataSource = Me.TblCountryBindingSource
        'displaymember is the text from that
        Me.DGVCountryCombo.DisplayMember = "CountryName"
        'valuemember is the PK from that = FK in Customer table
        Me.DGVCountryCombo.ValueMember = "CountryID"
        Me.DGVCountryCombo.HeaderText = "Country"

        'take the old, textbox, column out ...
        Me.TblCustomerDataGridView.Columns.RemoveAt(2)
        '... and put the new, combo, one in in the same position
        Me.TblCustomerDataGridView.Columns.Insert(2, Me.DGVCountryCombo)

    End Sub
End Class

VB.NET, rather than C#

Roger
0
 
jackjohnson44Author Commented:
sancler, thanks a lot, that helped I will award you points for this post.

You seem to know what you are doing with .net datasets can you please take a look at
http://www.experts-exchange.com/Microsoft/Development/.NET/Visual_CSharp/Q_22468349.html?cid=239#a18785526
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now