Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have one access table with 3 columns one of the columns being the Primary Key. The next column is called "Group" and the other two will be the data displayed in a heirachale format in a datagrid. For example

 

MyTable

 

PK Group Entry

1 Johnny Amber

2 Johnny Alex

3 Johnny Lauren

4 Johnny Stephanie

 

In my datagrid I want "Johnny" to be in the first cell with a "+" sign next to it, and when the user clicks the "+" it lists all the entries with "Johnny" has a group head in a hierachale form. Almost like a treeview be using a DataGrid. I am not quite sure how to accomplish this task. Any help would greatly be appreciated.

Posted

The only thing I could think of is creating a relationship between the same table. In the first SQL Statement select "Group" from tableName and the second SQL Statement Select Group, Entry from tableName.

 

Example

 

' Set strDatabaseName
       strDatabaseName = "Database Location"

       Dim cnAdoNetConnection As New OleDb.OleDbConnection
       Dim cCommand1 As OleDb.OleDbCommand
       Dim cCommand2 As OleDb.OleDbCommand
       Dim daDataAdapter1 As OleDb.OleDbDataAdapter
       Dim daDataAdapter2 As OleDb.OleDbDataAdapter
       Dim ds As New DataSet ' Dataset that holds data in disconnected mode

       Dim strSqlQuery1 As String
       Dim strSqlQuery2 As String
       
       strSqlQuery1 = "SELECT Group From TableName"
       strSqlQuery2 = "SELECT Group, Entry from TableName"

       ' This handles the relationship between the two tables
       Dim drDataRelation As DataRelation
       Dim dc1 As DataColumn
       Dim dc2 As DataColumn

       Try

           ' Open the connection to the database
           cnAdoNetConnection.ConnectionString = _
               "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
               strDatabaseName
           cnAdoNetConnection.Open()

           ' First Command for first table
           cCommand1 = New OleDb.OleDbCommand
           cCommand1.Connection = cnAdoNetConnection
           cCommand1.CommandText = strSqlQuery1

           ' Second Command for second table
           cCommand2 = New OleDb.OleDbCommand
           cCommand2.Connection = cnAdoNetConnection
           cCommand2.CommandText = strSqlQuery2

           ' Now, we will fill the first table and add it to the dataset
           daDataAdapter1 = New OleDb.OleDbDataAdapter
           daDataAdapter1.SelectCommand = cCommand1
           daDataAdapter1.TableMappings.Add("Table", "TableName1")
           daDataAdapter1.Fill(ds)

           ' As we did in the previous step, Hee for the second table
           daDataAdapter2 = New OleDb.OleDbDataAdapter
           daDataAdapter2.SelectCommand = cCommand2
           daDataAdapter2.TableMappings.Add("Table", "TableName2")
           daDataAdapter2.Fill(ds)

           dc1 = ds.Tables("TableName1").Columns("Group")
           dc2 = ds.Tables("TableName2").Columns("Group")

           ' Here we combined two datacolumns to the relations obj
           drDataRelation = New DataRelation("DataGrid Title", dc1, dc2)
           ds.Relations.Add(drDataRelation)

           ' Bind the dataset after all operation to the datagrid
           DataGrid1.DataSource = ds.DefaultViewManager

           ' Show the first table in the grid because it's the primary table
           DataGrid1.DataMember = "TableName1"

 

Hope this helps.

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

-- Rick Cook, The Wizardry Compiled

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...