Skip to content

Link DataGrid's Together

Jen

Asked Mar 30, 2006, 6:28 AM EST5 answers
Share
LinkedInX
I have 2 datagrids on 2 different forms bound to 2 different SQL tables.  On the 1st datagrid, I have a DataGridViewButtonColumn called Details.  Both DataGrids have a TimeEntryID column in common.  I would like to be able to click on the Details button in the 1st DataGrid and be able to link the TimeEntryID column to the TimeEntryID column in the 2nd DataGrid.  How can I do this?  I am relatively new to VB.NET.
Thanks!

5 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Jayadev Nair
    0

    Hi
    I'm not sure i understood your question well. But let me try to answer you.
    Say datagrid1's source recordset is achieved from Query1
    and similary datagrid2's source from Query2

    Dim dt1 as System.Data.DataTable = RecordSet from Query1
    Dim dt2 as System.Data.DataTable = RecordSet from Query2

    Dim ds as New System.Data.Dataset
    ds.Tables.Add(dt1)
    ds.Tables.Add(dt2)

    '\\ So the Dataset is having both tables you need
    '\\ Now set relation of first table's TimeEntryID to
    '\\ second ones TimeEntryID field

    ds.relations.Add("relationName",ds.Tables(0).Columns("TimeEntryID"),ds.Tables(1).Columns("TimeEntryID"))

    You can either keep this dataset object in Application cache or pass to both forms ByRef from a parent form.

    Will this help you?

    thanks.

    0
  • 0
    First, thank you for responding to my question!!  Yes, this has helped point me into the right direction, although I have run into other issues.  When I first attempted this, I kept receiving:
    DataTable already belongs to another DataSet.
    This occurs with the following code:
    ds.Tables.Add(dt1)
    ds.Tables.Add(dt2)

    Since then, I have found that you need to either copy or clone the tables then add them to the new Dataset.  I have done the following:
    Dim ds As New System.Data.DataSet
    Dim dt1 As New DataTable
    dt1 = Form1.DataSet1.tblDuplicate.Copy()
    Dim dt2 As New DataTable
    dt2 = Me.DataSet2.tblTime.Copy()
    ds.Tables.Add(dt1)
    ds.Tables.Add(dt2)
    ds.Relations.Add(New DataRelation("TimeEntry", ds.Tables(0).Columns("TimeEntryID"), ds.Tables(1).Columns("TimeEntryID")))

    When the debugger gets to the last line of code up above, I receive the following:
    ArgumentNullException was unhandled
    'column' argument cannot be null
    Parameter name: column

    I checked my column names to make sure that they are correct and it appears that they are. 

    Do you have any ideas?

    Thanks!
    0
  • Jayadev Nair
    0

    Hi Jen
    A datatable cannot be added to a dataset when it belongs to another dataset since, only reference to the datatable is passed to dataset. You can use clone(), but only table structure will be added. but as you used, copy() will do fulfill your reqrments. or remove the datatable from existing dataset by dt1.dataset.tables.remove(dt1) must work.

    Anyway, i found that is not an issue with you now. Setting a relation could work with your current code or simply like

    ds.Relations.Add("TimeEntry", ds.Tables(0).Columns("TimeEntryID"), ds.Tables(1).Columns("TimeEntryID"))

    The possibility of an ArgumentNullException is that your table doesnot have such a column. means either of your code

    ds.Tables(0).Columns("TimeEntryID")
    Or
    ds.Tables(1).Columns("TimeEntryID")

    returns Nothing.

    Please Check you have the right tables and its columns inserted at runtime.

    Regards!

    0
  • 0
    Thank you both for your suggestions.  With your help, I was able to resolve the issue and now have the master/details relation functioning correctly.
    0