PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms How to show fields from related tables in the same datagrid

Reply

How to show fields from related tables in the same datagrid

 
Thread Tools Rate Thread
Old 11-12-2005, 06:48 PM   #1
=?Utf-8?B?VG9yZQ==?=
Guest
 
Posts: n/a
Default How to show fields from related tables in the same datagrid


I run VS .Net 2003. I have a distributed application with sql server 2000 and
windows front end, written in c#.

I have a number of tables in a dataset with relations between them. In one
single read-only datagrid I would like to display child rows from a child
table as well as one column from the parent table.

Can I fix this with DataViewmanager as source for the datagrid? I need some
example code.

The Datagrid insists on showing both parent and child records in the same
datagrid, with the child rows following the parent row. I dont want this
behaviour. How can I switch it off?

Regards

Tore

  Reply With Quote
Old 12-12-2005, 01:35 AM   #2
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
Default RE: How to show fields from related tables in the same datagrid

Hi Tore,

Thanks for your feedback.

I think the best way to handle master-details view displaying in DataGrid
is displaying the child table in a DataGridComboBoxColumn. However, in
VS.net2003, there is not build-in support to display the combobox in
DataGrid. Fortunately, customizing a combobox column in DataGrid is not
very hard in .Net1.1.

The link below provided a sample implementation, which uses a mdb access
database as the back end source.
"5.9 How can I put a combobox in a column of a datagrid?"
http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q480q

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

  Reply With Quote
Old 15-12-2005, 12:52 PM   #3
=?Utf-8?B?VG9yZQ==?=
Guest
 
Posts: n/a
Default RE: How to show fields from related tables in the same datagrid

Combobox is not what I want. I want the datagrid to have textbox columns, or
columns that look like and behave like textbox columns

I have one parent table and one child table in the same dataset and a
relation between them.

I have one field from the parent table that I would like to display together
with 5 fields from the child table in the same line in a datagrid control (6
visible columns in the datagrid). The datagrid is read-only.

Is there no way to create a view of the two related tables that can be used
as datasource for the datagrid? I thought the dataviewmanager could fix this,
but I have not succeeded to find a description.

Regards

Tore












""Jeffrey Tan[MSFT]"" wrote:

> Hi Tore,
>
> Thanks for your feedback.
>
> I think the best way to handle master-details view displaying in DataGrid
> is displaying the child table in a DataGridComboBoxColumn. However, in
> VS.net2003, there is not build-in support to display the combobox in
> DataGrid. Fortunately, customizing a combobox column in DataGrid is not
> very hard in .Net1.1.
>
> The link below provided a sample implementation, which uses a mdb access
> database as the back end source.
> "5.9 How can I put a combobox in a column of a datagrid?"
> http://64.78.52.104/FAQ/WinForms/FAQ_c44c.asp#q480q
>
> Hope this helps
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
>
>

  Reply With Quote
Old 15-12-2005, 01:42 PM   #4
Thomas S
Guest
 
Posts: n/a
Default Re: How to show fields from related tables in the same datagrid


Tore wrote:
> Is there no way to create a view of the two related tables that can be used
> as datasource for the datagrid? I thought the dataviewmanager could fix this,
> but I have not succeeded to find a description.


Why dont you create a view in the backend database? I guess that would
be the easiest solution.

If not that is possible you could create a bunch of templatecolumns,
bind the grid to the main table and handle the OnItemDataBound event to
populate the templatecolumns with the data from the other tables.

--
Thomas

  Reply With Quote
Old 16-12-2005, 02:16 AM   #5
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
Default RE: How to show fields from related tables in the same datagrid

Hi Tore,

Thanks for your feedback.

Ok, with your further feedback, your requirement appears much clear to me
now.

First, the parent table and child table normally have master-details
relation, so one parent table record may be related with more than one
record in child table. What is your requirement for this scenario? This is
why I recommanded using combobox to display the related child table records
in the same row.

Also, if you still does not want to display combobox, we have another
choice of displaying the parent record multi-times for each related child
record. In this way, the child table is the main binding table, while the
parent table related record may be rundantly displayed. I think this is
what you are after, yes? If I misunderstand you, please feel free to tell
me.

To achieve this effect, we have several ways, I will list some below:
1. Use a JOIN helper class to join the 2 tables together, then bind it to
the DataGrid. The 2 KB below shows the joining at DataTable level and
DataView level:
"HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET"
http://support.microsoft.com/defaul...kb;en-us;326080
"HOW TO: Implement a Custom DataView Class in Visual Basic .NET"
http://support.microsoft.com/defaul...kb;en-us;325682
2. We can extend the binding expression to recognize another table's
binding field at DataGridTextBoxColumn UI level, see the KB below:
"HOW TO: Extend the Windows Form DataGridTextBoxColumn to Display Data From
Other Tables by Using Visual C# .NET"
http://support.microsoft.com/defaul...kb;en-us;319076
3. We can add an expression column in the child table, which display the
corresponding filed in the parent table. Like this:
DataColumn.Expression=Parent([relation name]).[parent field name];

For more information about the expression column syntax, please refer to
"DataColumn.Expression" property in MSDN.

In the above 3 options, I think #3 should be the simplest, which I used
most.

Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

  Reply With Quote
Old 16-12-2005, 09:42 AM   #6
=?Utf-8?B?VG9yZQ==?=
Guest
 
Posts: n/a
Default RE: How to show fields from related tables in the same datagrid

The articles will do the job for me.
Thank you

Tore

""Jeffrey Tan[MSFT]"" wrote:

> Hi Tore,
>
> Thanks for your feedback.
>
> Ok, with your further feedback, your requirement appears much clear to me
> now.
>
> First, the parent table and child table normally have master-details
> relation, so one parent table record may be related with more than one
> record in child table. What is your requirement for this scenario? This is
> why I recommanded using combobox to display the related child table records
> in the same row.
>
> Also, if you still does not want to display combobox, we have another
> choice of displaying the parent record multi-times for each related child
> record. In this way, the child table is the main binding table, while the
> parent table related record may be rundantly displayed. I think this is
> what you are after, yes? If I misunderstand you, please feel free to tell
> me.
>
> To achieve this effect, we have several ways, I will list some below:
> 1. Use a JOIN helper class to join the 2 tables together, then bind it to
> the DataGrid. The 2 KB below shows the joining at DataTable level and
> DataView level:
> "HOW TO: Implement a DataSet JOIN helper class in Visual C# .NET"
> http://support.microsoft.com/defaul...kb;en-us;326080
> "HOW TO: Implement a Custom DataView Class in Visual Basic .NET"
> http://support.microsoft.com/defaul...kb;en-us;325682
> 2. We can extend the binding expression to recognize another table's
> binding field at DataGridTextBoxColumn UI level, see the KB below:
> "HOW TO: Extend the Windows Form DataGridTextBoxColumn to Display Data From
> Other Tables by Using Visual C# .NET"
> http://support.microsoft.com/defaul...kb;en-us;319076
> 3. We can add an expression column in the child table, which display the
> corresponding filed in the parent table. Like this:
> DataColumn.Expression=Parent([relation name]).[parent field name];
>
> For more information about the expression column syntax, please refer to
> "DataColumn.Expression" property in MSDN.
>
> In the above 3 options, I think #3 should be the simplest, which I used
> most.
>
> Hope this helps
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no rights.
>
>

  Reply With Quote
Old 19-12-2005, 02:28 AM   #7
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
Default RE: How to show fields from related tables in the same datagrid

You are welcome

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off