none
Copying Row from one DataTable to other one RRS feed

  • Question

  • i m using the code as below to copy one row of one Table (Table1) to the

    other one (objDS.Tables[0]) which is the part of a DataSet but row of objDS.Tables[0] is empty even

    after copying the whole table by using loop.

     

     

    NewRow = objDS.Tables[0].NewRow();

    NewRow = Table1.Rows[0];

    objDS.Tables[0].Rows.Add(NewRow);

    objDS.Tables[0].AcceptChanges();

     

     

    Please tell me whats the wrong m i doing.

     

    Tuesday, February 12, 2008 6:20 PM

Answers

  • MSDN example of the works)

    Code Snippet

            private System.Data.DataSet dataSet;

            private void MakeParentTable()
            {
                // Create a new DataTable.
                System.Data.DataTable table = new DataTable("ParentTable");
                // Declare variables for DataColumn and DataRow objects.
                DataColumn column;
                DataRow row;

                // Create new DataColumn, set DataType,
                // ColumnName and add to DataTable.    
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.Int32");
                column.ColumnName = "id";
                column.ReadOnly = true;
                column.Unique = true;
                // Add the Column to the DataColumnCollection.
                table.Columns.Add(column);

                // Create second column.
                column = new DataColumn();
                column.DataType = System.Type.GetType("System.String");
                column.ColumnName = "ParentItem";
                column.AutoIncrement = false;
                column.Caption = "ParentItem";
                column.ReadOnly = false;
                column.Unique = false;
                // Add the column to the table.
                table.Columns.Add(column);

                // Make the ID column the primary key column.
                DataColumn[] PrimaryKeyColumns = new DataColumn[1];
                PrimaryKeyColumns[0] = table.Columns["id"];
                table.PrimaryKey = PrimaryKeyColumns;

                // Instantiate the DataSet variable.
                dataSet = new DataSet();
                // Add the new DataTable to the DataSet.
                dataSet.Tables.Add(table);

                // Create three new DataRow objects and add
                // them to the DataTable
                for (int i = 0; i <= 2; i++)
                {
                    row = table.NewRow();
                    row["id"] = i;
                    row["ParentItem"] = "ParentItem " + i;
                    table.Rows.Add(row);
                }
            }
    Tuesday, February 12, 2008 6:52 PM