求高手 给 这段代码 优化一下
DataTable dtPay = dgvPay.DataSource as DataTable;
DataTable dtReceive = dgvReceive.DataSource as DataTable;
if (dtPay != null)
{
if (dtReceive.Rows.Count == dtPay.Rows.Count)
{
for (int i = 0; i < dtPay.Rows.Count; i++)
{
dtReceive.Rows[i]["IsOriginal"] = dtPay.Rows[i]["IsOriginal"];
}
}
else if (dtPay.Rows.Count > dtReceive.Rows.Count)
{
int j = 0;
for (int i = 0; i < dtPay.Rows.Count; i++)
{
for (; j < dtReceive.Rows.Count; )
{
dtReceive.Rows[j]["IsOriginal"] = dtPay.Rows[i]["IsOriginal"];
j++;
break;
}
}
}
else if (dtPay.Rows.Count < dtReceive.Rows.Count)
{
int j = 0;
for (int i = 0; i < dtReceive.Rows.Count; i++)
{
for (; j < dtPay.Rows.Count; )
{
dtReceive.Rows[i]["IsOriginal"] = dtPay.Rows[j]["IsOriginal"];
j++;
break;
}
}
}
}
谢拉
[解决办法]
DataTable dtPay = dgvPay.DataSource as DataTable;
DataTable dtReceive = dgvReceive.DataSource as DataTable;
if (dtPay != null)
{
int length = dtReceive.Rows.Count > dtPay.Rows.Count ? dtPay.Rows.Count : dtReceive.Rows.Count;
for (int i = 0; i < length; i++)
{
dtReceive.Rows[i]["IsOriginal"] = dtPay.Rows[i]["IsOriginal"];
}
}
是这个意思不?