VB.NET: How to apply a DataView

B

Bill S.

Can somebody please help. I'm coming from VB6 and this is my first VB.NET
applciation.
Below is my code that fills two tables contained in a DataSet, creates a
relation between them, and finally, uses them as DataSources for two grid
controls on the form. This code works prefectly fine. Now, my question is
how do I apply a DataView in order to sort and filter my data without
destroying my relation? Ever snippet of code I've looked at involves setting
up a DataView and then setting it to the DataSource of my DataGrid. This,
however, destroys my relation.

Thanks for any help.


m_Ds = New DataSet
m_Da1 = New OleDbDataAdapter("SELECT * FROM Groups", Conn)
m_Da2 = New OleDbDataAdapter("SELECT * FROM Clients", Conn)

m_Da1.FillSchema(m_Ds, SchemaType.Source, "Groups")
m_DgGroupsCount = m_Da1.Fill(m_Ds, "Groups")
m_Da2.FillSchema(m_Ds, SchemaType.Source, "Clients")
m_Da2.Fill(m_Ds, "Clients")

'Create a relation and add it to the collection.
Dim rel1 As New DataRelation("LinkToClients", _
m_Ds.Tables("Groups").Columns("GroupID"), _
m_Ds.Tables("Clients").Columns("GroupID"))
m_Ds.Relations.Add(rel1)

'Master/detail setup.
Me.dgGroups.DataSource = m_Ds
Me.dgGroups.DataMember = "Groups"
Me.dgGroups.AllowNavigation = False
Me.dgClients.DataSource = m_Ds
Me.dgClients.DataMember = "Groups.LinkToClients"
 
G

Guest

You may have to grab the process and do some hand coding. Underneath the
hood, when you write code like:

DatGrid1.DataSource = MyDataSet.Tables("Groups")

it is actually compiled as:

DataGrid1.DataSource = MyDataSet.Tables("Groups").DefaultView

If you specifically set a view, you will have to filter both tables on the
same key and maintain the relationship in your code. The only other option,
which will work with the relationship in the tables, is to requery the
database for filtering. If you are taking a trip to the server already, and
not caching the DataSet, there is nothing wrong with requerying; if you are
caching, or working disconnected (client side), you are better to filter and
write code to control the relationship in the filtered DataView(s).


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************
 
G

Guest

Dim cm As CurrencyManager = CType(Me.BindingContext(DS, "TableName"), CurrencyManager)
Dim dv As DataView = CType(cm.List, DataView)
You can no use dv as a dataview for that table within the dataset.


Can somebody please help. I'm coming from VB6 and this is my first VB.NET
applciation.
Below is my code that fills two tables contained in a DataSet, creates a
relation between them, and finally, uses them as DataSources for two grid
controls on the form. This code works prefectly fine. Now, my question is
how do I apply a DataView in order to sort and filter my data without
destroying my relation? Ever snippet of code I've looked at involves setting
up a DataView and then setting it to the DataSource of my DataGrid. This,
however, destroys my relation.

Thanks for any help.


m_Ds = New DataSet
m_Da1 = New OleDbDataAdapter("SELECT * FROM Groups", Conn)
m_Da2 = New OleDbDataAdapter("SELECT * FROM Clients", Conn)

m_Da1.FillSchema(m_Ds, SchemaType.Source, "Groups")
m_DgGroupsCount = m_Da1.Fill(m_Ds, "Groups")
m_Da2.FillSchema(m_Ds, SchemaType.Source, "Clients")
m_Da2.Fill(m_Ds, "Clients")

'Create a relation and add it to the collection.
Dim rel1 As New DataRelation("LinkToClients", _
m_Ds.Tables("Groups").Columns("GroupID"), _
m_Ds.Tables("Clients").Columns("GroupID"))
m_Ds.Relations.Add(rel1)

'Master/detail setup.
Me.dgGroups.DataSource = m_Ds
Me.dgGroups.DataMember = "Groups"
Me.dgGroups.AllowNavigation = False
Me.dgClients.DataSource = m_Ds
Me.dgClients.DataMember = "Groups.LinkToClients"

User submitted from AEWNET (http://www.aewnet.com/)
 
Top