How to connect a DataView to an access database?

B

Brad Rogers

Im trying to understand DataView, to make a couple of lists of sorted data.

The 3 textbooks I have on .net dont have dataview in the index even.

Ive started by doing a database wizard. it finds the .mdb file and formats
the schema and genetated a program, I put a simple grid view in the form1
and pointed its source at the dataset.

But now I have to use DataView. Ive tried making a DataTable, and assigning
it to the dataset which only has the one table. I hook the dataview to a
second gridView. It doesnt complain, runs fine, but there is no data in the
gridView. I wired the gridView to the DataTable? still no data there.

Im searching the docs on how to use the DataView, have an example, but the
example builds up the table from scratch; I want to fill it with 2 specific
columns from the one table in the .mdb database.

Nothing Ive done gets the data into the 2nd gridView. I can manually add
columns to it, but I want the data from 2 specific columns. Seems easy.

Any suggestions appreciated.
 
C

Cor Ligthert [MVP]

Brad,

There are plenty of methods to set a dataview to the datatable.

By default the datatable has inbuild a property DefaultView. This is from
the type Dataview.

Can you explain us a little bit more how you try to create your dataview.

Cor
 
B

Brad Rogers

Hi Cor,

Thanks, the goal is to just display 2 columns from one table that has 10?
columns.

I must use the DataView somehow to do it.

I was able to cast/create a Data Table from my access.mdb database dataset.

So there is the DataTable. It has ALL the columns.

I have a 2nd GridView, created a DataView called thisview and said
thisview.RowStateFilter = DataViewRowState.Changed

so when I change an item in the first grid, it pops up on that grid.
cool.

Ive made a DataView and send the contents to a GridView, and played with
some filtering. But I want ONLY 2 columns, and in the columns want to maybe
change text to BOLD based on sorting criteria.

So now I want a GridView that has 2 columns, and those must be from 2
columns in the main DataTable.

Do I have to create a new DataTable? manually enter column names and ?

isnt there a way to say

dim DT as new data table
DT.Columns = (MyTable, ("item", "description") ?
DT.fill

But that doesnt make sense.

Why cant I say
dim DV as new DataView
DV.columns.add(MyTable, (0), (5)) 'adds only columns indexed 0, 5

Me.DataGrid2.datasource = DV

and then it works?? I try variations of this, and have been trying for over
a week, I wont give up until it works.

thanks again
 
C

Cor Ligthert [MVP]

Brad,

You cannot use the dataview to select columns.

However to give you a further answer it has to be clear what kind of grid
you are using because I become confused about that in your answer.

A GridView for ASPNET
A DataGrid WindowsForm
A DataGridView
A DataGrid for ASPNET

I assume it is a GridView for ASPNet with which I have no expirience yet.

Cor
 
B

Brad Rogers

Cor,

I was wondering about columns, I thought there was only 1 grid view control,
the one found in winforms in the designer view. It doesnt matter how its
displayed.

The important part here is the DataView itself

Its not in my textbooks so I cannot easily read up on it. I do have
examples on making the .mdb connection and dataset. So In memory Ive got
the Dataset.

I could make a new table in Access with just those 2 columns? but thats not
allowed.

So now it looks like Ive got to copy across selected columns from the table,
and make a new table.

Lets say I make a new table with 2 columns. Now I want to fill those with
the columns they represent from the Master column. How is this done??

Do I have to use recursiveness or recursion to say master table
column(3).row(x) = secondtable column(0).row(x) ??

There is no smart FILL routine?

Im not familiar with asp.net but hopefully soon.
 
C

Cor Ligthert [MVP]

Brad,

Although it are different classes are all the DataClasses thight connected
to each other. Therefore to help you I have to know what kind of datagrid.
(And if it is a GridView I cannot give you all the information just because
I don't know it yet).

However.

The dataview is the same as the inbuild DefaultView property from a
DataTable. It gives a view on the rows of that table, in fact it has a
rowfilter and a sort property in it.

A benefit is that you can create more views on a table yourself. The
dataview holds as well a lot of information itself. In Net 2.0 you can copy
the rows in the DataView to a new DataTable only containing the filterend
views with the method .ToTable. In Net 1.x you have to go thru the rows from
the dataview for that using than (because a datarow cannot have more
references than one to a table), the method DataRow.ImportRow.

Maybe is this the key for your problems with the DataView. A datarow holds
no information about the columns it contains. The ColumnsInformation is
written in the ColumnCollection from a DataTable. So if the DataView needs
information about a column it uses, than there has to be done internally:
DataRowView->DataTable->ColumnCollection.

Here a very simple sample with a dataview which hide a column on our
website.

http://www.vb-tips.com/default.aspx?ID=76a81eb8-ea2d-48f4-99c3-a3539697edbd

the datatable.defaultview is the same as
dim datatable as new datataview(dt)
where the last is an extra filter.

I hope this gives some ideas

Cor
 
C

Cor Ligthert [MVP]

Brad,

I am glad Sam answered you. I thought it was there, could not find it
anymore and thought I had dreamed it. I found it later, and than I could not
find your question anymore to make a quick correction..

You can select Columns with a dataview in Net 2.0

Problem is that it is not easy to find in MSDN

It is in the 3th overloaded method ToTable

http://msdn2.microsoft.com/en-us/library/h2b6ehaa.aspx

Sorry for giving you not the most optimal answer.
(The given answer works as well)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top