Serious Bug in BindingContext Position?

M

Mark Olbert

There appears to be a very serious bug in the BindingContext's position handling code. When a
BindingContext object is based on a DataView, and a new record is added to the DataView, the
BindingContext object will not "allow" you to position onto the new record.

The following code adds a new record to a DataTable that is the basis for a DataView object called
formView.

The last line is the one that fails.

// DataView formView = new DataView(dataTableBehindView); // (occurs earlier in life cycle of form)

this.BindingContext[formView].EndCurrentEdit(); // so new record doesn't trample pending edits

DataRow newRow = OnNewRecord(); // creates the new record; works fine
dataTableBehindView.Rows.Add(newRow);
formView.Sort = "displayfield"; // without this line, DataView doesn't "recognize" the new DataRow

// this next line correctly finds the new row in the DataView (e.g., row #2)
int rowNum = formView.Find(newRow["value of display field in new row"]);

// this line FAILS; Position has the same value before and after the assignment,
// and no exceptions are thrown, even though Position != rowNum (!!!)
this.BindingContext[formView].Position = rowNum;

Any workarounds for this problem would be most appreciated!

I'd also like to know how in the world an assignment statement can fail without any exceptions being
thrown. That's just downright goofy.

- Mark
 
W

William Ryan

I can't tell from the code snippet, but try using this.BindingContext.AddNew
to add your row. I've used this in a few different apps and haven't seen
any problems.

Another thing..if you move your bindingcontext.position to specific values,
so 0 or 1 or something other than the new row, is it working ok for you?
One other thing that would help diagnose the problem. Before you add the
new row, see what BindingContext.Count is. Compare that number with the
count after adding the row to the dataview and let me know what happens.

I'm going to check on something just to verify it, give me about 30 minutes
and I'll post back. If you can let me know about the above though, I think
we can figure it out.

Cheers,

Bill
 
M

Mark Olbert

Bill,

Thanx for the quick reply. I posted some of the replies to your questions in another post on this
newsgroup, but I'll repeat (and augment) them here.

1) Using BindingContext.AddNew() doesn't work, either because I can't find the record using
DataView.Find() or I can't reposition (I don't remember which). BTW, using DataView.AddNew() doesn't
work, either.

2) I can use BindingContext.Position to move to any record other than the just-added record.

3) DataView.Count shows that the new record was added to the DataView. Interestingly, >>I<< can find
the new record by specifically identifying the DataView row myself. It's just that in some cases
DataView.Find() can't find it, or, if it does, BindingContext.Position won't let me move to it (it
just fails, silently...which, personally, I think is a bug regardless of whether or not it's
something that I'm doing in my code that sets up a "bad" situation -- "simple" assignments should
never fail silently!)

- Mark
 
W

William Ryan

Agreed about the silent thing, everywhere else you'd get an indexoutofrange.
I'm actually quite surprised about it.

What about the DataTable find though? Also, what are the counts on the
binding context before and after? Does it show the added row?
Mark Olbert said:
Bill,

Thanx for the quick reply. I posted some of the replies to your questions in another post on this
newsgroup, but I'll repeat (and augment) them here.

1) Using BindingContext.AddNew() doesn't work, either because I can't find the record using
DataView.Find() or I can't reposition (I don't remember which). BTW, using DataView.AddNew() doesn't
work, either.

2) I can use BindingContext.Position to move to any record other than the just-added record.

3) DataView.Count shows that the new record was added to the DataView. Interestingly, >>I<< can find
the new record by specifically identifying the DataView row myself. It's just that in some cases
DataView.Find() can't find it, or, if it does, BindingContext.Position won't let me move to it (it
just fails, silently...which, personally, I think is a bug regardless of whether or not it's
something that I'm doing in my code that sets up a "bad" situation -- "simple" assignments should
never fail silently!)

- Mark
 
M

Mark Olbert

I have some more information on this problem, which I am now very sure is a bug in the way the .NET
DataBinding code works.

If anyone from MS is monitoring this list, please see the thread entitled "Adding New Record to
DataView -- Why So Difficult?"

- Mark
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top