May 29, 2007
@ 10:25 PM

Senaryo gereği bir DataTable'dan başka bir DataTable'a DataRow eklemek istediğimizde :

{
DataTable _sourceDataTable = _xDataSet.Tables[0];
DataTable _destinationDataTable = new DataTable();

foreach
(DataRow row in _sourceDataTable.Rows)
{
    if(row["col"] == "aranan_değer")
    {
        _destinationDataTable.Rows.Add(row);
    }
}

gibi bir kod işimizi görür gibi düşünsek de " This row already belongs to another table " hatası ile karşılaşırız. Çözüm, önce DataTable nesnesinin Clone() metodu ile kopyalamak istediğimiz diğer DataTable'i oluşturmak arkasından ImportRow() metodu ile kaynak DataTable'dan hedef DataTable'a isteniler DataRow'u eklemek.

{
DataTable _sourceDataTable = _xDataSet.Tables[0];
DataTable _destinationDataTable = new DataTable();
 
_destinationDataTable= _sourceDataTable.Clone();

foreach(DataRow row in _sourceDataTable.Rows)
{
     if(row["col"] == "aranan_değer")
    {
         _destinationDataTable.ImportRow(row);
    }
}


 
Categories: