Solved

dataviewgrid combobox double binding

Posted on 2007-03-22
4
628 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 53
Data is not showing from images 15 37
C# HTTP GET method sample code 3 41
Visual Studio 2015 Source Code Control 10 34
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
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.

920 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

13 Experts available now in Live!

Get 1:1 Help Now