Getting Exception : Here The given DataRow is not in the current DataRowCollection - c#

I have facing Problem..
DataTable Dt1 = table1;
DataTable Dt2 = Dt1.Copy();
DataRow[] row = Dt1.Select("Name = 'Test'");
foreach (DataRow row in Dt2)
{
Dt2.Rows.Remove(row); // Here The given DataRow is not in the current DataRowCollection
}
Its giving the exception because I Filter the date from Different Row and Removing It from Different Row.
Thanks, Shivam

Your current code removes all rows from the Dt2 DataTable because you overwrite the selected row from Dt1 when you loop through your foreach statement.

Related

Copy source datatable to destination datatable with the schema of destination

I'm trying to copy the data from source dt to destination datatable.
source datatble types are sting and destination datatble types contains datetime along with strings.
datatable dt2=new datatable();
foreach (DataRow row in dt1.Rows)
{
dt2.ImportRow(row); //String was not recognized as a valid DateTime.
}
I get String was not recognized as a valid DateTime as destination column type is datetime and is not able to import that row.
Use DataTable.Clone() to setup a new DataTable object with the existing schema. Then add any additional columns you might need.
DataTable dt1 = MyData();
DataTable dt2 = dt1.Clone();
foreach(DataRow row in dt1.Rows)
{
dt2.ImportRow(row);
}

How to assign one datarow values to another datarow

I want to copy first row of one DataTable to first row of another DataTable. I did as shown below but it throws error like
Property or indexer 'System.Data.DataRowCollection.this[int]' cannot be assigned to -- it is read only
DataTable dt = GetTable();
DataTable dt1 = GetTable1();
dt.Rows[0] = dt1.Rows[0];
You have to copy it field by field.
The data row within the datatable is immutable, which means that you can change the values of its fields, but not the object itself, being said that you should copy the values from you source row to the destination row.
Assuming your datatables have the same columns:
DataTable dt = GetTable();
DataTable dt1 = GetTable1();
foreach(DataColumn column in dt.Columns)
{
dt.Rows[0][column] = dt1.Rows[0][column];
}
Hope this helps.

ImportRow make the new row column missing

I want to copy all rows in old datatable (oldDT) to a new datatable (dt) using ImportRow method. But the new row doesn't have a same column with the old one. Here is my code:
foreach (DataRow dr in oldDT.Rows)
{
MessageBox.Show(dr["tenant_no"].ToString()); //giving a correct result
dt.ImportRow(dr);
MessageBox.Show(dt.Rows[0]["tenant_no"].ToString()); //giving an error Column 'tenant_no' does not belong to table .
}
i try to use the answer C# simple way to copy or clone a DataRow? and here is my new code:
foreach (DataRow dr in oldDT.Rows)
{
MessageBox.Show(dr["tenant_no"].ToString());
DataRow newDR = oldDT.NewRow();
newDR.ItemArray = dr.ItemArray.Clone() as object[];
dt.Rows.Add(newDR); //giving an error "This row already belongs to another table."
MessageBox.Show(dt.Rows[0]["tenant_no"].ToString());
}
Anyone can help me?
Try this... this will work
DataTable dt= new DataTable();
dt= oldDT.Clone();
for (int i=0;i < oldDT.Rows.Count; i++)
{
dt.ImportRow(dataTable.Rows[i]);
}
You could use DataTable.Copy, if you are not locked into using the ImportRow :
Copies both the structure and data for this DataTable.
DataTable dt = oldDt.Copy();

Selecting specific columns from a dataTable and copy it to another DataTable

I have a DataTable Dt1 and another DataTable Dt2.
Dt1 contains many columns and rows and Dt2 is basically empty
I know I should use Dt1.Select to select the specific DataTaRow [] but how can I copy them to Dt2
DataRow [] row = Dt1.Select("ID,MIN_VALUE,MAX_VALUE");
how can i copy them to Dt2?
For .Net Framework 3.5+
You can use CopyToDataTable method.
Returns a DataTable that contains copies of the DataRow objects, given
an input IEnumerable object.
DataTable dt1 = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt2 = dr.CopyToDataTable();

Exception in copying DataRow to another DataTable

I have two data tables, dt1 & dt2. dt1 has data while dt2 is a new table. Now I want to copy some rows, satisfying particular condition, to dt2. I have tried following code, but I get this exception:
System.ArgumentException This row already belongs to another table.
foreach(DataRow dr in dt1.Rows)
{
if(Convert.ToInt32(dr["col"]) == value)
{
dt2.Rows.Add(dr);
}
}
How can I resolve this?
change dt2.Rows.Add(dr); to dt2.Rows.Add(dr.ItemArray);
You directly cannot "Add" a DataTable row from one to another since it belongs to souce Datatable. You can "Add" a newly created row to DataTable.
Hence you are getting this exception.
There are many ways to overcome this. Some are listed below
Method 1:
If you want to get a row from source Datatable and add in destination without any changes, then use ImportRow this way.
dt2.ImportRow(dr);
Method 2:
But if you want to take specific data. Then use this
foreach(DataRow dr in dt1.Rows)
{
if(Convert.ToInt32(dr["col"]) == value)
{
Datarow ndr = dt2.NewRow();
ndr["Foo"] = dr["Foo"];
//..Similarly for other rows.
dt2.Rows.Add(ndr);
}
}
Every time when you add a row to the datatable it should be different(new) row........
Datarow newRow = null;
foreach(DataRow dr in dt1.Rows)
{
if(Convert.ToInt32(dr["col"]) == value)
{
newRow = dt2.NewRow();
newRow ["A"] = dr["A"];
dt2.Rows.Add(newRow );
}
}

Categories

Resources