Solved

dataviewgrid combobox double binding

Posted on 2007-03-22
4
627 Views
Last Modified: 2013-12-17
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
Comment
Question by:jackjohnson44
  • 2
4 Comments
 
LVL 7

Expert Comment

by:levyuk
ID: 18775472
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
 

Author Comment

by:jackjohnson44
ID: 18776927
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
 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 18784840
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
 

Author Comment

by:jackjohnson44
ID: 18785815
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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now