Question

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)

Was it helpful?

Solution

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.

OTHER TIPS

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;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top