Original Rows

  • Thread starter Thread starter One Handed Man \( OHM - Terry Burns \)
  • Start date Start date
O

One Handed Man \( OHM - Terry Burns \)

My Brain is dead !!!!!

I modify a single row in a DataTable By setting the FirstName column in a
specific row to another name. ( Without Accepting Changes ).

However, this statement still returns the rows with the 'New Value in it'
shouldnt it be the values before I made the change or have I missed
something.

r = Ds1.Tables(0).Select(Nothing, Nothing, DataViewRowState.OriginalRows)



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Yes, I'm aware of this page, however, this refers to a different method
which uses a DataView class to filter the output.

When using the .Select method of a DataTable, this appears to give strange
results.

Try it and let me know.,



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
The select method seems to have a bug in it, however, using the
StateFiltering of the devault view, this works no problem.

Ds1.Tables(0).DefaultView.RowStateFilter = CType(ComboBox1.SelectedItem,
State).MyState

Dim i As Integer

For i = 0 To Ds1.Tables(0).DefaultView.Count - 1

txtConsole.AppendText(Ds1.Tables(0).DefaultView.Item(i)("FirstName") &
(Microsoft.VisualBasic.vbCrLf))

Next


--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi Terry,

Did you try this?
myView.RowStateFilter = DataViewRowState.OriginalRows
Dim r() As DataRow = mytable.Select(Nothing,Nothing,myview.RowStateFilter)

I hope that your brain will come again alive with this?

:-)

Cor
 
No thats doesent work. It appears that the select method does not work
properly. So I just have to do it the way I already said.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Do you use VBNet?
No thats doesent work. It appears that the select method does not work
properly. So I just have to do it the way I already said.
I tested it completly and it did work, there is one strange thing, try this
one.

Module main
Public Sub main()
Dim myTable As New DataTable("myTable")
myTable.Columns.Add("myColumn")
For i As Integer = 0 To 9
Dim myrow As DataRow = myTable.NewRow()
myrow("myColumn") = "Cor " + i.ToString()
myTable.Rows.Add(myrow)
Next
myTable.AcceptChanges()
Dim myView As New DataView(myTable)
myTable.Rows(1)(0) = "Terry"
myView.RowStateFilter = DataViewRowState.OriginalRows
Dim dr() As DataRow = myTable.Select _
("myColumn = 'Cor 1'", Nothing, myView.RowStateFilter)
MessageBox.Show(dr(0)(0).ToString)
myTable.Rows(1).Delete()
dr = myTable.Select _
("myColumn = 'Cor 1'", Nothing, myView.RowStateFilter)
dr(0).RejectChanges()
MessageBox.Show(dr(0)(0).ToString)
End Sub
End Module

In this test when you do not do the Reject Changes it cannot get the deleted
information.

However the part that you showed did work perfectly for me.

Cor
 
Actually, it is not working, this snippit Displays, 'Terry' then 'Cor 1' The
RowState Filter is set to origional rows and so neither of these should have
appeared this way as the second was deleted.

If this is what you see then it confirms my suspicions.




--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi Terry,

I get the idea that you interpret the code not right and want something
else, hoewever it is correct, see my comment inline.

myTable.Rows(1)(0) = "Terry"
myView.RowStateFilter = DataViewRowState.OriginalRows
Dim dr() As DataRow = myTable.Select _
("myColumn = 'Cor 1'", Nothing, myView.RowStateFilter)

In my opinion should this sentence give the currentrows from the table which
had in the originalrowstate "Cor1" in myColumn" from those contains row 1
now "Terry".

And it works completly correct.

Maybe you want something else, however this is what in my opinion is written
in the sentences above.

Cor
 
Ah yes, but you are using a new view, not the default view of the table. If
you use that you will see a difference.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi Terry,

Do you mean this, that gives exactly the same resultst?

Cor

\\\
Module main
Public Sub main()
Dim myTable As New DataTable("myTable")
myTable.Columns.Add("myColumn")
For i As Integer = 0 To 9
Dim myrow As DataRow = myTable.NewRow()
myrow("myColumn") = "Cor " + i.ToString()
myTable.Rows.Add(myrow)
Next
myTable.AcceptChanges()
myTable.Rows(1)(0) = "Terry"
myTable.DefaultView.RowStateFilter = DataViewRowState.OriginalRows
Dim dr() As DataRow = myTable.Select _
("myColumn = 'Cor 1'", Nothing,
myTable.DefaultView.RowStateFilter)
MessageBox.Show(dr(0)(0).ToString)
myTable.Rows(1).Delete()
dr = myTable.Select _
("myColumn = 'Cor 1'", Nothing, _
myTable.DefaultView.RowStateFilter)
dr(0).RejectChanges()
MessageBox.Show(dr(0)(0).ToString)
End Sub
End Module
///
 
ADD THIS CODE TO A BUTTON
--------------------------


'create a new table
Dim MyTable As New DataTable
'create a column
MyTable.Columns.Add(New DataColumn("Data"))
'Create Rows
Dim r As DataRow
Dim i As Integer
For i = 0 To 5
r = MyTable.NewRow()
r("Data") = "Item" & i.ToString()
MyTable.Rows.Add(r)
Next
MyTable.AcceptChanges()


