Hi Jackl28,
Gnanadurai's reply indicate the root cause. You need to move the code "Items.Clear()" before the code to add the items. In other words, we need to clear the items at first and then add the item one by one.
You can also use BindingSource to bind the data source to the ComboBox column and set the Filter property of the BindingSource to only show items in one category. This is the code snippet:
DataGridView dgv = new DataGridView();
DataGridViewComboBoxColumn comBoxColSupplier = new DataGridViewComboBoxColumn();
BindingSource bindSrc;
private void InitComboBoxColumn(string catId)
{
DataTable listProducts = db.ProductItems(strTemp); //return list of Product, Supplier and Category
//Create a BindingSource and set its DataSource.
bindSrc = new BindingSource();
bindSrc.DataSource = listProducts;
//Bind the BindingSource to the DataGridViewComboBoxColumn
comBoxColSupplier.DataSource = bindSrc;
//Set which column is used to store the value and which column is used to show.
comBoxColSupplier.ValueMember = "valueColumnName";
comBoxColSupplier.DisplayMember = "showColumnName";
//Filter the BindingSource to only show the records in one category.
bindSrc.Filter = String.Format("CategoryID='{0}'", catId);
}
You can also bind the data source directly to the DataGridView, not add row one by one.
You can get more about BindingSource and its Filter property from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.aspxhttp://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspxYou can get more about how to bind a ComboBox Column from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspxYou can get more about how to bind a DataGridView from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspxRegards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.