?
Solved

dataviewgrid combobox double binding

Posted on 2007-03-22
4
Medium Priority
?
633 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

777 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