locked
DataGridViewComboBoxColumn cell value returning null RRS feed

  • Question

  • I am trying to get a value from a DataGridViewComboBoxColumn cell that is named "Item".

    When I run the program and make a selection from the combobox, string itemValue always returns "";

    I am also getting an error on the line

    cmbItems.ValueMember = productNames.ToString();

    What am I doing wrong in trying to get the value?

    My Code:

    // set values to combobox column cells in datagridview DataGridViewComboBoxColumn cmbItems = (DataGridViewComboBoxColumn)GridSellProducts.Columns["Item"]; cmbItems.DataSource = productNames;

    cmbItems.DisplayMember = cmbItems.ValueMember;
    cmbItems.ValueMember = productNames.ToString(); cmbItems.AutoComplete = true; GridSellProducts.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(GridSellProducts_EditingControlShowing);

    and

    private void LastColumnComboSelectionChanged(object sender, EventArgs e)
    {
          string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].FormattedValue.ToString();
    
          // get item price
          foreach (var item in itemListing)
          {
               if (item.name == itemValue)
               {
                        unitPrice = item.selling;
                        break;
               }
          }
    }
    
    private void GridSellProducts_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
         if (GridSellProducts.CurrentCell.ColumnIndex == 0 && e.Control is ComboBox)
         {
              ComboBox comboBox = e.Control as ComboBox;
              comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
         }
    }

    Why can't I get the selected cell value?

    productNames is a list of string item names.


    • Edited by kkjay Friday, November 22, 2013 11:47 AM
    Friday, November 22, 2013 10:40 AM

Answers

  • Try the below code. If you are populating combobox from a data base then you have to change the logic in populateCmbBoxes() function and rest logic remains same.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace Hardwareinfo
    {
        public partial class TestCmbBoxDGV : Form
        {
            public TestCmbBoxDGV()
            {
                InitializeComponent();
            }
    
            private void TestCmbBoxDGV_Load(object sender, EventArgs e)
            {
                populateCmbBoxes();
            }
    
            ComboBox cb = null;
    
            private void dGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (dataGridView1.CurrentCell.ColumnIndex == comboBoxColumn.Index)
                {
                    cb = e.Control as ComboBox;
                    if (cb != null)
                        cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
                }
            }
    
            void cb_SelectedIndexChanged(object sender, EventArgs e)
            {
                string itemValue = cb.Text;
    
                // and your logic here
            }
    
            private void populateCmbBoxes()
            {
                List<string> list = new List<string>();
                list.Add("Item1");
                list.Add("Item2");
                list.Add("Item3");
                list.Add("Item4");
                comboBoxColumn.DataSource = list; // Don't use comboBoxColumn.ValueMember and comboBoxColumn.DisplayMember properties if you are just populating combobox by a list of strings.
            }
        }
    }


    • Proposed as answer by Jeetendra Khandelwal Monday, December 2, 2013 7:31 AM
    • Marked as answer by kkjay Monday, December 2, 2013 9:48 AM
    Friday, November 29, 2013 9:13 AM
  • Hi kkjay,

    You mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article :http://msdn.microsoft.com/en-us/library/ms404353%28v=VS.90%29.aspx

    You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.

    You can try this code:

    cmbItems.DataSource = productNames;
    
    cmbItems.DisplayMember = cmbItems.ValueMember="Name";
    
    

    string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].Value;



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Proposed as answer by Eason_H Friday, November 29, 2013 2:44 AM
    • Edited by Eason_H Friday, November 29, 2013 2:53 AM
    • Marked as answer by Eason_H Monday, December 2, 2013 1:50 AM
    Friday, November 29, 2013 2:43 AM

All replies

  • If productNames is just a List<string> then you don't need to set the ValueMember and DisplayMember. The combo box will implicitly call ToString() on the items if you do not supply them.

    The ValueMember and DisplayMember are used when you are binding to objects with properties and you need to specify which property should be shown and which should be the value. For instance, if you had a collection of Person objects the Display member might be "FullName" and the value member might be "PersonId".

    When you are setting the value member to productNames.ToString() you are telling it to use the System.Collections.Generic.List[System.String] property of productNames. Of course, productNames has no such property.


    Bob - www.crowcoder.com

    • Proposed as answer by Eason_H Monday, November 25, 2013 2:33 AM
    Friday, November 22, 2013 1:24 PM
  • Thanks for your reply.

    I have removed the display and value members. The problem that I now have is that after I select a value in the combobox and put a break point when running my code in Debug, the value

    itemValue 

    is always = "";  ie string.empty

    string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].FormattedValue.ToString();

    What am I doing wrong and I need to get the itemvalue in order to fetch their prices?

    Tuesday, November 26, 2013 7:26 AM
  • Instead of FormattedValue, what is Value?

    Are you even handling the CellFormatting event?


    Bob - www.crowcoder.com

    Tuesday, November 26, 2013 12:50 PM
  • Hi,

    I am new to using datagrid views so I have been learning from just a few days ago.

    Value instead of Formatted Value is still returning null.



    • Edited by kkjay Tuesday, November 26, 2013 7:48 PM
    Tuesday, November 26, 2013 7:38 PM
  • Hi kkjay,

    You mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article :http://msdn.microsoft.com/en-us/library/ms404353%28v=VS.90%29.aspx

    You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.

    You can try this code:

    cmbItems.DataSource = productNames;
    
    cmbItems.DisplayMember = cmbItems.ValueMember="Name";
    
    

    string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].Value;



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    • Proposed as answer by Eason_H Friday, November 29, 2013 2:44 AM
    • Edited by Eason_H Friday, November 29, 2013 2:53 AM
    • Marked as answer by Eason_H Monday, December 2, 2013 1:50 AM
    Friday, November 29, 2013 2:43 AM
  • Try the below code. If you are populating combobox from a data base then you have to change the logic in populateCmbBoxes() function and rest logic remains same.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace Hardwareinfo
    {
        public partial class TestCmbBoxDGV : Form
        {
            public TestCmbBoxDGV()
            {
                InitializeComponent();
            }
    
            private void TestCmbBoxDGV_Load(object sender, EventArgs e)
            {
                populateCmbBoxes();
            }
    
            ComboBox cb = null;
    
            private void dGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
            {
                if (dataGridView1.CurrentCell.ColumnIndex == comboBoxColumn.Index)
                {
                    cb = e.Control as ComboBox;
                    if (cb != null)
                        cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
                }
            }
    
            void cb_SelectedIndexChanged(object sender, EventArgs e)
            {
                string itemValue = cb.Text;
    
                // and your logic here
            }
    
            private void populateCmbBoxes()
            {
                List<string> list = new List<string>();
                list.Add("Item1");
                list.Add("Item2");
                list.Add("Item3");
                list.Add("Item4");
                comboBoxColumn.DataSource = list; // Don't use comboBoxColumn.ValueMember and comboBoxColumn.DisplayMember properties if you are just populating combobox by a list of strings.
            }
        }
    }


    • Proposed as answer by Jeetendra Khandelwal Monday, December 2, 2013 7:31 AM
    • Marked as answer by kkjay Monday, December 2, 2013 9:48 AM
    Friday, November 29, 2013 9:13 AM