none
use Parent for BindingSource? RRS feed

  • Question

  • Hi guys!!
    So here is the issue.
    Lets say I have a strongly typed DataSet that haves these two tables:
    AUTHORS
    ID
    Name

    and
    BOOKS
    ISBN
    Name
    Subject
    AuthorID


    Somewhere my system haves a function that returns a DataSet with the books of a certain subject and all the Authors that wrote such books.

    I need to show the book's attributes in some textboxes inside a Form. I use a BindingSource to display the ISBN, Name and Subject of the books in a textbox, yet I don't know how to display the author name of the book I am currently viewing in a TextBox, taking into account I already fetched the Author into the DataSet.

    Its pretty much the Child/Parent display problem somewhere discussed in the codebank, but kinda backwards, the thing is that I;m navigating through the CHILD records here, not the parent, yet i have all the data needed in my Dataset.

    Hope I explained my problem enough :P
    Monday, May 16, 2011 3:17 PM

Answers

  • I figured it all out, the

    Dim actualBookAs DataSet.BookRow
    actualBook= DirectCast(Me.BindingSourceBooks.Current, DataRowView).Row

    Works perfectly fine, and with the propper use of the InitializeComponent() and a custom Constructor I got the results I needed!

    :)

    • Marked as answer by LECHIP Wednesday, May 18, 2011 11:51 AM
    Wednesday, May 18, 2011 11:51 AM

All replies

  • I haven't actually tested this, but it should work if you define a DataRelation backwards: with the Books as the Parent and the Authors as the Child.

    DataRelation dr = new DataRelation("BookAuthorRelation", MyData.Books.AuthorIDColumn, MyData.Authors.IDColumn);
    
    MyData.Relations.Add(dr);
    
    // bsBooks and bsAuthors are BindingSource objects
    bsBooks.DataSource = MyData;
    bsBooks.DataMember = "Books";
    bsAuthors.DataSource = bsBooks;
    bsAuthors.DataMember = "BookAuthorRelation";
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Monday, May 16, 2011 3:51 PM
  • Hey Bonnie!, tnx for the answer!

    I already tried this, since the column on the child table can have multiple times a same author (let's say i fetched many books from different authors, yet some are from the same), the backwards-relationship is not possible. The following exception is thrown, as it should:

    These columns don't currently have unique values.

    As it should happen.

    I guess I could just fetch the needed results again everytime i navigate through the records, but I just find it ridiculous, since I ALREADY have the needed records in my DataSet and a relationship is already built (that's half of the reason for making a strongly typed DataSet).

    Yet I'm open for better suggestions.

    Tnx!




    Tuesday, May 17, 2011 2:43 PM
  • There's something I don't understand about the exception you get: The column Books.AuthorID should not be unique, since it's an FK. The DataSet itself should have properly made the relation if you have proper relational integrity in your database. Could it be that you load duplicate rows into your Authors table?
    Tuesday, May 17, 2011 7:25 PM
  • The column Books.AuthorID is indeed not unique, it's an FK as you say. We were just trying to create a reverse relationship here with the purpose of binding the BindingSource to the backwards relationship.

    Obviously the relationship wont be created since it is the exact opposite of the good relationship I already have.

    Right now I;m trying a different approach. I can get the current record of my BindingSourceBooks like this:

     

    Dim actualBookAs DataSet.BookRow
    actualBook= DirectCast(Me.BindingSourceBooks.Current, DataRowView).Row
    

     

    And with that I can get any other row on any other Parent table on the DataSet.

    I just did not bind the Parent table(s) to anything, and I'm trying to display the data every time an event happens (like changing the current element of a navigator bound to the BindingSourceBooks. Yet i get this problem here:

    Let;s say I create a method

    fetchAndShowParents()
    

    Which gets the Parents of the current element on the BindingSourceBook and shows them.

    Lets say that I use such method in a DataNavigator RefreshItems Event In theory it should help, but theres a catch: The form receives the DataSet with the Data in runtime, and the Binding occurs in the FormLoad Event. The DataNavigator RefreshItems Event fires before the FormLoads it;s elements, and that means the navigator will try to use the fetchAndShow() method in an empty DataSet.

    Still haven't figured out what to do.

    cheers!

    • Marked as answer by LECHIP Wednesday, May 18, 2011 11:50 AM
    • Unmarked as answer by LECHIP Wednesday, May 18, 2011 11:50 AM
    Wednesday, May 18, 2011 8:23 AM
  • I figured it all out, the

    Dim actualBookAs DataSet.BookRow
    actualBook= DirectCast(Me.BindingSourceBooks.Current, DataRowView).Row

    Works perfectly fine, and with the propper use of the InitializeComponent() and a custom Constructor I got the results I needed!

    :)

    • Marked as answer by LECHIP Wednesday, May 18, 2011 11:51 AM
    Wednesday, May 18, 2011 11:51 AM
  • Sorry, I didn't get a notification about your reply yesterday. You actually *can* create this backwards relationship but without constraints enabled. Then it will work. I actually tried it this time and it works fine and doesn't break your other "good" relationship. ;0)

    // All you need, is to set the last parameter to false
    // I didn't show it this way in my first example
    DataRelation dr = new DataRelation("BookAuthorRelation", MyData.Books.AuthorIDColumn, MyData.Authors.IDColumn, false);
    
    MyData.Relations.Add(dr);
    
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, May 18, 2011 3:28 PM