Learn how to a build a cloud-first strategyRegister Now


Tree structure on a datagrid

Posted on 2006-03-20
Medium Priority
Last Modified: 2008-01-09

    I am doing a windows application using vb.net . I am using oracle database, but in order to explain I am using the "Pubs" database.
    When the form is loaded , the  datagrid is filled with the info about the authors . When selecting  a row, the info about the books written by that author should be displayed like branches just under that author . when selecting another row, the already eapanded row must collapse and the branches should appear under the now selected author.
    The rows of books (branches)  should be either displaced a little to the right compared to the  rows of the authors,   or atleast the rows of books and authors should be of different color.

    Can you help me??
Question by:sgs1970
  • 5
  • 4
LVL 96

Expert Comment

by:Bob Learned
ID: 16235579
I don't believe that this would be a simple solution with the DataGrid.  Is there a reason for not using a TreeView?


Author Comment

ID: 16269006
Hi Bob and everyone,
  Infact, I have never used treeview.  If I can use treeview with same way I can have multiple coulumns in a datagrid, probably that is good enough. But very essentialy, I need to have hidden columns- that is the columns that are not shown on the grid , but whose values are used for processing, when a user click on a row.
   Thanking you,
LVL 96

Expert Comment

by:Bob Learned
ID: 16270324
What version of .NET do you have?

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.


Author Comment

ID: 16286802
I am using .Net 2003 . But if it is possible in VS 2005 , please let me know. I will start using that version anyway
LVL 96

Expert Comment

by:Bob Learned
ID: 16342116
The ComponentOne VsFlexGrid supports having a tree structure in a grid:



Author Comment

ID: 16357971

 Hi Bob and everyone,
   Do we get something for free ? Treeview is ok.  But very essentialy, I need to have hidden columns- that is the columns that are not shown on the grid , but whose values are used for processing, when a user click on a row.
LVL 96

Expert Comment

by:Bob Learned
ID: 16360055
In 2003, you apply table styles to the DataGrid with column width set to 0.  For 2005, you set the column visible property a little differently.


Author Comment

ID: 16369080

 Hi Bob,
   The real issue is ,Clicking on an author, the info about the book from the author shouls be displayed  .  But very essentialy, I need to have hidden columns- that is the columns that are not shown , but whose values are used for processing, when a user click on a row.  It can be treeview, datagrid or any other control in any version of .net  or out of the box but with no additional cost.

LVL 96

Accepted Solution

Bob Learned earned 1200 total points
ID: 16539404
With the 2003 DataGrid, this is the best that you can do:

Imports System.Data.SqlClient

Public Class frmPubs
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()

    'This call is required by the Windows Form Designer.

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
      End If
    End If
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents gridPubs As System.Windows.Forms.DataGrid
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.gridPubs = New System.Windows.Forms.DataGrid
    CType(Me.gridPubs, System.ComponentModel.ISupportInitialize).BeginInit()
    Me.gridPubs.DataMember = ""
    Me.gridPubs.HeaderForeColor = System.Drawing.SystemColors.ControlText
    Me.gridPubs.Location = New System.Drawing.Point(12, 20)
    Me.gridPubs.Name = "gridPubs"
    Me.gridPubs.Size = New System.Drawing.Size(468, 296)
    Me.gridPubs.TabIndex = 0
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(492, 326)
    Me.Name = "frmPubs"
    Me.Text = "Pubs Example"
    CType(Me.gridPubs, System.ComponentModel.ISupportInitialize).EndInit()

  End Sub

#End Region

  Private m_adapterAuthors As SqlDataAdapter
  Private m_adapterTitles As SqlDataAdapter
  Private m_dataAuthors As New DataSet
  Private m_tableAuthors As DataTable
  Private m_tableTitles As DataTable

  Private Sub frmPubs_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    Me.gridPubs.DataSource = m_dataAuthors
    Me.gridPubs.DataMember = "authors"
    Me.gridPubs.AlternatingBackColor = Color.AliceBlue

  End Sub

  Private Sub LoadAuthorTitles()

    Dim connectionString As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=pubs;"

    Dim query As String = _
      "SELECT dbo.authors.au_id, dbo.titleauthor.title_id, dbo.authors.au_lname, dbo.authors.au_fname" & _
      "  FROM dbo.authors INNER JOIN" & _
      "       dbo.titleauthor ON dbo.authors.au_id = dbo.titleauthor.au_id"

    m_adapterAuthors = New SqlDataAdapter(query, connectionString)

    query = _
      "SELECT dbo.titleauthor.au_id, dbo.titleauthor.title_id, dbo.titles.title" & _
      "  FROM dbo.titleauthor INNER JOIN" & _
      "       dbo.titles ON dbo.titleauthor.title_id = dbo.titles.title_id"

    m_adapterTitles = New SqlDataAdapter(query, connectionString)

    m_adapterAuthors.Fill(m_dataAuthors, "authors")
    m_adapterTitles.Fill(m_dataAuthors, "titles")

    m_tableAuthors = m_dataAuthors.Tables("authors")
    m_tableTitles = m_dataAuthors.Tables("titles")

    Dim authorID As DataColumn = m_tableAuthors.Columns("au_id")
    Dim authorTitle As DataColumn = m_tableAuthors.Columns("title_id")
    Dim titleAuthor As DataColumn = m_tableTitles.Columns("au_id")
    Dim titleID As DataColumn = m_tableTitles.Columns("title_id")

    m_dataAuthors.Relations.Add("authors_titles", New DataColumn() {authorID, authorTitle}, _
      New DataColumn() {titleAuthor, titleID})

  End Sub

End Class

This is a sample form that highlights showing parent/child relationships hierarchical.  According to this article, the DataGrid doesn't support showing parent and child rows on the same form:

Using the Windows Forms DataGrid in Visual Basic .NET

Host: Scott (Microsoft)
Q: How can I show hierarchical data in the grid using a relation without requiring my users to click on the relation hyperlink?

A: Bind directly to the data you want to show. Set DataSource = the DataSet, DataMember = "ParentTableName.RelationName"

Host: Scott (Microsoft)
Q: I just tried that and it only shows the child data.

A: Sorry -- I misunderstood the question. It sounds like you want to show the parent row together with their children: the DataGrid doesn't support this, I'm afraid.



Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month21 days, 4 hours left to enroll

810 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