Re: Data Relations with DataTable Class
- From: "RobinS" <RobinS@xxxxxxxxxxxxxxx>
- Date: Sat, 9 Dec 2006 00:05:11 -0800
This may help. You might want to check out the BindingSource
component; using this for your databinding will more
easily enable the parent/child binding. Here's an example;
hopefully there will be something in here that you can use.
This example is for a form with two grids on it, and a bunch
of textboxes. One grid is the parent, the other is the
children, and the textboxes are bound to the parent.
---------------------------
I have a dataset that has two tables: Customers and Orders.
(This runs against Northwind, so if you have that
installed, you can try this out.)
I set up a strongly-typed dataset using the Data Set
Designer with both of those tables in it, and there is
a field called CustomerID in the Orders table that links
to the CustomerID table in Customers. So Orders have a
Customer as a parent. The data relation between
the customer and the orders is defined as FK_Orders_Customers.
(You can set up your dataset however you like.)
This is in Form_Load:
'First, populate the dataset.
Dim nwData As CustomersDataSet = CustomersDataSet.GetCustomers()
'Set the data source for the binding source to the dataset.
CustomersBindingSource.DataSource = nwData
'Add this so it knows which table to use .
CustomersBindingSource.DataMember = "Customers"
'Set the data source for the grid to the customers binding source.
CustomersDataGridView.DataSource = CustomersBindingSource
'Add the binding for the child; set its binding source to the same
' one as the parent.
Customers_OrdersBindingSource.DataSource = CustomersBindingSource
'Set the datamember to the foreign key joining the two tables.
' You can see this on your CustomersDataSet diagram.
Customers_OrdersBindingSource.DataMember = "FK_Orders_Customers"
'Set the data source on the child grid to points at its
' binding source.
OrdersDataGridView.DataSource = Customers_OrdersBindingSource
AddTextBoxDataBindings()
AddComboBoxDataBindings()
Here are the routines that bind the textboxes and combo box
in case you want them. These only bind to the Customers table,
but you can easily bind textboxes to the children by binding
to the Customers_OrdersBindingSource instead of the
CustomersBindingSource.
Private Sub AddTextBoxDataBindings()
'Bind each text box to the appropriate field in the
' CustomersBindingSource
CustomerIDTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CustomerID")
ContactNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "ContactName")
CompanyNameTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "CompanyName")
ContactPhoneTextBox.DataBindings.Add("Text", _
CustomersBindingSource, "Phone")
End Sub
Private Sub AddComboBoxDataBindings()
ContactsComboBox.DataSource = CustomersBindingSource
ContactsComboBox.DisplayMember = "ContactName"
ContactsComboBox.ValueMember = "CustomerID"
End Sub
This information came out of Brian Noyes's book on Data Binding.
---------------------------
As for displaying info from specific rows in textboxes
5 through 8, you could attach a handler to the positionChanged
event on the binding source of the parents. In the event, you
could check for multiple records in the children (probably
using the binding source, maybe BindingSource.Count?)
and populate the textboxes accordingly.
Hope this helps.
Robin S.
-----------------------------
"Anil Gupte" <anil-list@xxxxxxxxxxx> wrote in message
news:e%23STCD2GHHA.3780@xxxxxxxxxxxxxxxxxxxxxxx
Cor:
Thanx for your input. Unfrtunately, that example uses an access database,
but I am using a DataTable class, which I think will be more secure, since
people will not be able to get their hands on the data. The problem I am
having is this. If you see the code below:
DatasetInit.Tables.Add(tblSliceInfo) ' tblSliceInfo= "SliceInfo"
DatasetInit.Tables.Add(tblSliceRatings) ' tblSliceRatings = "SliceRatings"
Dim colSliceInfo As DataColumn
Dim colSliceRatings As DataColumn
colSliceInfo = tblSliceInfo.Columns("SliceID")
colSliceRatings = tblSliceRatings.Columns("SliceRatingsID")
Dim SliceIDRelation As DataRelation
SliceIDRelation = New DataRelation("SliceIDRelation", colSliceInfo,
colSliceRatings)
DatasetInit.Relations.Add(SliceIDRelation)
DataGrid1.SetDataBinding(DatasetInit, "SliceInfo")
DataGrid2.SetDataBinding(DatasetInit, "SliceInfo.SliceIdRelation")
TextBox1.DataBindings.Add("Text", DatasetInit, "SliceInfo.SliceID")
TextBox2.DataBindings.Add("Text", DatasetInit, "SliceInfo.SliceTitle")
TextBox3.DataBindings.Add("Text", DatasetInit, "SliceInfo.SliceStartAbs")
TextBox4.DataBindings.Add("Text", DatasetInit, "SliceInfo.L3Filename")
TextBox5.DataBindings.Add("Text", DatasetInit, "SliceRatings.RatingName")
TextBox6.DataBindings.Add("Text", DatasetInit, "SliceRatings.RatingValue")
TextBox7.DataBindings.Add("Text", DatasetInit, "SliceRatings.RatingName")
TextBox8.DataBindings.Add("Text", DatasetInit, "SliceRatings.RatingValue")
Everything works great in the Datagrids. Datagrid1 shows the rows in the
master/parent table Datagrid2 shows the child rows in the child table. In
the text boxes, Textbox1 thru 4 are fine and as I step through the rows in
Datagrid1, their value changes. Unfortunately, the values in the Textbox5
thru 8 do not change at all, leading me to believe that they are not
somehow bound. Also, how do I make Textbox7 and 8 have the value from the
second child row (right now it just repeats what is in textbox5 and 6
respectively)
Thanx for any help.
--
Anil Gupte
www.keeninc.net
www.icinema.com
"Cor Ligthert [MVP]" <notmyfirstname@xxxxxxxxx> wrote in message
news:OXWUXT1GHHA.1216@xxxxxxxxxxxxxxxxxxxxxxx
Anil,
I give you only this link, I think that you can than go on.
http://msdn2.microsoft.com/en-us/library/ms233770(vs.80).aspx
The missing link you have are the bad written parts about the
datarowstate on MSDN.
Cor
"Anil Gupte" <anil-list@xxxxxxxxxxx> schreef in bericht
news:Oj9z1BsGHHA.2456@xxxxxxxxxxxxxxxxxxxxxxx
After reading a tutorial and fiddling, I finally got this to work. I
can now put two tables created with a DataTable class into a
DataRelation. Phew! And it works!
Dim tblSliceInfo As New DataTable("SliceInfo")
Dim tblSliceRatings As New DataTable("SliceRatings")
'.... All the adding datacolumns, datarows, etc. goes here..
DatasetInit.Tables.Add(tblSliceInfo)
DatasetInit.Tables.Add(tblSliceRatings)
Dim colSliceInfo As DataColumn
Dim colSliceRatings As DataColumn
colSliceInfo = tblSliceInfo.Columns("SliceID")
colSliceRatings = tblSliceRatings.Columns("SliceRatingsID")
Dim SliceIDRelation As DataRelation
SliceIDRelation = New DataRelation("SliceIDRelation", colSliceInfo,
colSliceRatings)
DatasetInit.Relations.Add(SliceIDRelation)
Anyway, what I want to do now is that instead of using those tables as
above, I want to use a SQL statement so that I can select the correct
records from the child table. How do I do this? I am planning to bind
the values in the fields to textboxes and then allow editing of those.
Please give me a clue how to go about it.
Thanx,
--
Anil Gupte
www.keeninc.net
www.icinema.com
.
- Follow-Ups:
- Re: Data Relations with DataTable Class
- From: Anil Gupte
- Re: Data Relations with DataTable Class
- References:
- Data Relations with DataTable Class
- From: Anil Gupte
- Re: Data Relations with DataTable Class
- From: Cor Ligthert [MVP]
- Re: Data Relations with DataTable Class
- From: Anil Gupte
- Data Relations with DataTable Class
- Prev by Date: Need some help...
- Next by Date: Re: destroying/releasing objects from memory?
- Previous by thread: Re: Data Relations with DataTable Class
- Next by thread: Re: Data Relations with DataTable Class
- Index(es):
Relevant Pages
|