[C#] DataTables 간에 DataRows를 복사하는 방법

// 원본 객체로 DB 가져온 데이터를 매핑 시켜줍니다. (생략)
DataTable tblProducts = new  DataTable();
// 원본 객체에서 복사할 객체입니다.
DataTable tblProductsCopy = new DataTable();
// 복사용 객체에 스키마, 관계, 제약 조건들을 원본 객체와 같게 적용 합니다.
tblProductsCopy = tblProducts.Clone();
for (i=0; i<=4;++i)
{
    // 원본 객체에서 복사를 실행합니다.
    tblProductsCopy.ImportRow(tblProducts.Rows[i]);
}
 
 
//COPY 사용
DataTable DataTable1 = new DataTable();
DataTable DataTable2 = new DataTable();
// DataTable의 Copy 메서드를 이용하여 복사한다.
DataTable2 = DataTable1.Copy();
 
 
// Select 메서드를 사용하여 결과값을 복사한다.
foreach (DataRow MyDataRow in DataTable1.Select("Region = 'WA'"))
{
    DataTable2.ImportRow(MyDataRow);
}
// DataView의 필터 기능을 사용하여 결과값을 복사한다.
DataView1 = DataTable1.DefaultView;
DataView1.RowFilter = "Region = 'WA'";
for (int i = 0; i <= DataView1.Count - 1; ++i)
{
    DataTable2.ImportRow(DataView1[I].Row);
}

 

Post Author: 김 키티

답글 남기기

이메일 주소를 발행하지 않을 것입니다. 필수 항목은 *(으)로 표시합니다