DataRowView changes when DataView re-ordered

I

IraG

Say you have a sorted DataView, and create a DataRowView that points to
a particular row in this DataView.

If you edit the DataRowView and change its contents so that it re-sorts
into a different position in the DataView, the result is that the
DataRowView will now point to a *different* row in the DataView (the
row that now occupies the same position as the original DataRowView
before the edit that caused it to sort to a different position).

Is this a bug, or a feature?

It's possible that this behavior was present prior to VS 2005, but I
never noticed it.

It seems is that a DataRowView always points to a row with a particular
fixed index location in a DataView. So if rows get re-sorted in a
DataView, a DataRowView can potentially point to a different row. But
I have seen no references anywhere to this behavior.


Dim dt As New DataTable
Dim dr As DataRow
dt.Columns.Add("Col1", GetType(String))

dr = dt.NewRow
dr("Col1") = "D"
dt.Rows.Add(dr)

dr = dt.NewRow
dr("Col1") = "E"
dt.Rows.Add(dr)

Dim dv As DataView = dt.DefaultView
dv.Sort = "Col1 ASC"

Dim drv As DataRowView = dv(1)
Dim drSave As DataRow = drv.Row
Console.WriteLine(drv("Col1")) ' displays "E", as expected
Console.WriteLine((drSave Is drv.Row).ToString) ' displays "True"

drv.BeginEdit()
drv("Col1") = "A" ' change alphabetical order of the last row
Console.WriteLine(drv("Col1")) ' displays "A", as expected

drv.EndEdit()

' EndEdit results in the dt.DefaultView getting re-sorted, with
the
' last row becoming the first row. But drv is now the *new* last
row,
' even though the dataview row that it previously pointed to is now
' drv(0). That is, the EndEdit causes drv to represent an entirely

' different row in the dataview.

Console.WriteLine(drv("Col1")) ' displays "D", not "A" !!!!!!

' drv.Row is no long the same row as it was before the sort column
' was updated

Console.WriteLine((drSave Is drv.Row).ToString) ' displays "False"
 
C

Cor Ligthert [MVP]

Ira,

The dataview shows rows in datatables in a reordered way, therefore it is
clear that the position of those datarowviews change.

However the first row in a datatable will stay the first row if you are not
interting before that row other rows.

I hope this gives an idea,

Cor
 
I

IraG

Cor,

I don't have a problem with the row in the DataView being reordered.
That's exactly as it should be.

My problem concerns a DataRowView being changed to point to a
*completely different* row when you edit it and change a column on
which the DataView it belongs to is sorted.

Look at my example. I changed the sort column value of the DataRowView
to 'A', and then did an EndEdit. After the EndEdit, the column value
changes from 'A' to 'D', and the .Row property now references a
different row in the datatable.

The effect seems to be that if you set drv = dv(i), then the drv will
always be the i'th row in the dataview, even if you update the original
i'th row so that it moves to a different location in the dataview.
That is, the drv will no longer be the original i'th row, but the new
row that occurs at the i'th position after the EndEdit.

I have trouble believing this is not a bug, and a serious one at that.
An object reference being changed to a completely different object by
the framework seems like pretty strange behavior, and it seems like
Microsoft would have at least hinted at this behavior it was intended
to work this way.

My impression is also that it did not work this way prior to .Net 2.0.
 
C

Cor Ligthert [MVP]

Ira,

There are a lot of answers possible, while I understand your problem basicly
is it in my opinion not a general behaviour. This happens if people are
using indexes instead of the currencymanager or are binding the wrong object
to controls.

Therefore do you use the currencymanager and can you show how you have
binded ?

Cor
 
I

IraG

Cor,

I'm not using the currencymanager, and I am not doing databinding.

The sample code I posted shows exactly what the problem is--there's
nothing extra or hidden.

Take a look at the newsgroup thread titled "BUG: DataRowView.Delete()
deletes the wrong row in the table".

In that thread, Kawarjit Bedi (apparently with Microsoft) seems to
concede that there were definitely issues with the DataRowView in
version 1.1, and implies that these were fixed in 2.0. But the problem
(or a related problem) still seems to be there.
 
C

Cor Ligthert [MVP]

Ira,

I now see what you mean, I agree it is strange

I won't know if it is as you wrote about the dataview, probably did that
confuse me.

I see it more as behaviour of that datarowview object you are busy with.

You know that this is the place to report this kind of behaviour.

http://lab.msdn.microsoft.com/productfeedback/Default.aspx

I would than talk about the datarowview.

I hope this helps however something,

Cor
 
G

Guest

Hi IraG,

Thank you, now I know I am not crazy. I have a web application that
worked fine under .net 1.1. However under 2.0 it started exhibiting some
very strange behavior. One of which is exactly the behavior you describe.

I have a web datagrid bound to a dataview. When the user finishes editing
the row and I set the new values into the dataviewrow the row changes as soon
as the sort field is changed. I verified it by displaying the row on every
colomn change. As soon as the sort column changed the row changed to a
different row.

I get around this by making my updates to the underlying datarow. Use the
row property of the datarowview and update the row directly. I did not check
if the datarowview changed but I no longer needed the Datarowview at that
point.

Since .net v1.1 has no concept of currency that suggestion was irrelevant.

I have other strange behaviors that the web app exhibited and had to do a
lot of recoding to get around them.

My beef is that .net 2.0 should not make your applications behave
differently from 1.1 unless you use new features of 2.2

And do not even attempt to let VS 2005 migrate your applications from
2003. It took me longer to clean up then if I had just recreated every
module and cut and paste the code in.

Reply to this thread if you get a resolution from MS so Ill know. Thanks.


--
Robert Pfeffer
VP of Technology
Sun Valley Systems, Inc.
 
D

David Sceppa [MSFT]

This is a change in behavior from ADO.NET 1.0/1.1 to ADO.NET 2.0.
I've spoken to a members of the team that works on the DataSet and they are
working on a blog post that describes the change in more detail. This
change in behavior should have been documented.

David Sceppa
Microsoft
This posting is provided "AS IS" with no warranties,
and confers no rights. You assume all risk for your use.
© 2006 Microsoft Corporation. All rights reserved.
 

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