0

I have a datatable that is filled from a database. I load a bindingsource with that table.

Sub LoadData()

Dim bsTemp As BindingSource = New BindingSource
bsTemp.DataSource = dtTemp

End Sub

I then have other code programmatically editing the values in the datatable. I NEVER call AcceptChanges() ..let me be clear NEVER.

I do call bsTem.EndEdit() and I also call that on my dtTemp.Row(x).EndEdit() Whenever I make a change to it.

So now all I want to do is compare the two rows (I know I can do this with a for each column but I am not wanting to do that.)

I would like to know how to make this work:

Dim modview As New DataView(dtTemp.Copy, "", "Id", DataViewRowState.ModifiedCurrent)

Dim origView As New DataView(dtTemp.Copy, "", "Id", DataViewRowState.ModifiedOriginal)

So I can then perform something like this:

    Dim rowComparer As DataRowComparer(Of DataRow) = DataRowComparer.Default

    IsEqual = rowComparer.Equals(origRow.Row, modRow.Row)

When I do this both views show the Modified data, one of them should only show me the Original unmodified Row.

I know I can do this [C# version]:

SomeDataRow[0, DataRowVersion.Original] //by index
SomeDataRow["ColumnName", DataRowVersion.Original]

But again tis works on a column by column basis - me being the iterator - and I see no reason to do that when the DataView supposedly has this built in . So what could I be doing wrong that I do not see the original version .

Ken
  • 2,518
  • 2
  • 27
  • 35

1 Answers1

0

The New DataView(..) does not determine which rows to copy, it only says what the state of the rows after they are in the view will have. Your first parameter says which rows dtTemp.Copy.

Since the copy method of a datatable is a copy of all rows, you might want to use something like the select method, which allows you to filter based on state.

dtTemp.Select("","",ModifiedOriginal)

EDIT:

There must be an issue with getting it that way. According to the example given from MSDN, it should work if you use the DataView.RowStateFilter. I tested it and it did work. I think the key is that the DataView controls what you see. If you look at the raw data through the DataRow, it is always current.

Steve
  • 5,585
  • 2
  • 18
  • 32
  • It seems odd but this also provides the same thing - I see only the changed row but not the original row - yet if I perform this SomeDataRow("myColumnName", DataRowVersion.Original) I get the original value for the column! What is going on? – Ken Oct 20 '16 at 01:35
  • I created a test and it didn't work for me. So I followed MS's example and it worked if you use the `RowStateFilter` on the DataView. See this example: https://msdn.microsoft.com/en-us/library/system.data.dataviewrowstate(v=vs.110).aspx – Steve Oct 20 '16 at 22:48
  • that is the example I used, and when I check I see it filters the rows but when you look at the actual values in those rows - all of those filters will show the changed column with the changed value and not the Unmodified value or otherwise. In order to do that I must go to the row object and the column and request the DataRowVersion to get that version (column by column for each row. Quick question - are you seeing this same thing in yours. I am on win7 pro 64 .NET4.0 I am watching the values in the denugger of VS 2015 CE. – Ken Oct 21 '16 at 02:44
  • When I run the example, as is, I get the values you expect to get. There is a difference between the 2 lines of code when you debug the program. `dataTable.Rows(1)("dataColumn")` and `dataView(1)("dataColumn")`. The first one accesses the RowsCollection, which will show the current value, the second accesses the DataRowViewCollection, which shows you the original value. It works, just not like you (or I) think it should. – Steve Oct 21 '16 at 14:17