Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > dynamically binding data to a combobox in datagridview
 

dynamically binding data to a combobox in datagridview

I have a datagridview which is bound to a databindingsource
one of the column in this datagridview is a combobox
the combobox is also bound to a different binding source
now my requirement is that once an item from the the combobox is entered into a row
that item should not be shown in the combobox later.
for eg. i have doctor,engineer,advocate in my combobox
so when i enter the first row all items are displayed and in the first row i have selected doctor from the combobox.
now when i enter the second row of the grid the combobox should only have engineer and advocate and viceversa
  • Moved byUmeshMR Friday, September 11, 2009 5:35 PMMisroute (From:Transactions Programming)
  •  
Suraj Chandran  Friday, September 11, 2009 9:31 AM

If you are filling the combobox from database(data source), you cannot delete the item while running ,datasource property wont be allowed for that.
In this case, you have to update the datasourceused to fill the combobox after each selection and then fill the combobox items after doing a clear.

Example:

Write code to delete the selected item from the datasource then fill the combobox

Dim qdfsSelDate As New SqlCommand("select distinct exec_date from t_report_master", frmSelectCategory.conMssqlSeoserver)
Dim sdsFillCom As New DataSet
dcboSelect.Items.Clear()
Dim sdaFillCom As New SqlDataAdapter(qdfsSelDate)
sdaFillCom.Fill(sdsFillCom)
qdfsSelDate.ExecuteNonQuery()
If you fill the comboxbox using datareader without binding to datasource like use a datareader to fill all relavent items in combobox by reading from datasource, then you can delete the combobox items after each selection easily without getting any error.

Hope this will help you..:)
Rohini Chavakula  Monday, September 14, 2009 7:56 AM
Before filling the secondrow in grid, delete the combobox item and refresh it.
combobox.items.remove(combobox1.selecteditem)
Rohini Chavakula  Saturday, September 12, 2009 5:55 AM
Thanks,
but An errror is Shown:
Items collection cannot be modified when the DataSource property is set.
Suraj Chandran  Saturday, September 12, 2009 9:16 AM

If you are filling the combobox from database(data source), you cannot delete the item while running ,datasource property wont be allowed for that.
In this case, you have to update the datasourceused to fill the combobox after each selection and then fill the combobox items after doing a clear.

Example:

Write code to delete the selected item from the datasource then fill the combobox

Dim qdfsSelDate As New SqlCommand("select distinct exec_date from t_report_master", frmSelectCategory.conMssqlSeoserver)
Dim sdsFillCom As New DataSet
dcboSelect.Items.Clear()
Dim sdaFillCom As New SqlDataAdapter(qdfsSelDate)
sdaFillCom.Fill(sdsFillCom)
qdfsSelDate.ExecuteNonQuery()
If you fill the comboxbox using datareader without binding to datasource like use a datareader to fill all relavent items in combobox by reading from datasource, then you can delete the combobox items after each selection easily without getting any error.

Hope this will help you..:)
Rohini Chavakula  Monday, September 14, 2009 7:56 AM

You can use google to search for other answers

Custom Search

More Threads

• Datagrid Best Practice?
• Binding selected column only to datagrid
• Excel automation
• How to embed child datagrid into its parent in windows forms
• Initializing a Windows Forms datagrid
• Combobox and Datagridview Data's
• databind to transformed XML doc is not relected in datagrid
• DataGrid Sum into TextBox
• Suppressing repeating values in rows with the DataGridView
• How can I set the default Font Style to TimesNewRoman-11 in C#.net Windows Application?