How to show fields from related tables in the same datagrid

G

Guest

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
 
J

Jeffrey Tan[MSFT]

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.
 
G

Guest

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
 
T

Thomas S

Tore said:
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.
 
J

Jeffrey Tan[MSFT]

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/default.aspx?scid=kb;en-us;326080
"HOW TO: Implement a Custom DataView Class in Visual Basic .NET"
http://support.microsoft.com/default.aspx?scid=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/default.aspx?scid=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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top