Solved

Linking Data Tables Programmatically

Posted on 2006-06-22
8
175 Views
Last Modified: 2008-01-16
I have two tables (in memory only), which belong to a dataset. The expense table is bound to a DataGridView. How can I link the two tables so that instead of showing the CategoryID in the grid, it shows the actual CategoryName associated with the CategoryID on the Expense table?

' Category Table
dtCategory = New DataTable("Category")
dtCategory.Columns.Add("CategoryID", GetType(Integer))
dtCategory.Columns.Add("CategoryName", GetType(String))
dtCategory.Columns.Add("Description", GetType(String))

' Expense Table
dtExpense = New DataTable("Expense")
dtExpense.Columns.Add("ExpenseID", GetType(Integer))
dtExpense.Columns.Add("CategoryID", GetType(String))
dtExpense.Columns.Add("Amount", GetType(Integer))

' Dataset
dsBudget = New DataSet("Budget")
dsBudget.Tables.Add(dtCategory)
dsBudget.Tables.Add(dtExpense)

' DataGridView
grdSelection.AutoGenerateColumns = False
grdSelection.DataSource = dsBudget
grdSelection.DataMember = "Expense"
0
Comment
Question by:jmsbtyler
  • 4
  • 4
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16964126
Add a DataRelation:

      ' Category Table
        dtCategory = New DataTable("Category")
        dtCategory.Columns.Add("CategoryID", GetType(Integer))
        dtCategory.Columns.Add("CategoryName", GetType(String))
        dtCategory.Columns.Add("Description", GetType(String))

        ' Expense Table
        dtExpense = New DataTable("Expense")
        dtExpense.Columns.Add("ExpenseID", GetType(Integer))
        dtExpense.Columns.Add("CategoryID", GetType(String))
        dtExpense.Columns.Add("Amount", GetType(Integer))

        ' Dataset
        dsBudget = New DataSet("Budget")
        dsBudget.Tables.Add(dtCategory)
        dsBudget.Tables.Add(dtExpense)

        ' Relation
        dsBudget.Relations.Add("Category_Expense", New DataColumn() {dtCategory.Columns("CategoryID")}, New DataColumn() {dtExpense.Columns("CategoryID")})

        ' DataGridView
        grdSelection.AutoGenerateColumns = False
        grdSelection.DataSource = dsBudget
        grdSelection.DataMember = "Expense"

Bob
0
 

Author Comment

by:jmsbtyler
ID: 16970891
Thanks for your response.

I've since looked into the Data Relation, and played around with some things but I am still not sure what to do to get it to work in the grid.

What do I need to set the DataProperty Name to for the CategoryID column? (The grid is linked to the Expense Table)

Bruce
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16971374
The DataGrid can display parent/child records with a plus sign to expand, but it's a little weird, and a lot of people don't like that.  You need to bind columns in the DataGrid to the text columns, and not the primary key columns.

Bob
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:jmsbtyler
ID: 16971785
How do I bind the columns in the grid to Text columns instead of the fields from the table?

Instead of binding the table to the grid, do I need to loop through the table and manually insert each row into the grid (while looking up the Category Name using the ID)?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16985017
What .NET version are you using?  2002, 2003, or 2005?

Bob
0
 

Author Comment

by:jmsbtyler
ID: 16985047
2005

Sorry, I should have put that in my original question.

Bruce
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 16985209
Yeah, I couldn't see declarations, but I noticed the DataGridView comment, and I didn't want to make any assumptions.

Here is an example of adding a column programmatically:

        Me.DataGridView1.AutoGenerateColumns = False
        Me.DataGridView1.Columns.Clear()

        Dim column As New DataGridViewTextBoxColumn
        column.DataPropertyName = "name"
        column.HeaderText = "Name"
        column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

        Me.DataGridView1.Columns.Add(column)

Bob
0
 

Author Comment

by:jmsbtyler
ID: 16988611
It works!

I left the datagrid bound to the "Expense" table, and then I created a new column called "CategoryName". I then added the following logic to loop through each row in the datagrid and populate the Categoryname column based on the CategoryID.

For Each Row As DataGridViewRow In grdSelection.Rows
       Dim CategoryID As Integer = Row.Cells("CategoryID").Value
       Dim FoundRow As DataRow = dsBudget.Tables("Category").Rows.Find(CategoryID)
       If FoundRow IsNot Nothing Then
                Row.Cells("CategoryName").Value = FoundRow(1).ToString
       End If
Next

Thanks for all of your help Bob! I really appreciate it!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

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