copy DataRow from one DataTable to another?
I have a very 开发者_运维技巧specific requirement. I've two datatables (both of same schema) one table(aqTable) with 2 rows and another (rapidsTable) with 5 rows. I need to check if those 2 rows in aqTable are present in those 5 rows of rapidsTable or not, if those two rows are present in rapidsTable then copy remaining (difference) 3 rows to aqTable.
I tried it in this way, but when it loops over for 3rd row throws an exception "There is no row a position2" because there are no more than 2 rows in one of the table.
if (rapidsTable.Rows.Count > aqTable.Rows.Count)
{
try
{
int aqRow = 0; rows = 0;
int rCount = rapidsTable.Rows.Count;
for (int rRow = 0; rRow < rCount; rRow++)
{
if (aqTable.Rows.Count == 0)
{
DataRow row = aqTable.NewRow();
row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
aqTable.Rows.Add(row); rows++;
}
else
{
string str = aqTable.Rows[aqRow].ToString();
if (aqTable.Rows.ToString() == null || aqTable.Rows.ToString() == "")
{ }
if ((aqTable.Rows[aqRow][1].ToString() == rapidsTable.Rows[rRow][1].ToString()) &&
(aqTable.Rows[aqRow][2].ToString() == rapidsTable.Rows[rRow][2].ToString()) &&
(aqTable.Rows[aqRow][3].ToString() == rapidsTable.Rows[rRow][3].ToString()))
{ }
else
{
DataRow row = aqTable.NewRow();
row.ItemArray = rapidsTable.Rows[aqRow].ItemArray;
rowStr += rapidsTable.Rows[rRow][0].ToString() + ",";
aqTable.Rows.Add(row); rRow = rRow - 1; rows++;
}
}
aqRow++;
}
catch (Exception ex)
{ throw new Exception("R>AQ Copy: " + ex.Message); }
Is there a way to solve this for that exception. I tried many different ways from many different blogs. Found this blog has some best examples, thought to share my problem.
I think this is what you are looking for
http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q308909
精彩评论