Thursday, November 28, 2013

DataGridViewComboBoxColumn cell value returning null

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.



No comments:

Post a Comment