Click here to Skip to main content
15,896,541 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

Imagine having a Strongly Typed Dataset with 2 tables

* Books (ISBN, BookTitle, BookAuthor)

* Authors (AuthorID, AuthorName)

and a FK relation between the 2 tables (BookAuthor <-> AuthorID)

I need to bind a datagridview to the show the Books table, so I set a Binding Source
C#
LibraryDataSet dsLib = new LibraryDataSet();
// Filling the tables
// ...
BindingSource bsBooks = new BindingSource();
bsBooks.DataSource = dsLib;
bsBooks.DataMember = "Books";


Then Set the DataGridView to be bound to bsBooks.

Now I can view ISBN, BookTitle, and BookAuthor

I need to show the AuthorName instead of the AuthorID.

What's the best practices to do so?
I don't want to add a new table as the combination of the two tables as this will result in 2 copies of the same data which will lead to difficulties maintaining both of them (Updates, ... etc)


Here is what I've tried so far:

1- Add a new unbound column and manually set the value inside a event handler for RowAdded event

C#
for (int i = 0; i < e.RowCount; i++)
{
   BooksDataSet.BooksRow book = (BooksDataSet.BooksRow)((DataRowView)(dgv.Rows[e.RowIndex + i]).DataBoundItem).Row;
   dgv["colAuthor", e.RowIndex + i].Value = book.AuthorsRow.AuthorName;
}

But this adds a lot of headache with Filtering the BindingSource or Refreshing data

2- Added a new Property to the BooksRow class to return the AuthorName

C#
partial class BooksRow
{
   public string AuthorName
   {
      get
      {
         return this.AuthorsRow.AuthorName;
      }
      set
      {
         this.AuthorsRow.AuthorName= value;
      }
   }
}

But it doesn't seem to be recognized by the BindingSource

Any better Ideas?
Posted
Updated 25-Sep-13 6:29am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900