0

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 ""; ie string.null.

What am I doing wrong in getting this value that I need to set price on another column on the same row?

I checked on other forums and I learn that is is not necessary to equate ValueMember or DisplayMember when dealing with List as a datasource.

private void AutocompleteItems()
        {
            // get products
            productsURL = "https://eko-app.com/Products/list_products/sessionId:" + sessionID + ".json";

            var products = Products.GetProducts(productsURL);

            List<string> productNames = new List<string>();           

            foreach (var p in products)
            {
                var x = p.Product;

                foreach (var pn in x)
                {
                    productNames.Add(pn.name);                   
                }
            }

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

            cmbItems.DataSource = productNames;

            cmbItems.AutoComplete = true;

            GridSellProducts.EditingControlShowing += new
            DataGridViewEditingControlShowingEventHandler(GridSellProducts_EditingControlShowing);
        }



Private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
     string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].FormattedValue.ToString();
// more code   <--- null value shown here always
}

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; //remove event if it was added before
                comboBox.SelectedIndexChanged += LastColumnComboSelectionChanged;
     }
}
2
Contributors
1
Reply
36
Views
3 Years
Discussion Span
Last Post by The Diamonds
0

Hi,

You need to use EditedFormattedValue instead of FormattedValue.

Private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
     string itemValue = GridSellProducts.Rows[GridSellProducts.CurrentCell.RowIndex].Cells["Item"].EditedFormattedValue.ToString();
// more code   <--- null value shown here always
}

Appearently, the cell value would be null while in editing mode.. see more about FormattedValue & EditedFormattedValue here

One more thing, why didn't you use sender object inside the handler method?
e.g:

Private void LastColumnComboSelectionChanged(object sender, EventArgs e)
{
    if (sender is ComboBox)
    {
        ComboBox cbox = sender as ComboBox;
        string itemValue = cbox.SelectedItem.ToString();
    }
}

Good luck.

Edited by The Diamonds

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.