question

lkubler-2593 avatar image
0 Votes"
lkubler-2593 asked TimonYang-MSFT commented

Combobox in datagridview, fire method on selectedindexchange

Hi,

I have a combobox added to a datagridview. Currently, when the user makes a selection in the combobox they need to hit a button on the form that writes the data back to the backend database and then refreshes the datagridview to reflect this change plus any other changes made by other users. Works Ok but I was thinking it would be better if it would automatically perform this refresh of the data when the user selects an item in the combobox.

frmRefresh() is the method that is called when the Refresh button is clicked, I thought it would be simple enough to all it from the combobox_selectedIndexChanged method but that did not work.

What am I missing?

Thanks in advance!

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @lkubler-2593
First, Karen's solution should solve your problem. I'm just a little curious about what caused your current problem.
You said, "that did not work", but we don't know how to reproduce your problem. Could you please provide some code for us to test?

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered

Hello,

The following code sample shows how to work with a ComboBox columns in a DataGridView. When a change is made to either DataGridViewComboBox a method, [CurrentValuesView][2] has the new values. From here you can update your database. No buttons needed.

There is one button but that can be totally ignored.

What's left? pass changes to a method that updates the table(s).

  • Data script included

  • Actual database type does not matter so the code can be adapted to any database


94030-figure1.png


94056-codedbykpmvp.png


[2]: https://github.com/karenpayneoregon/datagridview-combobox-cs/blob/master/DataGridViewComboCS/Form1.cs#L106


figure1.png (10.7 KiB)
codedbykpmvp.png (3.5 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.