How to tell if user has modified data using bindingsource?

后端 未结 12 2181
南方客
南方客 2021-01-05 06:05

I have a DataGridView bound to a bindingsource which is bound to a List. The user clicks a row that goes to a form with textboxes, etc. The textboxes a

12条回答
  •  猫巷女王i
    2021-01-05 06:36

    I set up a fairly simple mechanism, as follows:

    1. After binding my controls, I run a method that finds all the bound controls and saves their current values (I do a ReadValue() just to be sure I've got the values from the DataSource) in a Dictionary that maps a control to its value (there's a small method that gets the appropriate value for each kind of control that I have).
    2. I also add a change-event handler for each one (again, the specific event is determined by the type of control, but they all point to the same handler)
    3. The change-handler checks the current value against the Dictionary value. If it's different then it acts accordingly (in my case it switches the Close button for the Cancel button). If it's the same it checks all the other bound controls, so that if nothing is different it can switch Cancel back to Close; it's a nice feature of this method that it also recognizes when a change has been undone, even if it's by re-entering the original value.
    4. Before leaving, if there are changes to be saved I loop through the bound controls again to do WriteValue(), just in case WinForms didn't get around to propagating some change.

    I can share the source if anyone is interested.

提交回复
热议问题