[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

dataviewgrid combobox double binding

Posted on 2007-03-22
4
Medium Priority
?
635 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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

650 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