473,386 Members | 1,694 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,386 software developers and data experts.

Binding a child table to a datagrid control and setting the parent's ID

I have a dataset containing a parent table related to a child table. The
child table contains an ID field (which is configured as autonumber in the
datatable), the ID of the parent, plus some addition fields.

I'm able to get the datagrid to be properly populate with only the child
records relating to the specified parent by setting the
..DefaultView.RowFilter property.

Currently, to properly add a new child record, I need to manually enter in
the value of the parent's ID. As I'm planning on hiding this column (since
the end user won't know or care about this value), I'm looking for a way of
getting the datagrid to assign the parent's ID automatically. I'm pretty
sure there's a way, but I haven't been able to find any documentation that
explains how to do it. Nor do I see any obvious events in the datagrid
control to tap into. I've tried checking my .Net programming books, and
searching Google, MSDN, etc. to no avail. Can someone please point me to
the appropriate documentation/tutorial/example/etc.?

TIA,

Richard Rosenheim
Nov 21 '05 #1
8 3908
Richard,

I saw our question so often that I decided to make a little sample today,

You can try it as first.

I do not know if it completly answers your question however it should give
it a big go.
(There is as well a method using the rowfilter, however that will maybe look
weird in this sample, so I have to decide if it bring it in this)

Cor

\\\ Needs a form with two datagrids, a button and a label
'The first click on the button shows one datagrid
'The second click shows the same info with two datagrid
'To make it nice a lot of other code is needed by instance
datagridtablestyles
'and columnstyles
Private Ds As New DataSet
Private dtCountries As DataTable
Private dtVBLTRegulars As DataTable
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateTables()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Static pushed As Integer = 1
Select Case pushed
Case 1
Label1.Text = "Relation One Datagrid"
Dim drel As New DataRelation _
("Regulars", Ds.Tables("Countries").Columns("Country"), _
Ds.Tables("Persons").Columns("Country"))
Ds.Relations.Add(drel)
DataGrid1.DataSource = dtCountries
DataGrid1.Expand(-1)
Case 2
Ds.Relations.RemoveAt(0)
Label1.Text = "Relation two Datatgrids"
Dim drel As New DataRelation _
("Regulars", Ds.Tables("Countries").Columns("Country"), _
Ds.Tables("Persons").Columns("Country"))
Ds.Relations.Add(drel)
DataGrid1.SetDataBinding(Ds, "Countries")
DataGrid2.SetDataBinding(Ds, "Countries.Regulars")
End Select
pushed += 1
End Sub
'This is only needed to show the sample
Private Sub CreateTables()
dtVBLTRegulars = New DataTable("Persons")
dtVBLTRegulars.Columns.Add("Id")
dtVBLTRegulars.Columns.Add("Name")
dtVBLTRegulars.Columns.Add("Country")
For i As Integer = 0 To 7
Dim dr As DataRow = dtVBLTRegulars.NewRow
dr(0) = i.ToString
dtVBLTRegulars.Rows.Add(dr)
Next
dtVBLTRegulars.Rows(0)(1) = "Herfried K. Wagner"
dtVBLTRegulars.Rows(1)(1) = "Ken Tucker"
dtVBLTRegulars.Rows(2)(1) = "CJ Taylor"
dtVBLTRegulars.Rows(3)(1) = "Jay B Harlow"
dtVBLTRegulars.Rows(4)(1) = "Terry Burns"
dtVBLTRegulars.Rows(5)(1) = "Tom Shelton"
dtVBLTRegulars.Rows(6)(1) = "Cor Ligthert"
dtVBLTRegulars.Rows(0)(2) = "EU"
dtVBLTRegulars.Rows(1)(2) = "US"
dtVBLTRegulars.Rows(2)(2) = "US"
dtVBLTRegulars.Rows(3)(2) = "US"
dtVBLTRegulars.Rows(4)(2) = "EU"
dtVBLTRegulars.Rows(5)(2) = "US"
dtVBLTRegulars.Rows(6)(2) = "EU"
dtCountries = New DataTable("Countries")
dtCountries.Columns.Add("Id")
dtCountries.Columns.Add("Country")
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountries.NewRow
dr(0) = i.ToString
dtCountries.Rows.Add(dr)
Next
dtCountries.Rows(0)(1) = "EU"
dtCountries.Rows(1)(1) = "US"
Ds.Tables.Add(dtVBLTRegulars)
Ds.Tables.Add(dtCountries)
End Sub
///
I hope this helps a little bit?

Cor
Nov 21 '05 #2
Cor,

Thanks for the reply and the code. I saw that you are binding the datagrid
to the listbox. While it should have, it never occurred to me to try
binding the datagrid to the listbox...

But, in trying out the code you provided me, I'm encountering the same
issue. Yes, when I click between "US" and "EU" in the listbox, the datagrid
is updated accordingly. But, when I go to add a new record in the datagrid,
the "Country" field is not being automatically populated, which is what I'm
trying to achieve.

The reasons I'm trying to achieve that is (a) there's really is no reason to
display that column as the user selected the country via the list box and
(b) it will actually be a numeric record key which the user won't know
anyway.

There's a Microsoft chat on using the datagrid control starting in about 45
minutes. I'll let you know if I learn anything.

Thanks again for the assistance,

Richard Rosenheim
"Cor Ligthert" <no************@planet.nl> wrote in message
news:ev*************@TK2MSFTNGP11.phx.gbl...
Richard,

I do it just this way when it is with a listbox or a combobox
\\\a form, a datagrid, a listbox and a textbox
Private ds As New DataSet
Private dtCountries As DataTable
Private dtVBreg As DataTable
Private dv As DataView
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateTables()
dv = New DataView(dtVBreg)
ListBox1.DataSource = ds.Tables("Countries")
ListBox1.DisplayMember = "Country"
ListBox1.ValueMember = "Country"
DataGrid2.DataSource = dv
TextBox1.DataBindings.Add("Text", dv, "Name")
dv.RowFilter = "Country = '" & ListBox1.SelectedValue.ToString & "'" End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
dv.RowFilter = "Country = '" & ListBox1.SelectedValue.ToString & "'" End Sub
Private Sub CreateTables()
dtVBreg = New DataTable("Persons")
dtVBreg.Columns.Add("Name")
dtVBreg.Columns.Add("Country")
For i As Integer = 0 To 7
dtVBreg.Rows.Add(dtVBreg.NewRow)
Next
dtVBreg.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU"}
dtVBreg.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US"}
dtVBreg.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US"}
dtVBreg.Rows(3).ItemArray = New Object() _
{"Jay B Harlow", "US"}
dtVBreg.Rows(4).ItemArray = New Object() _
{"Terry Burns", "EU"}
dtVBreg.Rows(5).ItemArray = New Object() _
{"Tom Shelton", "US"}
dtVBreg.Rows(6).ItemArray = New Object() _
{"Cor Ligthert", "EU"}
dtCountries = New DataTable("Countries")
dtCountries.Columns.Add("Country")
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountries.NewRow
dr(0) = i.ToString
dtCountries.Rows.Add(dr)
Next
dtCountries.Rows(0)(0) = "EU"
dtCountries.Rows(1)(0) = "US"
ds.Tables.Add(dtVBreg)
ds.Tables.Add(dtCountries)
End Sub
//
I hope this helps?

Cor

Nov 21 '05 #3
Cor (and anyone else),

Here's the modified code:

Private ds As New DataSet
Private dtCountries As DataTable
Private dtVBreg As DataTable

Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateTables()
ListBox1.DataSource = ds.Tables("Countries")
ListBox1.DisplayMember = "Country"
ListBox1.ValueMember = "Country"
DataGrid2.DataSource = ds
DataGrid2.DataMember = "Countries.CountryPersons"
TextBox1.DataBindings.Add("Text", "ListBox1", "SelectedItem")
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

CType(Me.BindingContext(ds, "Countries"), CurrencyManager).Position
= ListBox1.SelectedIndex
End Sub

Private Sub CreateTables()
Dim drel As DataRelation

dtVBreg = New DataTable("Persons")
dtVBreg.Columns.Add("Name")
dtVBreg.Columns.Add("Country")
For i As Integer = 0 To 7
dtVBreg.Rows.Add(dtVBreg.NewRow)
Next
dtVBreg.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU"}
dtVBreg.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US"}
dtVBreg.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US"}
dtVBreg.Rows(3).ItemArray = New Object() _
{"Jay B Harlow", "US"}
dtVBreg.Rows(4).ItemArray = New Object() _
{"Terry Burns", "EU"}
dtVBreg.Rows(5).ItemArray = New Object() _
{"Tom Shelton", "US"}
dtVBreg.Rows(6).ItemArray = New Object() _
{"Cor Ligthert", "EU"}
dtCountries = New DataTable("Countries")
dtCountries.Columns.Add("Country")
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountries.NewRow
dr(0) = i.ToString
dtCountries.Rows.Add(dr)
Next
dtCountries.Rows(0)(0) = "EU"
dtCountries.Rows(1)(0) = "US"
ds.Tables.Add(dtVBreg)
ds.Tables.Add(dtCountries)

drel = New DataRelation("CountryPersons", _
dtCountries.Columns(0), _
dtVBreg.Columns(1))
ds.Relations.Add(drel)
End Sub

"Richard L Rosenheim" <ri*****@rlr.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Cor,

Thanks for the reply and the code. I saw that you are binding the datagrid to the listbox. While it should have, it never occurred to me to try
binding the datagrid to the listbox...

But, in trying out the code you provided me, I'm encountering the same
issue. Yes, when I click between "US" and "EU" in the listbox, the datagrid is updated accordingly. But, when I go to add a new record in the datagrid, the "Country" field is not being automatically populated, which is what I'm trying to achieve.

The reasons I'm trying to achieve that is (a) there's really is no reason to display that column as the user selected the country via the list box and
(b) it will actually be a numeric record key which the user won't know
anyway.

There's a Microsoft chat on using the datagrid control starting in about 45 minutes. I'll let you know if I learn anything.

Thanks again for the assistance,

Richard Rosenheim
"Cor Ligthert" <no************@planet.nl> wrote in message
news:ev*************@TK2MSFTNGP11.phx.gbl...
Richard,

I do it just this way when it is with a listbox or a combobox
\\\a form, a datagrid, a listbox and a textbox
Private ds As New DataSet
Private dtCountries As DataTable
Private dtVBreg As DataTable
Private dv As DataView
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
CreateTables()
dv = New DataView(dtVBreg)
ListBox1.DataSource = ds.Tables("Countries")
ListBox1.DisplayMember = "Country"
ListBox1.ValueMember = "Country"
DataGrid2.DataSource = dv
TextBox1.DataBindings.Add("Text", dv, "Name")
dv.RowFilter = "Country = '" & ListBox1.SelectedValue.ToString &

"'"
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
dv.RowFilter = "Country = '" & ListBox1.SelectedValue.ToString &

"'"
End Sub
Private Sub CreateTables()
dtVBreg = New DataTable("Persons")
dtVBreg.Columns.Add("Name")
dtVBreg.Columns.Add("Country")
For i As Integer = 0 To 7
dtVBreg.Rows.Add(dtVBreg.NewRow)
Next
dtVBreg.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU"}
dtVBreg.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US"}
dtVBreg.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US"}
dtVBreg.Rows(3).ItemArray = New Object() _
{"Jay B Harlow", "US"}
dtVBreg.Rows(4).ItemArray = New Object() _
{"Terry Burns", "EU"}
dtVBreg.Rows(5).ItemArray = New Object() _
{"Tom Shelton", "US"}
dtVBreg.Rows(6).ItemArray = New Object() _
{"Cor Ligthert", "EU"}
dtCountries = New DataTable("Countries")
dtCountries.Columns.Add("Country")
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountries.NewRow
dr(0) = i.ToString
dtCountries.Rows.Add(dr)
Next
dtCountries.Rows(0)(0) = "EU"
dtCountries.Rows(1)(0) = "US"
ds.Tables.Add(dtVBreg)
ds.Tables.Add(dtCountries)
End Sub
//
I hope this helps?

Cor


Nov 21 '05 #4
Richard,

It works, however how do you now fix that column insert, that stays the
problem

Cor
Nov 21 '05 #5
I don't understand what you mean. What column insert? Can you please
elaborate?

Also, I noticed that the textbox binding broke in my experimentation. To
get it to display the country name, I commented out the textbox binding
statement in the Form_Load and added the following line to the ListBox1
SelectedIndexChanged event:

TextBox1.Text = ListBox1.SelectedItem("Country").ToString

Richard
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Richard,

It works, however how do you now fix that column insert, that stays the
problem

Cor

Nov 21 '05 #6
Richard,

There is not much difference in this solution and the one I provided.
You still use the selectedindex change to get the defaultview of the
datagrid changed.

It would be great when that was not necessary as what it is with 2
datagrids.

I thought that your question was how can I set the country in this case by
default to the one in the datagrid by the persons. For that I have no other
answer than I gave and I did not see another one, however maybe I missed
something.

Cor

"Richard L Rosenheim" <ri*****@rlr.com>
..
I don't understand what you mean. What column insert? Can you please
elaborate?

Also, I noticed that the textbox binding broke in my experimentation. To
get it to display the country name, I commented out the textbox binding
statement in the Form_Load and added the following line to the ListBox1
SelectedIndexChanged event:

TextBox1.Text = ListBox1.SelectedItem("Country").ToString

Richard
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Richard,

It works, however how do you now fix that column insert, that stays the
problem

Cor


Nov 21 '05 #7
The issue I was trying to resolve was how to populate the parent key in the
child record when the user adds a new child record. Using your example, it
would be to populate the child's Country field with the country that was
currently selected in the listbox. With the two datagrid example, that was
being done automatically. But, when I moved away from using the two
datagrids, that capability disappeared. By using the code JFuentes
provided, that ability was restored.

The importance is that in real life, the Country table will be normalized,
and as such it would have both the Country name, and an ID field (which
would be an autonumber index). This ID field would need to be assigned to
the child record's Country field, and is not a value that the end user would
know. So expecting them to enter such a field would not be practical.

Thanks again for all your help,

Richard Rosenheim
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OW**************@TK2MSFTNGP10.phx.gbl...
Richard,

There is not much difference in this solution and the one I provided.
You still use the selectedindex change to get the defaultview of the
datagrid changed.

It would be great when that was not necessary as what it is with 2
datagrids.

I thought that your question was how can I set the country in this case by
default to the one in the datagrid by the persons. For that I have no other answer than I gave and I did not see another one, however maybe I missed
something.

Cor

"Richard L Rosenheim" <ri*****@rlr.com>
.
I don't understand what you mean. What column insert? Can you please
elaborate?

Also, I noticed that the textbox binding broke in my experimentation. To get it to display the country name, I commented out the textbox binding
statement in the Form_Load and added the following line to the ListBox1
SelectedIndexChanged event:

TextBox1.Text = ListBox1.SelectedItem("Country").ToString

Richard
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Richard,

It works, however how do you now fix that column insert, that stays the
problem

Cor



Nov 21 '05 #8
Richard,

You are right I never use that method with the * anymore because I cannot
delete with it, so I did not expect that, therefore I have learned again
something.

Cor
Nov 21 '05 #9

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

Similar topics

0
by: Mark Tait | last post by:
Hi - I think I'm getting very tied up here. Using Web Matrix, and have posted in the asp.net forum, with no help unfortunately. I have a table, which has a parent/child relationship with itself....
0
by: Andrew Chen | last post by:
I'm trying to add/delete a row to the child table via the navigating link from the DataGrid control. If the binding parent table has more than one link how do I get the name of the child table...
1
by: muesliflakes | last post by:
I have a Data Set with parent / child related tables and wish to show the parent rows in one data grid and the child rows in another data grid. The problem is that the parent datagrid has the...
0
by: lanky_tx | last post by:
Hello, I am trying to hide a column in a child table within a windows form DataGrid control. I am populating the Datagrid datasource with an dataset which is populated with hierarchical XML...
3
by: Bob Day | last post by:
Using VS 2003, VB.NET, SQL MSDE.. I am stumped. I have a datagrid with a parent/child relationship between two tables. If I add 200 rows from code in the same win form to the child table with...
11
by: Kay | last post by:
Hi all, I have populated a dataset with several tables, say - Roster, Agent and few more . The dataset is used to populated a listview with some shift info.. I think I'm sucessfully created a...
0
by: latin & geek via DotNetMonster.com | last post by:
hi! ok, im working on a database application. ive successfully managed to establish a relationship between two tables and display them on a datagrid, edit and add new records to them. now i...
2
by: Bob | last post by:
I got three related datagrid views one parent and two children of the same. The two child tables contain many thousands of records and some of the contents are bitmap files in a sql server...
1
by: renuami | last post by:
Hi I am receiving Object reference not set to an instance of an Object when binding Child dataGrid in the ItemCommandEvent of Parent dataGrid. Can someone please guide me what is wrong in the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.