Question DataRowView Question

TBossAZ

Active member
Joined
Mar 31, 2009
Messages
43
Programming Experience
5-10
I have a simple question (but the answer could be complex :)).

Is is at all possible to retreive data from DataRowView, or is that not what it is for?

What I have done is create a dataview from a datatable:
oleDataAdapter.Fill(dsDocTypes)
dvDocTypes = dsDocTypes.DefaultView

Then I used the Sort, Find and Findrows functions of the dataview to return the data I want in a datarowview

How Do I go about retreiving my data from the datarowview?

Any comments or ideas would be appreciated
 
DefaultView returns a DataView, this view is for example used when the data is bound to a display control. The row items of the view are DataRowView objects, which is a view of a DataRow, the Item property expose the row fields, and it is also possible to get the underlying DataRow from the Row property. Have a look in help for the mentioned classes.
 
What JohnH says is true but, in most cases, it's not necessary to get the underlying DataRow. To answer your question, yes you can get the data from the DataRowView. Not only that, you can set it as well. How do you think that changes get made to the data when you bind a DataTable to a control?

Let me explain in some detail how the DataTable, DataRow, DataView and DataRowView work together. When you bind data to a control like a DataGridView or ComboBox, the object must implement the IList or IListSource interface. The IList interface defines the basic functionality of a list of objects, i.e. use in a For Each loop, Count property, Item property. The IListSource interface defines the functionality of an object that is not itself a list but can provide access to one.

Now, notice that the DataTable class has no Count or Item property? It is not itself a list. If you want to get at the data you normally access its Rows property to get the DataRows. The DataTable is an IListSource object, i.e. it implements the IListSource interface. That means that it has a GetList method that returns an IList object. That IList object is the same DataView that gets returned by its DefaultView.

So, when you bind a DataTable to a DataGridView it is NOT the data from the DataRows that you see. It's actually the data from the DataRowViews in its DefaultView that you see. That is how you are able to sort the grid by clicking a column header. A DataTable can't be sorted but a DataView can. Any edits you make to the grid are actually pushed to the underlying DataRowView objects.

Another reason for this is that they support transactional editing. That means that you can make changes to the data in a row and then press Escape to cancel those changes. The data in the DataRowView was changed but it is changed back again without ever being pushed to the underlying DataRow.

So, to get back to your original question, once you have the DataView then you can get and set the data like this:
VB.NET:
Dim row As DataRowView = dvDocTypes(0) 'Get the first row.

row("Name") = someName 'Set the Name column.
The DataRowView can be treated in exactly the same way as the corresponding DataRow in most cases.
 
Then I used the Sort, Find and Findrows functions of the dataview to return the data I want in a datarowview

Be careful that youre not having the client perform operations that the database server should perform. e.g. it wouldnt make sense for me to download a million transactions rows across the network from our accounts server, then use a local filter to find only transactions from a certain card; you'd have the database do it itself, because it's faster and requires less network traffic
 
I want to thank everyone for their help. After reading everyone's responses, I realized all I had to do was the following:

VB.NET:
dvRows(0).Row("COLUMN_NAME")

This allowed me to access the specific peice of data I needed.

I also undestand the point that was made about returning an entire DataTable and then just needing one peice of data. Im my context hovever the table is loaded in a user control that also displays a pick list of items from the table. Once a user selects an item, the table is used to find all of the related data. Make sense?

Basically I load the table just once (87 rows of data, 3 columns), instead of the pick list loading the data over and over again.

As always, I'm open to everyone else's opinions, as I continue to learn.
 
Once a user selects an item, the table is used to find all of the related data.

You should also investigate DataRelations, then.. They are a dynamic filter, returning only rows whose key value is related to a parent table

Two datatables have a datarelation
The parent datatable binds to a parent bindingsource
The parent bindingsource exposes the datarelation
Whichever row the parent BS has as current, the datarelation only exposes child rows from the child tabl;e that are related to that row

See DW2 in my sig, section Displaying Related Data
 
Back
Top