Solved

Linking Data Tables Programmatically

Posted on 2006-06-22
8
174 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 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

19 Experts available now in Live!

Get 1:1 Help Now