There are tables with a RowNumber field, which is a PrimaryKey, i.e. unique and read only. The table structures are the same, but the data is different. It is necessary for me that in one table there was data of two tables. The RowNumber value is indifferent. There are 3 rows in the table. RowNumber that the first, that the second from 1 to 3.

table1 RowNumber Data ------------------ 1 data 1 2 data 2 3 data 3 table2 RowNumber Data ------------------ 1 data A 2 data B 3 data C Необходимый результат RowNumber Data ------------------ 1 data 1 2 data 2 3 data 3 4 data A 5 data B 6 data C 

If i do like this

 table1.Merge(table2); 

then I get in table1 what was in table 2. If I try to add rows from table2 to table1 in a loop, then the exception "This row already belongs to another table."

Who knows how to combine two tables with a unique rowNumber. The content of RowNumber after the merging of tables does not matter to me, the main thing is that in one table the data of two tables appear

    2 answers 2

    Did as follows

     long maxRowNum = table1.Rows.Count > 0 ? (long)table1.Compute("MAX(RowNumber)", string.Empty) : 0; foreach (DataRow r in table2.Rows) { var dr = r.ItemArray; dr[0] = ++maxRowNum; table1.Rows.Add(dr); } 

      Here is my code, I have been using it for a long time.

       public static void CopyRow(DataRow rowIn, DataRow rowOut, bool isAddOutColumns) { foreach (DataColumn column in rowIn.Table.Columns) { if (!rowOut.Table.Columns.Contains(column.ColumnName)) { if (!isAddOutColumns) continue; rowOut.Table.Columns.Add(column.ColumnName, column.DataType); } bool equals;/* = rowOut[column.ColumnName].GetType().Equals(rowIn[column.ColumnName].GetType()); if (equals) { */ if (rowOut[column.ColumnName] is byte[] && rowIn[column.ColumnName] is byte[]) equals = ((byte[])rowOut[column.ColumnName]).SequenceEqual((byte[])rowIn[column.ColumnName]); else equals = rowOut[column.ColumnName].Equals(rowIn[column.ColumnName]); /* } */ if (!equals) try { rowOut[column.ColumnName] = rowIn[column.ColumnName]; } catch { } } } public static void UnionTables(DataTable table, DataTable source, string uniqueName = "id", bool ReplaceIfDouble = true) { DataView vSource = new DataView(source) { Sort = uniqueName }; foreach (DataColumn column in source.Columns) if (!table.Columns.Contains(column.ColumnName)) table.Columns.Add(column.ColumnName, column.DataType); DataView vTable = new DataView(table) { Sort = uniqueName }; int iRow = 0; foreach (DataRowView rowSource in vSource) { if (rowSource[uniqueName] == DBNull.Value) continue; IComparable idSource = (IComparable)rowSource[uniqueName]; DataRow newRow = null; for (; iRow < vTable.Count; ) { if (vTable[iRow][uniqueName] == DBNull.Value) { ++iRow; continue; } IComparable idTable = (IComparable)vTable[iRow][uniqueName]; if (idTable.CompareTo(idSource) > 0) break; if (idTable.CompareTo(idSource) == 0) { if (ReplaceIfDouble && newRow == null) { newRow = vTable[iRow].Row; CopyRow(rowSource.Row, newRow, true); } else throw new Exception("Строка с таким ID уже существует"); } ++iRow; } if (newRow == null) { newRow = table.NewRow(); CopyRow(rowSource.Row, newRow, true); table.Rows.Add(newRow); } } } 

      Your case

       UnionTables(table1, table2, "RowNumber"); 

      in table1 there will be all the data, but you need to change the name of the Data columns to be unique in all tables

      • Thanks for the answer, but I didn't want to rename the columns and, it seemed, the implementation was too complicated. I found the answer, published it as an answer to my own question - Olejan