Return DataRow or Row index from DataView

  • Thread starter Thread starter Tim Frawley
  • Start date Start date
T

Tim Frawley

I need to return a DataRow or the Row Index in a DataSet wherein the
value I am attempting to find is not a primary key.

I have to do this often, more than 200 times when importing a file so
it needs to be fast.

Could I use a Dataview to filter for the value (which is unique) and
return either the DataRow object so I can modify it and put it back
into the DataSet the view is based on or somehow get the RowIndex in
the DataSet that the row corresponds to?
 
Hi

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that you want to find a datarow in a
dataset, and change the datarow.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

Based on my knowledge, you may try the Select method of DataTable.

Me.SqlDataAdapter1.Fill(Me.DataSet11)
'Find the DataRow(s)
Dim dr() As DataRow =
Me.DataSet11.Customers.Select("contactname='Maria Anders'")
Dim odr As DataRow
For Each odr In dr
Console.WriteLine(odr.Item(1).ToString)
Next

'Change the datarow
odr.Item(1) = "sdfsdf"

'Check if the datarow has been changed in the dataset
dr = Me.DataSet11.Customers.Select("contactname='Maria Anders'")
For Each odr In dr
Console.WriteLine(odr.Item(1).ToString)
Next


Please apply my suggestion above and let me know if it helps resolve your
problem.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Tim,

In addition to Peter,
I need to return a DataRow or the Row Index in a DataSet wherein the
value I am attempting to find is not a primary key.
I have to do this often, more than 200 times when importing a file so
it needs to be fast.
Could I use a Dataview to filter for the value (which is unique) and
return either the DataRow object so I can modify it and put it back
into the DataSet the view is based on or somehow get the RowIndex in
the DataSet that the row corresponds to?

The dataview is a view on the datarow. (And therefore much simpler than you
write)
So when you have in the dataview the datarow, you can do with that the same
as Peter showed with the select. (both are a kind of datarow collection,
with the select it is named so)

(Peter made a loopfunction, but it loops one record, there is in my opinion
no need to do that(also not with the select) further is this procedure with
the dataview basicly the same as from Peter)

I hope this helps,

Cor

\\\
Me.SqlDataAdapter1.Fill(Me.DataSet11)
dim dv as new dataview(dataset11.tables(0))
dv.rowfilter = "contactname=Maria Anders"
if dv.count = 1 then
dv(0).item(myitem) = "What I want"
else
messagebox.show("There is no or there are more Maria's)
end if
///
 
Hi Cor,

Yes, your suggestion is another solution, however the DataView is using
the select method of DataTable underlyingly. If we use the DataView, we
need to declare a dataview object, also, I use the loop in case that there
are more than one row returned from select method.

Thank you for sharing the knowledge with us.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,

I think also that the select is better for this.

My answer was a direct answer to the OP, and one of the reasons I did it was
to show to him that your sample was not totally different from his
question.

(More to show him that he did not have to push the datarow back into the
dataset).

Cor
 
Tim,
I need to return a DataRow or the Row Index in a DataSet wherein the
value I am attempting to find is not a primary key. ....
I have to do this often, more than 200 times when importing a file so
it needs to be fast.

Are you searching for a value in the same column each time?

If not then I would probably use the DataTable.Select method that Peter
showed.

If you are I would extend Cors example to define the DataView to be sorted
by the value that you are searching for. I would only create this DataView
once when I created the DataTable itself.

Dim table As New DataTable
Dim view As New DataView(table)
view.Sort = "Field to find value in"


Then each time you need to find the value use DataView.Find or
DataView.FindRows to process the found rows. Depending on if there will be a
single or multiple values found.

Dim value As String

' handle multiple found rows
For Each viewRow As DataRowView In view.FindRows(value)
Dim row As DataRow = viewRow.Row

Next

' handle a single found row
Dim row As DataRow = view(view.Find(value)).Row

David Sceppa's book "Microsoft ADO.NET - Core Reference" from MS Press
covers using a DataView verses using the DataTable.Select methods and when
you would or would not use each.

David's book is both a good tutorial on ADO.NET plus a good desk reference
once you know ADO.NET!

Hope this helps
Jay

Hope this helps
Jay
 
Hi Jay B.

You overlooked this sentence in the message from the OP
"the value (which is unique)"

The best method is obvious for this the datatable.select, which Peter
showed, combined with the procedure as I did show.

I am not often sure of something that it is "the best" but from this I am.

\\\
Dim dr() As DataRow =Ds.tables(0).Select("contactname='Maria Anders'")
if dr.count = 1 then
dr(0).item(myitem) = "What I want"
else
messagebox.show("There is no or there are more Maria's")
end if
///

Cor
 
Wow, Thanks Peter, Cor, and Jay

I have some interesting results from your posts that I would like to
share. First of all, the methods, Table Select and DataView Find
methods both work. What I was not prepared for was the difference in
speed.

When I tested my application using the Table Select method it was taking
approximately 68 seconds to import 200 rows of data from a text file. I
then tested the DataView find method and low and behold, 33 seconds!

I know these times may seem extreme even though for me 33 seconds is an
awesome result. The data is extremely complicated and may contain up to
a hundred values on one row and each value may be inserted into any of
15 tables. We do not know from one value to the next which of those 15
tables will get the value. Thats what you get with Biological data. ;)

The value I am trying to find is in a primary table and it must exist
before the line is allowed to be imported. The value should be unique
but with an Access database anything could happen.

In any case, I want to thank all three of you for helping me out. I now
have a greater understanding of how to find my data and some great
methods for doing the job.

Much appreciated,

Timothy Frawley
 
Cor,
Remember that a DataView with the Sort property set effectively creates an
Index on that column, which means the DataView.Find method should out
perform the DataTable.Select method! What I'm not sure about is will the
DataTable.Select method use the DataView's index if the DataView has been
created?

As I stated, please read Sceppa's book on the merits of both methods.

Hope this helps
Jay
 
Tim,
Out of curiosity.

Does the DataTable.Select method change in speed if you create a DataView
first?

I may have to try some timings of my own later this weekend...

Hope this helps
Jay
 
Hi Jay B.

I intuitive take always the dataview.

We see it if you give samples about adonet with me it is usually
dataview(when I cannot use the find), while it is with you mostly
table.select and table.compute, which belong not to the set I like to use.
(To give you an idea about that, when I wrote RPG it was more assembler than
RPG, RPG I did use only for the logic and the IO).

When I looked at that, the words of Peter looked more than clear for me that
the dataview should use the same functionality as the select, because the
rowfilter is just the same, so why do it on another way. (They both should
iterate through a datarowcolletion to find the equals and with answering
that I forgot all the things I know about tagsorts and things like that)

If the dataview builds tag indexes because of speed, than that can speed up
but also slow down the first time. Can you examine that also when you test
in this weekend?

Again is proven that the "best" method not exists.

:-) a little bit :-( but because I learned again something :-)))

Cor.
 
Back
Top