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!
Loading
JenPosted Apr 7, 2006, 10:50 AM
y_khandelwalPosted Apr 6, 2006, 12:59 PM
Jayadev NairPosted Apr 6, 2006, 10:46 AM
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!
JenPosted Apr 3, 2006, 2:35 PM
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!
Jayadev NairPosted Apr 1, 2006, 5:42 AM
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.