mario
mario

Reputation: 11

Stuck with datagridview and combo box column

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

Upvotes: 1

Views: 2258

Answers (3)

Deepul Sharma
Deepul Sharma

Reputation: 11

Datagrid Combo-Box value will retain the string value but will refresh any integer values automatically.

Here is what you need to do :

-When populating Combo-Box value to just convert its value to toString(). -Also if you are setting a default select value also set it with string type.

-Your Combo-box will automatically retain the value selected even after refreshing.

:)

Upvotes: 1

ioWint
ioWint

Reputation: 1629

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

Upvotes: 0

Oliver John
Oliver John

Reputation: 135

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?

Upvotes: 1

Related Questions