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. DataGridView with ComboBox. Need help

DataGridView with ComboBox. Need help

Scheduled Pinned Locked Moved C#
databasehelp
2 Posts 2 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.
  • A Offline
    A Offline
    aerinai
    wrote on last edited by
    #1

    I have been wracking my brain for hours on this problem. I have a DataGridView object that has a DataGridViewComboBoxColumn inside of it. The combo box displays fine and is populated from an Access database. The column has the Primary key mapped to the ValueMember variable and the column 'Type' is mapped to DisplayMember. What I need is for each of these combo boxes to have their selected index (I think it is the Value property under DataGridViewComboBoxCell, but I'm pulling at straws at this point) be populated from another table. This took me about 6 seconds in Access. My problem is I cannot even find a way to access the Value/SelectedIndex of the Combobox once I place it in the DataGridView. If anyone could help me out I'd greatly appreciate it. Here is the code:

        //Connection string to DB.
        OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\"" + "NGDemoSetup.mdb" + "\\"");
    
        //Dataset for majority of data
        DataSet ds = new DataSet();
    
        //Dataset for the combobox
        DataSet cboDs = new DataSet();
    
        //Create a DGVComboBoxColumn to hold the Types we have in the ADDRESSTYPES Table of the DB.
        DataGridViewComboBoxColumn cboCol = new DataGridViewComboBoxColumn();
    
        //Open the DB.
        dbCon.Open();
    
        //Grab all the data we need from the Location Info table 
        OleDbDataAdapter da = new OleDbDataAdapter("SELECT AddrType,isRange,Value1,Value2 FROM LOCATIONINFO", dbCon);
    
        //Grab all the columns from ADDRESSTYPES (ID and Types).
        OleDbDataAdapter cboDa = new OleDbDataAdapter("SELECT \* FROM ADDRESSTYPES", dbCon);
    
        da.Fill(ds, "LocationInfo");
        cboDa.Fill(cboDs, "AddressTypes");
    
        cboCol.Width = 100;
        cboCol.DataSource = cboDs.Tables\[0\];
        
        cboCol.ValueMember = "ID";
        cboCol.DisplayMember = "Type";
        DataTable dt = ds.Tables\["LocationInfo"\];
       
        grdAddressInfo.DataSource = dt;
    
        /\*
         \* Somehow make the ComboBox Column display the correct Dropdown object
         \* Based upon the AddrType field.
         \*/
    
        //Column formatting
        grdAddressInfo.Columns.Insert(0, cboCol);
        grdAddressInfo.Columns\[0\].HeaderText = "Address Type";
        grdAddressInfo.Columns\[1\].HeaderText = "Ranged Value";
        grdAddressInfo.Columns\[2\].HeaderText = "From";
        grdAddressInfo.Columns\[3\].HeaderText = "To";
    
        dbCon.Close();
    
    U 1 Reply Last reply
    0
    • A aerinai

      I have been wracking my brain for hours on this problem. I have a DataGridView object that has a DataGridViewComboBoxColumn inside of it. The combo box displays fine and is populated from an Access database. The column has the Primary key mapped to the ValueMember variable and the column 'Type' is mapped to DisplayMember. What I need is for each of these combo boxes to have their selected index (I think it is the Value property under DataGridViewComboBoxCell, but I'm pulling at straws at this point) be populated from another table. This took me about 6 seconds in Access. My problem is I cannot even find a way to access the Value/SelectedIndex of the Combobox once I place it in the DataGridView. If anyone could help me out I'd greatly appreciate it. Here is the code:

          //Connection string to DB.
          OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\"" + "NGDemoSetup.mdb" + "\\"");
      
          //Dataset for majority of data
          DataSet ds = new DataSet();
      
          //Dataset for the combobox
          DataSet cboDs = new DataSet();
      
          //Create a DGVComboBoxColumn to hold the Types we have in the ADDRESSTYPES Table of the DB.
          DataGridViewComboBoxColumn cboCol = new DataGridViewComboBoxColumn();
      
          //Open the DB.
          dbCon.Open();
      
          //Grab all the data we need from the Location Info table 
          OleDbDataAdapter da = new OleDbDataAdapter("SELECT AddrType,isRange,Value1,Value2 FROM LOCATIONINFO", dbCon);
      
          //Grab all the columns from ADDRESSTYPES (ID and Types).
          OleDbDataAdapter cboDa = new OleDbDataAdapter("SELECT \* FROM ADDRESSTYPES", dbCon);
      
          da.Fill(ds, "LocationInfo");
          cboDa.Fill(cboDs, "AddressTypes");
      
          cboCol.Width = 100;
          cboCol.DataSource = cboDs.Tables\[0\];
          
          cboCol.ValueMember = "ID";
          cboCol.DisplayMember = "Type";
          DataTable dt = ds.Tables\["LocationInfo"\];
         
          grdAddressInfo.DataSource = dt;
      
          /\*
           \* Somehow make the ComboBox Column display the correct Dropdown object
           \* Based upon the AddrType field.
           \*/
      
          //Column formatting
          grdAddressInfo.Columns.Insert(0, cboCol);
          grdAddressInfo.Columns\[0\].HeaderText = "Address Type";
          grdAddressInfo.Columns\[1\].HeaderText = "Ranged Value";
          grdAddressInfo.Columns\[2\].HeaderText = "From";
          grdAddressInfo.Columns\[3\].HeaderText = "To";
      
          dbCon.Close();
      
      U Offline
      U Offline
      User 4467139
      wrote on last edited by
      #2

      You can create objects of the items and then add the list of objects to the combo box.This way you can access the selected object from the datagridview cell.

      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