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. DataGridViewComboBoxColumn and databinding?

DataGridViewComboBoxColumn and databinding?

Scheduled Pinned Locked Moved C#
csharpdatabasevisual-studiohelpquestion
3 Posts 3 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.
  • K Offline
    K Offline
    kbalias
    wrote on last edited by
    #1

    Hi I am using Visual Studio 2005 and MS Access 2003 to create a Windows application in C#. I have a DataGridView and I programmatically add a DataGridViewComboBoxColumn to the DataGridView. I populate the ComboBoxes in the DataGridViewComboBoxColumn from a lookup table in the database and bind the DataGridView to a DataTable loaded from another table in the database. However the correct item in the ComboBox does not get selected. In fact nothing is selected in the ComboBox. Looking at my code it feels to me that I am missing something in my code, but I do not know what. The doctorTable has a Primary key: Doctor_ID The patientTable has a Foreign key: Doctor_ID Using the following code snippets:

    private void DataController()
    {
    try
    {
    //this methods loads the data from the database into two DataTables: patientTable, doctorTable
    this.LoadData_Controller();

        this.FormatDataGridView();
    
        dgvPatients.DataSource = patientTable;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    

    }

    /// <summary>
    /// adds a DataGridViewComboBoxColumn to the DataGridView and set the DataSource
    /// </summary>
    private void FormatDataGridView()
    {
    try
    {
    #region Format Doctor column

        //create combobox column for doctors
        DataGridViewComboBoxColumn comboboxColumnDoctors = new DataGridViewComboBoxColumn();
        comboboxColumnDoctors.DataPropertyName = "Doctor";
        comboboxColumnDoctors.HeaderText = "Doctor";
    
        //add data to new comboboxcolumn
        comboboxColumnDoctors.DataSource = doctorTable;
        comboboxColumnDoctors.ValueMember = "Doctor\_ID";
        comboboxColumnDoctors.DisplayMember = "DrName";
    
        //insert column into datagridview
        dgvPatients.Columns.Insert(3, comboboxColumnDoctors);
    
        #endregion
    }
    catch (Exception ex)
    {
        throw ex;
    }
    

    }

    Any help will be greatly appreciated. Thanks. Kobus

    C U 2 Replies Last reply
    0
    • K kbalias

      Hi I am using Visual Studio 2005 and MS Access 2003 to create a Windows application in C#. I have a DataGridView and I programmatically add a DataGridViewComboBoxColumn to the DataGridView. I populate the ComboBoxes in the DataGridViewComboBoxColumn from a lookup table in the database and bind the DataGridView to a DataTable loaded from another table in the database. However the correct item in the ComboBox does not get selected. In fact nothing is selected in the ComboBox. Looking at my code it feels to me that I am missing something in my code, but I do not know what. The doctorTable has a Primary key: Doctor_ID The patientTable has a Foreign key: Doctor_ID Using the following code snippets:

      private void DataController()
      {
      try
      {
      //this methods loads the data from the database into two DataTables: patientTable, doctorTable
      this.LoadData_Controller();

          this.FormatDataGridView();
      
          dgvPatients.DataSource = patientTable;
      }
      catch (Exception ex)
      {
          throw ex;
      }
      

      }

      /// <summary>
      /// adds a DataGridViewComboBoxColumn to the DataGridView and set the DataSource
      /// </summary>
      private void FormatDataGridView()
      {
      try
      {
      #region Format Doctor column

          //create combobox column for doctors
          DataGridViewComboBoxColumn comboboxColumnDoctors = new DataGridViewComboBoxColumn();
          comboboxColumnDoctors.DataPropertyName = "Doctor";
          comboboxColumnDoctors.HeaderText = "Doctor";
      
          //add data to new comboboxcolumn
          comboboxColumnDoctors.DataSource = doctorTable;
          comboboxColumnDoctors.ValueMember = "Doctor\_ID";
          comboboxColumnDoctors.DisplayMember = "DrName";
      
          //insert column into datagridview
          dgvPatients.Columns.Insert(3, comboboxColumnDoctors);
      
          #endregion
      }
      catch (Exception ex)
      {
          throw ex;
      }
      

      }

      Any help will be greatly appreciated. Thanks. Kobus

      C Offline
      C Offline
      CodingYoshi
      wrote on last edited by
      #2

      Check DataBindings property. I know combobox has this property and hopefully DataGridViewComboBox has it as well. Here you can make each control be ware of the other. If property is not there then simply write some code. For example, when the comboBox is selected, select the appropriate patients which have the foreign key.

      1 Reply Last reply
      0
      • K kbalias

        Hi I am using Visual Studio 2005 and MS Access 2003 to create a Windows application in C#. I have a DataGridView and I programmatically add a DataGridViewComboBoxColumn to the DataGridView. I populate the ComboBoxes in the DataGridViewComboBoxColumn from a lookup table in the database and bind the DataGridView to a DataTable loaded from another table in the database. However the correct item in the ComboBox does not get selected. In fact nothing is selected in the ComboBox. Looking at my code it feels to me that I am missing something in my code, but I do not know what. The doctorTable has a Primary key: Doctor_ID The patientTable has a Foreign key: Doctor_ID Using the following code snippets:

        private void DataController()
        {
        try
        {
        //this methods loads the data from the database into two DataTables: patientTable, doctorTable
        this.LoadData_Controller();

            this.FormatDataGridView();
        
            dgvPatients.DataSource = patientTable;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        

        }

        /// <summary>
        /// adds a DataGridViewComboBoxColumn to the DataGridView and set the DataSource
        /// </summary>
        private void FormatDataGridView()
        {
        try
        {
        #region Format Doctor column

            //create combobox column for doctors
            DataGridViewComboBoxColumn comboboxColumnDoctors = new DataGridViewComboBoxColumn();
            comboboxColumnDoctors.DataPropertyName = "Doctor";
            comboboxColumnDoctors.HeaderText = "Doctor";
        
            //add data to new comboboxcolumn
            comboboxColumnDoctors.DataSource = doctorTable;
            comboboxColumnDoctors.ValueMember = "Doctor\_ID";
            comboboxColumnDoctors.DisplayMember = "DrName";
        
            //insert column into datagridview
            dgvPatients.Columns.Insert(3, comboboxColumnDoctors);
        
            #endregion
        }
        catch (Exception ex)
        {
            throw ex;
        }
        

        }

        Any help will be greatly appreciated. Thanks. Kobus

        U Offline
        U Offline
        u2envy12
        wrote on last edited by
        #3

        you must remove dgvPatients.Columns.Remove("Doctor_ID"); from the DataGridView

        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