Results 1 to 5 of 5

Thread: [RESOLVED] Inserting data into the DataGridView. How to show columns from different tables?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Resolved [RESOLVED] Inserting data into the DataGridView. How to show columns from different tables?

    I need to get a table on my Form that has different columns from differen tables.
    As I call many tables by their Identity ID, I also need a way to show the information from that table.


    I created a query in my TableAdapter which selects information from 2 different tables. Tested it in the query builder and all was OK.
    I tried to put an empty DataGridView onto my Form and inserted the correct BindingSource for my Data Source.
    I then added a query and selected the query I created in my TableAdapter.

    But, when I ran the program, I got an error:
    "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints."

    Weird, because it showed the correct data when I ran the query in The Query Builder....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Inserting data into the DataGridView. How to show columns from different tables?

    The problem is exactly what the error message says it is. One or more rows contain a null value in a non-nullable column, duplicate values in a column that must be unique or foreign key values that don't appear in the parent table.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Re: Inserting data into the DataGridView. How to show columns from different tables?

    Quote Originally Posted by jmcilhinney View Post
    The problem is exactly what the error message says it is. One or more rows contain a null value in a non-nullable column, duplicate values in a column that must be unique or foreign key values that don't appear in the parent table.
    Yup. If I add the columns that are not supposed to be nulls the error disappears. But still, I can not add columns from other tables... ? That is the part I can't figure out...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Inserting data into the DataGridView. How to show columns from different tables?

    We don't know anything about your data other than it comes from multiple tables. That's not enough information for us to provide any insight.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2010
    Posts
    110

    Re: Inserting data into the DataGridView. How to show columns from different tables?

    Quote Originally Posted by jmcilhinney View Post
    We don't know anything about your data other than it comes from multiple tables. That's not enough information for us to provide any insight.
    Oh, ok. Sorry mate...
    I have somewhat long and complex column and query names (in finnish), so going to give a simplified example...





    In the Data Set 'DS_Example' I have 2 tables: Table1 and Table2. In the TableAdapter of Table 1 I created a query named "Fill_TEST":
    Code:
    SELECT Table1.Date, Table1.PersonID, Table1.Value1, Table1.Value2, Table1.Value3, Table2.PersonName
    FROM Table1 INNER JOIN
    Table2 ON Table1.PersonID = Table2.PersonID
    WHERE (Table1.BooleanVal <> 'True')
    The above code tested out well in the Query Builders Execute Query...

    Then on my Form1 I added a DataGridView1 which I connected to a DataSource: DS_Example -> Table1BindingSource
    After that I added a query from the DataGridView1 "Add Query" option and chose an existing query "Fill_TEST"...

    The program added a toolstrip with a toolstripbutton "Fill_TEST" and some code to populate the datagridview.

    When I run the program and press the Fill_TEST -button, I only get information from Table1.

    I tried to modify the DataGridView1 by adding columns, but I didn't find a way to add columns that are from another table.


    So, basically the VB code that should populate the DataGridView1 with the query I wrote, does so only for the information of Table1, not the "PersonName" information of Table2. The two tables are relation connected by the "PersonID"...


    The VB code for "Fill_TEST" -toolstrip button:
    Code:
    Private Sub Fill_TESToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Try
                Me.Table1TableAdapter.Fill_TEST(Me.DS_Example.Table1)
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try
        End Sub

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width