?
Solved

Display Hierarchy of ADO.NET data in WinForms DataGrid

Posted on 2003-03-17
12
Medium Priority
?
584 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

801 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