Solved

Linking Data Tables Programmatically

Posted on 2006-06-22
8
176 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Gridview selected row 9 49
HTML - Color not displaying correctly in EMAIL. 6 49
Get number of Files in Directory and Sub Directories 2 48
location of a form 2 25
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

840 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