Re: parent column in child row
From: Mike McIntyre [MVP] (mikemc_at_dotnetshowandtell.com)
Date: 06/03/04
- Next message: William Ryan eMVP: "Re: ADO.NET DataTable or XML Documenr ?"
- Previous message: William Ryan eMVP: "Re: Selecting Data"
- In reply to: Frank: "Re: parent column in child row"
- Next in thread: Frank: "Re: parent column in child row"
- Reply: Frank: "Re: parent column in child row"
- Messages sorted by: [ date ] [ thread ]
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:
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
> >
> >
>
>
- Next message: William Ryan eMVP: "Re: ADO.NET DataTable or XML Documenr ?"
- Previous message: William Ryan eMVP: "Re: Selecting Data"
- In reply to: Frank: "Re: parent column in child row"
- Next in thread: Frank: "Re: parent column in child row"
- Reply: Frank: "Re: parent column in child row"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|