Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Adding databound ComboBox into cells of a DataGridView

Adding databound ComboBox into cells of a DataGridView

Scheduled Pinned Locked Moved C#
databasecsscomhelpannouncement
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    I have a DataGridView on a form which is populated through databinding from a table in my database. I've followed, more or less, the process outlined on this MSDN page[^] I've implemented the databinding both ways so that, by clicking on an "Update" button, the changes that have been made on the DataGridView is stored back into the DB. All works well, but... Imagine the table used for the databinding being something like this:

    USERS

    • UserID (INT)
    • UserName (VARCHAR)
    • UserType (INT)

    And here's the problem, that UserType field references the identifier in a separate table like this one:

    USERTYPES

    • UserType (INT)
    • UserTypeDescription (VARCHAR)

    Currently, my databinding is done with a simple query like this:

    SELECT * FROM USERS;

    The problem is that the value displayed in the third column is an integer which means nothing to the end user. If I wasn't interested in two way databinding but only wanted to display the contents of the table in the DataGridView I could easily have overcome that problem by using the following query:

    SELECT UserID, UserName, UserTypeDescription FROM USERS INNER JOIN USERTYPES ON USERS.UserType = USERTYPES.UserType;

    Unfortunately I need for the end user to be able to change that value and I'd like to put a ComboBox in all the cells of that column which is populated from the USERTYPES table. Would really appreciate some advice. ***EDIT*** Maybe I should add that I have some experience with adding a ComboBox (rather a DataGridViewComboBoxCell) in a DataGridView but that has always been to DataGridViews that have not been databound to a table. Here's how I would've done that:

    private void myDataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
    for (int row = 0; row < (sender as DataGridView).Rows.Count; row++)
    {
    // Create a new DataGridViewComboBoxCell that will replace the existing UserType cell in the relevant column
    DataGridViewComboBoxCell newCell = new DataGridViewComboBoxCell();

        // Populate the DataSource of the combo box
        using (SqlCommand sqlCommand = new SqlCommand("SELECT UserType, UserDescription FROM UserTypes", mySQLConnection))
        {
            using (SqlDataReader sqlData
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups