views:

659

answers:

3

I have a typical requirement.

I have a datagridview with a combobox column(items loaded at design time). When a user selects an item from combobox, remaining rows gets updated in database based on the selectedItem and dgv gets refreshed.

Problem is the combo box will lose its current selection and goes to unselected state. I want to retain the selected item even after dgv refreshed.

Could anyone help me out

Thanks in advance

A: 

I am using winforms with c#. Let me expalin.

Requirement is simple. I have a form with datagridview loaded on form load event. Gridview has a comboboxcolumn bound at design time. User selects an item from the combo box column which updates remaining columns of that row based on some business logic at run time.

Inbetween the updatations the changes go into the database and it is refetched to the datasource. Once it is refetched, the selection in the combo box columns is lost and it goes to unselected mode.

I want to retain the selected item even after reloading dgv from database.

Also, I need a suggestion on how can I improve my datagridview for this requirement. Can I do this without using dgv combo box column. (having a custom bound combo box integrated into the dgv)

mario
I don't understand. Data binding is the process of tying value in the database and selected value of the combobox. If combox is in "unselected" mode it means that the database value is equal to "unselected". Be it null or something else.
Jacob Seleznev
you might want to consider "editing" your original post when making clarifications. That helps keep the info in one spot making it easier to give a direct answer.
Jason D
+1  A: 

Do you mean that you're using an unbound comboboxcolumn? If so, the value can't automatically be persisted when refreshing the datasource. You need to store the selected value before updating and setting it in code after refresh.

If your column is actually databound, the selected value is either not stored in the database or you have some data type problem.

Is the combobox there to let the user select a value for the field or do you use it as a way to execute a command on the record?

Do you have any code you can post?

Oliver John
A: 

have a combobox in ur datagridview. Assign values to it using a bindingsource. then write an eventhandler for the datagridviews "EditingControlShowing" event. in that, remove had handler if any exists for the comboboxes Selectedindexchanged event. then add an event handler for the selectedIndexChanged event say "ComboBoxValueChanged"

in that "ComboBoxValueChanged", DirectCast the sender to System.Windows.Forms.DataGridViewComboBoxEditingControl and get the selected value of it. Now use it to compute any value you want.

you may wana refer to this http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxeditingcontrol.aspx

ioWint