DataTable Merge - problem with deleted row

F

Frank Uray

Hi all

I have trouble with DataTable.Merge() ...

For example:
DataTable1 has 3 rows
DataTable2 has 3 rows

now I am going to delete 1 row out of DataTable1.
After Merge, the deleted row is still in DataTable2 ... ???

I have tried to get the DataRowState.Deleted of DataTable2
to delete manually, but there is no row marked as deleted.

How can I hande this ??
Everything else (inserting, updateing) works fine.

Thanks for any help.
Regards
Frank Uray
 
F

Frank Uray

Hi Mark

Right now, I dont know what I am doing wrong ...
It is not merge at all ...

Thanks for your help,
Frank

// Current Data
System.Data.DataTable DataTableCurrent = new
System.Data.DataTable("Data");
System.Data.DataColumn DataColumnCurrentID = new
System.Data.DataColumn("ID", typeof(System.Int32));
System.Data.DataColumn DataColumnCurrentTEXT = new
System.Data.DataColumn("TEXT", typeof(System.String));
DataTableCurrent.Columns.Add(DataColumnCurrentID);
DataTableCurrent.Columns.Add(DataColumnCurrentTEXT);
DataTableCurrent.PrimaryKey = new System.Data.DataColumn[] {
DataTableCurrent.Columns["ID"] };

System.Data.DataRow DataRowCurrent1 = DataTableCurrent.NewRow();
DataRowCurrent1["ID"] = 1;
DataRowCurrent1["TEXT"] = "Text1";
DataTableCurrent.Rows.Add(DataRowCurrent1);

System.Data.DataRow DataRowCurrent2 = DataTableCurrent.NewRow();
DataRowCurrent2["ID"] = 2;
DataRowCurrent2["TEXT"] = "Text2";
DataTableCurrent.Rows.Add(DataRowCurrent2);

System.Data.DataRow DataRowCurrent3 = DataTableCurrent.NewRow();
DataRowCurrent3["ID"] = 3;
DataRowCurrent3["TEXT"] = "Text3";
DataTableCurrent.Rows.Add(DataRowCurrent3);


// New changed Data
System.Data.DataTable DataTableChanged = new
System.Data.DataTable("Data");
System.Data.DataColumn DataColumnChangedID = new
System.Data.DataColumn("ID", typeof(System.Int32));
System.Data.DataColumn DataColumnChangedTEXT = new
System.Data.DataColumn("TEXT", typeof(System.String));
DataTableChanged.Columns.Add(DataColumnChangedID);
DataTableChanged.Columns.Add(DataColumnChangedTEXT);
DataTableChanged.PrimaryKey = new System.Data.DataColumn[] {
DataTableChanged.Columns["ID"] };

System.Data.DataRow DataRowChanged1 = DataTableChanged.NewRow();
DataRowChanged1["ID"] = 1;
DataRowChanged1["TEXT"] = "Text1";
DataTableChanged.Rows.Add(DataRowChanged1);

System.Data.DataRow DataRowChanged3 = DataTableChanged.NewRow();
DataRowChanged3["ID"] = 3;
DataRowChanged3["TEXT"] = "ChangedText3";
DataTableChanged.Rows.Add(DataRowChanged3);


DataTableCurrent.Merge(DataTableChanged, true);
 
F

Frank Uray

Hi Mark

No, thats not what I want ...
ID 1 and 3 should remain with changed values,
ID 2 should be deleted.

Regards
Frank Uray

Mark Rae said:
[please don't top-post]
How can I hande this ??

Difficult to tell without seeing your code...

DataTableCurrent.PrimaryKey = new System.Data.DataColumn[] {
DataTableCurrent.Columns["ID"] };
DataRowCurrent1["ID"] = 1;
DataRowCurrent2["ID"] = 2;
DataRowCurrent3["ID"] = 3;
DataTableChanged.PrimaryKey = new System.Data.DataColumn[] {
DataTableChanged.Columns["ID"] };
DataRowChanged1["ID"] = 1;
DataRowChanged3["ID"] = 3;
Right now, I dont know what I am doing wrong ...

You're breaking primary key validation.

Change the ID values in DataRowChanged to 4 and 5, and you will have 5 rows
in the merged DataTable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top