?
Solved

Display Hierarchy of ADO.NET data in WinForms DataGrid

Posted on 2003-03-17
12
Medium Priority
?
587 Views
Last Modified: 2013-11-19
[VB.NET]

Hi.  I have a lovely XML schema which already has DataRelations defined between master and detail tables.  (Think the traditional "Orders" and "OrderDetails" tables).  I also have a Windows form containing a DataGrid control and a combobox control that allows my user to select any table from the XML schema.

Everything works fine except for the DataGrid automagically honoring my DataRelations.  I'm sure there's something more I need to do, but I'm not sure how to go about it.

I'm looking for some VB code that demonstrates how to do this...basically, given a DataTable object, (1) determine if it has ChildRelations, and (2) if so, display those relationships in the DataGrid (ideally, each "master" record would be expandable, or something to that effect).

I've been searching around online, but haven't seen a good example of this in VB.NET so far.

Thanks a lot.
0
Comment
Question by:bryker
  • 8
  • 4
12 Comments
 
LVL 28

Accepted Solution

by:
iboutchkine earned 800 total points
ID: 8153768
You relationships you are defining in datateble. Datagrid is automatically shows hierarchy.
If you want to expand dg throug code
Protected Sub TextExpanded(myGrid As DataGrid)
    ' Get the DataTable of the grid
    Dim myTable As DataTable
    ' Assuming the grid is bound to a DataTable
    myTable = CType(myGrid.DataSource, DataTable)
    Dim i As Integer
    For i = 0 To myTable.Rows.Count - 1
        If myGrid.IsExpanded(i) Then
            Console.WriteLine(("Row " & i & " was expanded"))
        End If
    Next i

End Sub 'TextExpanded

To collapse dg:
dg1.Collapse(-1)

Another thing:
If you dont want to see '+' and immediately go to the related table view , you can implement datagrid.DataMemeber

 for example

dg.DataSource = ds
dg.DataMember = "MyTable"


If you don't want to show related tables at all then:

To get rid of the "+" you can set the AllowNavigation property to False.
Your grid will then only show the parent table and not let rows be expanded
to show children.


0
 

Author Comment

by:bryker
ID: 8153788
Well, as it turns out, my DataRelations as defined in the XML schema aren't even *honored* in the DataTables at run-time.  Neither the "master" nor the "detail" DataTables have any Parent- or ChildRelations objects in their collections.

Surely MS doesn't ignore these, and force us to re-create them via hard-coding at runtime?...

0
 

Author Comment

by:bryker
ID: 8153831
Sheesh, it doesn't even honor my PrimaryKeys on these tables...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 28

Expert Comment

by:iboutchkine
ID: 8153950
These relationships are honored by datatable. Probably something wrong with your XML file or schema.

Try to create schema in IDE designer, add relationships there and you will see that everything works. Let mw know if you need help in creating XML in IDE
0
 

Author Comment

by:bryker
ID: 8153955
Sheesh, it doesn't even honor my PrimaryKeys on these tables...
0
 

Author Comment

by:bryker
ID: 8153994
Would you mind if I copied in the XML for the offending master-detail relationship?

Here's the schema (at least, the relevant parts).  I created it through the IDE.

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="ThisInfo" targetNamespace="http://www.mpsisys.com/This/ThisInfo.xsd" elementFormDefault="qualified" xmlns="http://www.mpsisys.com/This/ThisInfo.xsd" xmlns:mstns="http://www.mpsisys.com/This/ThisInfo.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" version="1.0">
     <xs:complexType name="MarketType">
          <xs:sequence>
               <xs:element name="Key" type="xs:string" />
               <xs:element name="GroupKey" type="xs:string" />
               <xs:element name="DataModelKey" type="xs:string" />
               <xs:element name="RexID" type="xs:integer" />
               <xs:element name="RexName" type="xs:string" />
               <xs:element name="Order" type="xs:integer" />
               <xs:element name="Caption" type="xs:string" />
               <xs:element name="Type" type="xs:integer" />
               <xs:element name="State" type="xs:integer" />
               <xs:element name="Attributes" type="xs:long" />
               <xs:element name="ExtendedProperties" type="xs:string" />
               <xs:element name="CountryCode" type="xs:integer" />
               <xs:element name="CountryName" type="xs:string" />
               <xs:element name="CountryDivName" type="xs:string" />
               <xs:element name="SurveyDate" type="xs:string" />
          </xs:sequence>
     </xs:complexType>
     <xs:complexType name="MarketDetailType">
          <xs:sequence>
               <xs:element name="Key" type="xs:string" />
               <xs:element name="MarketKey" type="xs:string" />
               <xs:element name="DataModelKey" type="xs:string" />
               <xs:element name="Caption" type="xs:string" />
               <xs:element name="Order" type="xs:integer" />
               <xs:element name="Name" type="xs:string" />
               <xs:element name="Value" type="xs:string" />
               <xs:element name="OriginalValue" type="xs:string" />
               <xs:element name="ChangeInValue" type="xs:string" />
               <xs:element name="DataType" type="xs:string" />
               <xs:element name="Type" type="xs:integer" />
               <xs:element name="State" type="xs:integer" />
               <xs:element name="Attributes" type="xs:long" />
               <xs:element name="ExtendedProperties" type="xs:string" />
          </xs:sequence>
     </xs:complexType>
     <xs:element name="Markets" type="MarketType">
          <xs:unique name="MarketsKey" msdata:PrimaryKey="true">
               <xs:selector xpath="." />
               <xs:field xpath="mstns:Key" />
               <xs:field xpath="mstns:DataModelKey" />
          </xs:unique>
     </xs:element>
     <xs:element name="MarketDetails" type="MarketDetailType">
          <xs:unique name="MarketDetailsKey" msdata:PrimaryKey="true">
               <xs:selector xpath="." />
               <xs:field xpath="mstns:Key" />
               <xs:field xpath="mstns:MarketKey" />
               <xs:field xpath="mstns:DataModelKey" />
          </xs:unique>
          <xs:keyref name="MarketsToMarketDetails" refer="MarketsKey">
               <xs:selector xpath="." />
               <xs:field xpath="mstns:MarketKey" />
               <xs:field xpath="mstns:DataModelKey" />
          </xs:keyref>
     </xs:element>
</xs:schema>
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8154103
As I can see you have 2 different tables MarketType and MarketDetailType. They are not related

To have them related it must be something like
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema1" targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
     <xs:complexType name="MarketType">
          <xs:complexType>
               <xs:sequence>
                    <xs:element name="Key" type="xs:string" />
                    ...other fields
                    <xs:complexType name="MarketDetailType">
                         <xs:complexType>
                              <xs:sequence>
                                    <xs:element name="Key" type="xs:string" />
                                   ...other fields
                              </xs:sequence>
                         </xs:complexType>
                    </xs:element>
               </xs:sequence>
          </xs:complexType>
              ...key info
     </xs:element>
</xs:schema>
0
 

Author Comment

by:bryker
ID: 8154146
You mean relate the ComplexTypes, and not the tables themselves?!

I'm looking through the .NET IDE right now, at my various tables and types, and the IDE doesn't offer a way to relate ComplexTypes (by right-clicking; which it does if you're on a table).
0
 
LVL 28

Expert Comment

by:iboutchkine
ID: 8154194
Drag Complex Type to your table. That will make them hierarchical
0
 

Author Comment

by:bryker
ID: 8154894
I tried dragging my MarketType to my Markets table. This is disallowed in the IDE (shows the slashed-circle symbol).
0
 

Author Comment

by:bryker
ID: 8155475
Also:

>As I can see you have 2 different tables MarketType and MarketDetailType. They are not related

These aren't tables, but are ComplexTypes.  The 2 tables in my XML snippet each are typed using these types.

I really do not think that you can either use PrimaryKeys in ComplexTypes, nor can you define DataRelations for them.
0
 

Author Comment

by:bryker
ID: 8164183
I finally found some things wrong with my schema.  Chiefly, it needed an <element> tag which wrapped up everything in the schema, plus an attribute on this <element> named "IsDataSet", set to True.

After this change, my schema was finally a "true" DataSet and everything showed up--keys, constraints, etc.

Your original answer was right on and helpful.  Thanks.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question