views:

566

answers:

2

How can I prevent bindingSource Current item from changing? (there is no changing event with cancel argument...)

This is scenario:

  • I have a dataGridView, and text-boxes on the same form.
  • I am using text-boxes to change values in the datasource (with standard databinding)
  • Bindings are written manually (After save button is clicked)

  • When user selects another row using DataGridView, bindingSource.Current propery is changed, and text boxes are updated with values from selected row. Changes that user entered are lost.

Is there any way to prevent this problem?

Can I prevent bindingSource.Current property from changing?

Is there any better option to prevent this behaviour?

(disabling dataGridView is unfortinutelly not an option)

A: 

I have a somewhat similar framework with both grid and textboxes on same form. When user clicks the EDIT button (or Add), I just disable the gridview control itself...

MyDataGrid.Enabled = false;

continue editing..

Then in the SAVE, if all is ok,

MyDataGrid.Enabled = true;

DRapp
Is disabling datagridview confusing for the end users? Did you tweaked disabled grid appearance?
Emir
+1. I would also probably set BackColor to Control or Butonface
Steve
The simplest answer is just enable/disable the grid. If you want to get fancier and change colors, fonts, or other things you can. I've even seen in other threads how people will create a "shape" object that can have a soft grey-shading or other visual indicator, and have it directly overlap the entire grid based on its positions / coordinates. Then show/hide that control while enable/disable grid respectively to give the user a visual indicator that it has been disabled.
DRapp
A: 

It sounds like you don't want to not change bindingSource.Current, but rather you want to save the contents of the text boxes before you change the current row? If you've bound a collection to the bindingSource, then don't the text boxes refer to properties in the current element in that collection?

I'm not really sure what you're trying to do, but a shot in the dark might be to bind the same DataSource to two different BindingSource objects, something like this:

gridBindingSource.DataSource = theDataSource;
textBoxBindingSource.DataSource = theDataSource;
myDataGrid.DataSource = gridBindingSource;
firstNameTextBox.Bindings.Add (new Binding ("Text", textBoxBindingSource, "FirstName"));

but this would be weird, because if theDataSource is appropriate for a grid control, then it's a collection of things that have a FirstName property. Maybe if you were more specific in your question.


ETA: If you want to save the text box contents to the current row, call ValidateChildren () on the container before the bindingSource.Current property changes.

XXXXX
Your answer definitely gave me a new perspective for my problem. This could easily be solution for my problem.
Emir
This is solution I will implement. It's more complex than simply disabling grid, but It gives me more features (Users can browse grid while adding new record, e.g. for reference purpose)
Emir