Hi,
I am trying to understand copying of DataTables and processing changes.
I have created a DataTable dt1 (with not rows) in a Dataset ds1 and persisted it into an XML file.
And set Column 1 as a Primary Key and an Autoincrement Integer:
If I read the XML into ds1
then create a new DataTable dt2:
(it has 0 rows)
then in a Form (Form1) copy dt1 into dt2:
......
then load a DataGridView (dgv1) control with dt2
Then I add a row using the DatagridView (dgv1)
Then I look at dt2
And find as expected that dt2 has 1 row (the row I added)
Then I look at the changes to dt2:
I find the RowState = 4
and ID = 0
again as expected
Now what I don't understand is if I clear dt1 and copy dt2 back into dt1
dt2 still has 1 row
but dt1 has 0 rows
the copy appears to have failed but without errors.
Why is that?
does it have anything to do with the primary key or autoincrement?
Thanks for any light you can shed on this.
Doug
I am trying to understand copying of DataTables and processing changes.
I have created a DataTable dt1 (with not rows) in a Dataset ds1 and persisted it into an XML file.
And set Column 1 as a Primary Key and an Autoincrement Integer:
VB.NET:
Dim key(0) As DataColumn
key(0) = dt1.Columns("ID")
dt1.PrimaryKey = key
dt1.Columns("ID").AutoIncrement = True
then create a new DataTable dt2:
VB.NET:
Private dt2 as DataTable = new DataTable
then in a Form (Form1) copy dt1 into dt2:
......
VB.NET:
dt2 = ds1.Tables(dt1).Copy
then load a DataGridView (dgv1) control with dt2
VB.NET:
dgv1.DataSource = dt2
Then I look at dt2
And find as expected that dt2 has 1 row (the row I added)
Then I look at the changes to dt2:
VB.NET:
Dim dt1Chng As DataTable = dt2.GetChanges
Dim dr As DataRow
For Each dr In dt2Chng.Rows
st = dr.RowState
st = dr("ID").ToString
and ID = 0
again as expected
Now what I don't understand is if I clear dt1 and copy dt2 back into dt1
VB.NET:
dt1.Clear()
dt1 = dt2.Copy
ds1.AcceptChanges()
but dt1 has 0 rows
the copy appears to have failed but without errors.
Why is that?
does it have anything to do with the primary key or autoincrement?
Thanks for any light you can shed on this.
Doug
Last edited by a moderator: