首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求好手 给 这段代码 优化一下

2013-04-02 
求高手 给 这段代码 优化一下DataTable dtPay dgvPay.DataSource as DataTableDataTable dtReceive d

求高手 给 这段代码 优化一下


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"];
                }                
            }


是这个意思不?

热点排行