Solved

Linking Data Tables Programmatically

Posted on 2006-06-22
8
178 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
[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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

705 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