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;
}
}