473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Return DataRow or Row index from DataView

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?
Nov 20 '05 #1
11 27401
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.

Nov 20 '05 #2
Cor
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
///

Nov 20 '05 #3
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.

Nov 20 '05 #4
Cor
Hi Tim,

There is a typo in my sample.
dv.rowfilter = "contactname='Maria Anders'"

Cor
Nov 20 '05 #5
Cor
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
Nov 20 '05 #6
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

"Tim Frawley" <ti*********@fishgame.state.ak.us> wrote in message
news:bf*************************@posting.google.co m... 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?

Nov 20 '05 #7
Cor
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
Nov 20 '05 #8


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #9
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
"Cor" <no*@non.com> wrote in message
news:uD**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #10
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

"Tim Frawley" <ti*********@fishgame.state.ak.us> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...


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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 20 '05 #11
Cor
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.

Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rodusa | last post by:
I am getting an error on line 133. I am trying to update a datarow using the find method, but it keeps throwing the error below: int ItemKey = dv.Find(Item.inv_mast_uid); DataRow dr; if...
4
by: Hein Albrecht | last post by:
'ello How can I get the DataRow of the DataGrid's SelectedItem for the bound DataView's DataTable? Thanx Hein
3
by: Shravan | last post by:
Hi, How can I bind DataRow array to ComboBox. I tried setting DataSource -> DataRow Array DisplayMember -> ColumnName But it was showing "System.Data.DataRow" for every item in the...
0
by: Ryan | last post by:
Is there a way to get a row's DataView index from the underlying DataTable index. Example DataTable (sorted by key - 1st column) DataView (sorted by second column) Row 0 - A, 2 ...
4
by: Maziar Aflatoun | last post by:
Hi, I have a DataRow that I want to bind to a dropdown list. I have 2 columns (TypeName, TypeValue). When do ddlNewAttribute.DataSource = drAttrValues; ddlNewAttribute.DataBind(); How do...
13
by: MadCrazyNewbie | last post by:
Hey Group, I wondered if somebody would be so kind as to help me? Im having trouble with DataViews and DataRows:( I just don`t understand, so I wondered if somebody would be so kind enought...
1
by: Ryan Liu | last post by:
Hi, When my code run to DataRow.Delete() It throws: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: length at System.Array.Copy(Array sourceArray, Int32...
8
by: kimtherkelsen | last post by:
Hi, I do this: this.comboBox.DataSource = myDataSet.Tables.Select("location_key = " + selectedLocationKey); this.comboBox.DisplayMember = "text"; this.comboBox.ValueMember =...
5
by: Rainer Queck | last post by:
Hello NG, what would be the best way to locate a DataRrow in a DataGridView? I have by DataTable.Select a bunch of DataRows from a DataTable (which is the data source to the DataGridView). Now...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.