Solved

Linking Data Tables Programmatically

Posted on 2006-06-22
8
173 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jmsbtyler
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 96

Expert Comment

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

Bob
0
 

Author Comment

by:jmsbtyler
Comment Utility
2005

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

Bruce
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
Comment Utility
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
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now