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. ValueMember and DisplayMember on DataGridViewComboBoxCell

ValueMember and DisplayMember on DataGridViewComboBoxCell

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.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    I'm hoping someone can help because I'm stumped here. I have a DataGridView that is populated with a SQL query. For simplicity's sake, let's say the SQL query is as follows:

    SELECT Name, Number, Lookup FROM ValuesTable

    Now, depending on the value of the 'Lookup' column, the value in the 'Number' column can either be a straight forward value (when Lookup==0) or a reference to a different table (when Lookup==1), in which case I want a ComboBox in the cell which shows the various options in that other table. In other words, I can't really use DataGridViewComboBoxColumn because not all cells in the column are ComboBoxes, only those for which the Lookup value is 1. So far, so good. In the DataBindingComplete event handler of the DataGridView I have code similar to the following to put a ComboBox (and populate it) into the 'Number' cell of each row that has a value of 1 in the 'Lookup' cell.

    foreach (DataGridViewRow row in myDataGridView.Rows)
    {
    if (row.Cells["Lookup"].Value.ToString() != "0")
    {
    DataGridViewComboBoxCell myComboCell = new DataGridViewComboBoxCell();
    using (SqlCommand mySqlCommand = new SqlCommand("SELECT Number, Text FROM LookupTable"))
    {
    using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
    {
    DataSet ds1 = new DataSet();
    DataTable dt1 = new DataTable();
    ds1.Tables.Add(dt1);
    ds1.Load(mySqlDataReader, LoadOption.PreserveChanges, ds1.Tables[0]);
    myComboCell.DataSource = ds1.Tables[0];
    myComboCell.DisplayMember = "Text";
    myComboCell.ValueMember = "Number";
    }
    }
    row.Cells["Value"] = newCell;
    }
    }

    This sort of works, but not quite. All the appropriate cells in the DataGridView has a ComboBox inside them and the options of the ComboBox are those retrieved from the Lookup Table. But when an option is selected in a ComboBox and the focus is moved away from that cell, the actual value displayed in the cell is the number by which the value was looked up. Also, if the DataGridView is populated (like described above), all the cells with ComboBoxes show the number that is stored in the 'Number' field of ValuesTable, not the 'Text' field of LookupTable like myComboCell.DisplayMember = "Text" dictates. I'd really appreciate if someone co

    E 1 Reply Last reply
    0
    • D Dewald

      I'm hoping someone can help because I'm stumped here. I have a DataGridView that is populated with a SQL query. For simplicity's sake, let's say the SQL query is as follows:

      SELECT Name, Number, Lookup FROM ValuesTable

      Now, depending on the value of the 'Lookup' column, the value in the 'Number' column can either be a straight forward value (when Lookup==0) or a reference to a different table (when Lookup==1), in which case I want a ComboBox in the cell which shows the various options in that other table. In other words, I can't really use DataGridViewComboBoxColumn because not all cells in the column are ComboBoxes, only those for which the Lookup value is 1. So far, so good. In the DataBindingComplete event handler of the DataGridView I have code similar to the following to put a ComboBox (and populate it) into the 'Number' cell of each row that has a value of 1 in the 'Lookup' cell.

      foreach (DataGridViewRow row in myDataGridView.Rows)
      {
      if (row.Cells["Lookup"].Value.ToString() != "0")
      {
      DataGridViewComboBoxCell myComboCell = new DataGridViewComboBoxCell();
      using (SqlCommand mySqlCommand = new SqlCommand("SELECT Number, Text FROM LookupTable"))
      {
      using (SqlDataReader mySqlDataReader = sqlCommand.ExecuteReader())
      {
      DataSet ds1 = new DataSet();
      DataTable dt1 = new DataTable();
      ds1.Tables.Add(dt1);
      ds1.Load(mySqlDataReader, LoadOption.PreserveChanges, ds1.Tables[0]);
      myComboCell.DataSource = ds1.Tables[0];
      myComboCell.DisplayMember = "Text";
      myComboCell.ValueMember = "Number";
      }
      }
      row.Cells["Value"] = newCell;
      }
      }

      This sort of works, but not quite. All the appropriate cells in the DataGridView has a ComboBox inside them and the options of the ComboBox are those retrieved from the Lookup Table. But when an option is selected in a ComboBox and the focus is moved away from that cell, the actual value displayed in the cell is the number by which the value was looked up. Also, if the DataGridView is populated (like described above), all the cells with ComboBoxes show the number that is stored in the 'Number' field of ValuesTable, not the 'Text' field of LookupTable like myComboCell.DisplayMember = "Text" dictates. I'd really appreciate if someone co

      E Offline
      E Offline
      egypogramer
      wrote on last edited by
      #2

      dear dewald try this order display member value member datasource i hope it help you

      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