DataGridView daily problems...

i have a DataGridview with some ComboBox cells in there.

each row should have a different list of items to select the value from.

you can set the bindingsource on Enter and reset the bindingsource to the list with all items on EndEdit...

in dataGridView CellEnter
if (e.ColumnIndex == CELLINDEXWhereComboBoxIs) { //Set DataSource of cell if comboBoxCell DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView[e.ColumnIndex, e.RowIndex]; cell.DataSource = bindingSourceFiltered; }

in dataGridView CellEndEdit

if (e.ColumnIndex == CELLINDEXWhereComboBoxIs) { //set DataSource to BindingSource with all items DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)dataGridView[e.ColumnIndex, e.RowIndex]; cell.DataSource = bindingSourceAll; }

you can add filters to the bindingsource!
but only if the underlying list supports filtering!!


pkdenver said...

I'm doing something similar but trying to change the datasource for the combobox cell as I add the row. My method is failing but not sure why. it gives me -

Index was out of range. Must be non-negative and less than the size of the collection.

Parameter name: index

If I try to add the column in the Rows.add method with the other columns, I get: DataGridViewComboBoxCell value is not valid.

Here is the code:
foreach (DataRow dr in dt.Rows)
int itemID = Convert.ToInt32(dr["ItemID"]);
col1 = dr["ComponentDescription"].ToString();
col2 = Convert.ToInt32(dr["TotalQuantity"]);
col3 = dr["UnitDescription"].ToString();

DataGridViewComboBoxCell cboCell = GetComboBox(itemID);
int index = grdBOM.Rows.Add(col1, col2, col3);
grd.Rows[index].Cells["LotNumber"] = cboCell; //Column is defined as a comboBoxColumn in other method.


private DataGridViewComboBoxCell GetComboBox(int itemID)
ArrayList lotItems = new ArrayList();
dsDataAccess.dtItemLotReceivedListDataTable dtLots = new dsDataAccess.dtItemLotReceivedListDataTable();
dtLots = doh.GetRawLotList(itemID);
foreach (DataRow dr in dtLots.Rows)
lotItems.Add(new ComboBoxItems((string)dr["Lot"], Convert.ToInt32(dr["ItemID"])));
DataGridViewComboBoxCell dgvComboBoxCell = new DataGridViewComboBoxCell();
dgvComboBoxCell.ToolTipText = "Some Text";
dgvComboBoxCell.DataSource = lotItems;
dgvComboBoxCell.DisplayMember = "Display";
dgvComboBoxCell.ValueMember = "Value";
return dgvComboBoxCell;
catch (Exception ex)
return null;

Is there an easier way to accomplish this?

.peter.gfader. said...

I'm cant understand why you would do this when you add a row...

the logic of this Filter Solution is:
you have a list with many items, but in different rows you want to display only part of this long list...

you could use the bindingSource AddingNew event to set some fields when adding a row...

void bindingSourceTEST_AddingNew(object sender, AddingNewEventArgs e)
MyObject temp = new MyObject();
temp.Prop1 = -1;
temp.PropXX = "Name";
e.NewObject = temp;

Janice Zaguirre said...

Thanks! a very helpful post!

Post a Comment

Latest Posts

Popular Posts