'Ok Lets Print out the rows unmodified
Debug.WriteLine("------- Unmodified ---------")
For Each r In MyTable.Rows
Debug.WriteLine(r("Data"))
Next

'Now lets Modify one element at row(1)
MyTable.Rows(1)("Data") = "Terry Burns"

'Ok Lets Print out the rows modified
Debug.WriteLine("------- Modified ---------")
For Each r In MyTable.Rows
Debug.WriteLine(r("Data"))
Next

'Declare an array of rows
Dim OriginalRows() As DataRow

'Get them from the Table using select statement
OriginalRows = MyTable.Select(Nothing, Nothing,
DataViewRowState.OriginalRows)

Debug.WriteLine("------- These 'SHOULD' be the original
ows ---------")
For Each r In OriginalRows
Debug.WriteLine(r("Data"))
Next


THIS IS THE RESULT
-------------------


------- Unmodified ---------
Item0
Item1
Item2
Item3
Item4
Item5
------- Modified ---------
Item0
Terry Burns
Item2
Item3
Item4
Item5
------- These 'SHOULD' be the original rows ---------
Item0
Terry Burns
Item2
Item3
Item4
Item5

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Yes this samle explicitly sets the RowState Filter of the default view,
whereas, the select statement should return a set of rows based on the
parameters passed, and if the rowstate filter has not been explicitly set
before this call, the select statement does not work. See my sample,
alongside this post.

Cheers for looking at this with me.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi Terry,

With me it are the original rows with new values. Just as I expected.
Deleted rows are in it and added rows not.

A problem I discovered is that it goes wrong when the value is nothing
however I tried that and that is testable to Nothing however the rownumber
is there.

Cor

\\\
OriginalRows = MyTable.Select(Nothing, Nothing,
DataViewRowState.OriginalRows)
Debug.WriteLine("------- These 'SHOULD' be the
originalows(---------")
For Each r In OriginalRows
Debug.WriteLine(r("Data"))
Next
///
 
Hi Terry,

When you add this to your sample you see better how it looks internally

Cor
\\\
Dim ds As New DataSet
ds.Tables.Add(MyTable)
Dim sw As New System.IO.StringWriter
ds.WriteXml(sw, XmlWriteMode.DiffGram)
MessageBox.Show(sw.ToString)
///
 
I have 2 go for a while, I'l come back to this later
Cheers terry

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Hi,

Use a dataview for that to work the way you want.

'create a new table

Dim MyTable As New DataTable

'create a column

MyTable.Columns.Add(New DataColumn("Data"))

'Create Rows

Dim r As DataRow

Dim i As Integer

For i = 0 To 5

r = MyTable.NewRow()

r("Data") = "Item" & i.ToString()

MyTable.Rows.Add(r)

Next

MyTable.AcceptChanges()

Dim drv As DataRowView

'Ok Lets Print out the rows unmodified

Debug.WriteLine("------- Unmodified ---------")

For Each drv In MyTable.DefaultView

Debug.WriteLine(drv("Data"))

Next

'Now lets Modify one element at row(1)

MyTable.Rows(1)("Data") = "Terry Burns"

'Ok Lets Print out the rows modified

Debug.WriteLine("------- Modified ---------")

MyTable.DefaultView.RowStateFilter = DataViewRowState.ModifiedCurrent

For Each drv In MyTable.DefaultView

Debug.WriteLine(drv("Data"))

Next

'Declare an array of rows

'Get them from the Table using select statement

MyTable.DefaultView.RowStateFilter = DataViewRowState.OriginalRows

Debug.WriteLine("------- These 'SHOULD' be the original rows---------")

For Each drv In MyTable.DefaultView

Debug.WriteLine(drv("Data"))

Next



Ken
 
Hi Ken,

This gives exact the same results as I showed Terry in 5 different ways.
On one of those Terry gave this answer.
Yes, I'm aware of this page, however, this refers to a different method
which uses a DataView class to filter the output.

When using the .Select method of a DataTable, this appears to give strange
results.

I get the idea that Terry want to have with this methode the original values
and not the original rows.

Cor
 
Hi Terry,

Is this the result you want?
See the second one I first have taken the old one to show where it has to be
in the code.

\\\
Dim OriginalRows() As DataRow
'Get them from the Table using select statement
OriginalRows = MyTable.Select(Nothing, Nothing,
DataViewRowState.OriginalRows)
Debug.WriteLine("------- These 'SHOULD' be the originalows(---------")
For Each r In OriginalRows
If Not r("Data") Is Nothing Then
Debug.WriteLine(r("Data"))
End If
Next
Dim dt2 As DataTable = MyTable.Copy
dt2.RejectChanges()
'Get them from the Table using select statement
OriginalRows = dt2.Select(Nothing, Nothing, DataViewRowState.OriginalRows)
Debug.WriteLine("------- These 'Are' the originalows(---------")
For Each r In OriginalRows
If Not r("Data") Is Nothing Then
Debug.WriteLine(r("Data"))
End If
Next
///

Cor
 
First Question.

1.) Has anyone 'actually' tried my code example ?, if so did you get the
same results ?

2.) If yes to 1.), then why does this not work. The select satement should
be able to generate a row set based on my criteria with a set of 'Original
Rows' IE Before modification took place.

3.) If What I have said in 2.) Is incorrect, why ?

Back Later



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
Back
Top