Re: parent column in child row

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Mike McIntyre [MVP] (mikemc_at_dotnetshowandtell.com)
Date: 06/03/04


Date: Thu, 3 Jun 2004 12:27:23 -0700

Frank,

You can do what you want by adding a calculated column to the child
DataTable that contains and expression that references a parent row column.

Learn more at the links below:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatadatacolumnclassexpressiontopic.asp

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconaddingdatacolumnstodatatable.asp

Below is some code for a button click handler that demonstrates how to
accomplish your goal. If you create a Windows Forms project, add a Button1
and a DataGrid1, add this code below to the form code, and make sure the
SqlConnection is valid for your Sql server you can see this code in action.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' Open a database connection.

Dim strConnection As String = _

"Data Source=localhost;Initial Catalog=Northwind;" _

& "Integrated Security=True"

Dim cn As SqlConnection = New SqlConnection(strConnection)

cn.Open()

' Set up a data adapter object.

Dim strSql As String = "SELECT * FROM Customers" _

& " WHERE City = 'Buenos Aires' AND Country = 'Argentina'"

Dim da As SqlDataAdapter = New SqlDataAdapter(strSql, cn)

' Load a data set.

Dim ds As DataSet = New DataSet()

da.Fill(ds, "Customers")

' Set up a new data adapter object.

strSql = "SELECT Orders.*" _

& " FROM Customers, Orders" _

& " WHERE (Customers.CustomerID = Orders.CustomerID)" _

& " AND (Customers.City = 'Buenos Aires')" _

& " AND (Customers.Country = 'Argentina')"

da = New SqlDataAdapter(strSql, cn)

' Load the data set.

da.Fill(ds, "Orders")

' Close the database connection.

cn.Close()

' Create a relation.

ds.Relations.Add("CustomerOrders", _

ds.Tables("Customers").Columns("CustomerID"), _

ds.Tables("Orders").Columns("CustomerID"))

' Create a child row calculated column that shows

' a datacolumn from the child row's parent.

Dim companyNameColumn As New DataColumn("CompanyName",
System.Type.GetType("System.String"))

companyNameColumn.Expression = "Parent.CompanyName"

ds.Tables("Orders").Columns.Add(companyNameColumn)

Me.DataGrid1.DataSource = ds

End Sub

"Frank" <frank@frank.com> wrote in message
news:c9nr3q$sel$1@news3.tilbu1.nb.home.nl...
> Is this question so difficult or is it impossible what I want? I can't be
> the first one to bump into this problem. Plse tell me if it is not
possible
> then I can look into another direction.
> Frank
>
> "Frank" <frank@frank.com> wrote in message
> news:c9i8nv$7vc$1@news3.tilbu1.nb.home.nl...
> > Hello,
> > I have a dataset with 2 tables and a relation (parent - child). I linked
> > that ds to a datagrid. Shows everything fine (very little coding for
such
> > functionality!).
> > But.. in the child row I want to show a column of the parent. Example:
> > orderColumn shows customerId and I want the customerName which is
located
> in
> > the parent.
> > How do I do that?
> > Thanks in advance
> > Frank
> >
> >
>
>



Relevant Pages

  • Re: parent column in child row
    ... You can do what you want by adding a calculated column to the child ... DataTable that contains and expression that references a parent row column. ... Dim cn As SqlConnection = New SqlConnection ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Cascade fill for tables with FK relationships
    ... We populate the parent table into a Dataset, limiting it to a set of search ... way to populate the child Datasets once we have the parent Dataset. ... Dim dtMaster as DataTable ... Use an "IN" clause that does the parent table query with all the ...
    (microsoft.public.dotnet.framework.adonet)
  • expression column
    ... column to the child using 'Parent' syntax, I will get an exception whenever ... Dim dtParent As New DataTable ... This line throws out an exception ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: expression column
    ... Did you try without expression column? ... handle this situation just getting parent row, related to the child and then ... Suppose I have two tables, parent and child, ... > Dim dtParent As New DataTable ...
    (microsoft.public.dotnet.framework.adonet)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)