Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridView DataGridViewComboBox Empty
 

DataGridView DataGridViewComboBox Empty

following are my code, why combobox are empty? on debug mode there are item in the lists. but the datagridview combobox is empty. what is missing on the code?

 


ArrayList

 

listProducts;

ArrayList

 

listSupplier;

DataGridView

 

dgv = new DataGridView();

DataGridViewComboBoxColumn

 

comBoxColSupplier = new DataGridViewComboBoxColumn();

listProducts = db.ProductItems(strTemp); //return list of Product, Supplier and Category

foreach

 

 

(DataRow product in listProducts)

{

 

ArrayList lstSuppliers = db.Category((int) CategoryID); // return list of Supplier ID base on Category

foreach

 

 

(DataRow Supplier in listSupplier)

{

comBoxColSupplier.Items.Add(Supplier ); //add list of SupplierID to DataGridViewTextBoxColumn

}

this

 

 

.dgv.Rows.Add(Product, comBoxColSupplier, CategoryID)

comBoxColSupplier.Items.Clear();

}

Jackl28  Tuesday, October 06, 2009 6:23 AM
Hi,
In the last line you are clearing items in the combobox.remove that line and check.i hope it will help you
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Proposed As Answer byGnanadurai Thursday, October 08, 2009 5:37 AM
  •  
Gnanadurai  Tuesday, October 06, 2009 6:41 AM
Hi Gnanadurai,

If remove Items.Clear() all supplier will show up in this column, I need to only show supplier for each category. each category have it list (Category A, {1,2,3}, Category B {3,4,5}).
Jackl28  Thursday, October 08, 2009 5:45 AM
Hi,
Now you can able to view the data in your combobox?
could you please little more clear with your requirement.then only i can able to help you.
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  23 hours 14 minutes ago
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.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.bindingsource.filter.aspx

You can get more about how to bind a ComboBox Column from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.datasource.aspx

You can get more about how to bind a DataGridView from:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx

Regards,
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.
  • Proposed As Answer bya-caridad- 12 hours 16 minutes ago
  •  
Aland Li  23 hours 7 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• Binding ArrayList (of Structures) to a Datagrid Question?
• How to get Port name on which printer is attached?
• Lookup where is the primary key of a foreign key
• Databinding adding and saving new records
• BindingSource's AddNew fired automatically if no object in collection
• Custom DataGridView cell
• Question on Lesson 9 -Databinding Data to User Interface Controls
• DataGridView cell editing
• Problem with TextBox and MS Access
• WF: Scheduler Calendar in C#