复制DataRow,小技巧

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        /// <summary>
        
/// 包装单位选择发生变化
       
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        protected void ddlUnitname_SelectedIndexChanged(object sender, EventArgs e)
        {
            
//选择不同的包装入库
            DataSet ds = (DataSet)ViewState["dbMateriels"];
            DataTable tmp1dt 
= ds.Tables[0];
            DataTable tardt 
= tmp1dt.Clone();
            
for (int i = 0; i < tmp1dt.Rows.Count; i++)
            {
                
for (int j = 0; j < gvMaterialDetail.Rows.Count; j++)
                {
                    
if (tmp1dt.Rows[i]["tmpid"].ToString() == gvMaterialDetail.DataKeys[j]["tmpid"].ToString())
                    {
                        DropDownList ddl 
= (DropDownList)gvMaterialDetail.Rows[j].FindControl("ddlUnitname");
                        Label lbl 
= (Label)gvMaterialDetail.Rows[j].FindControl("lblUnitname");
                        DataRow dr 
= tardt.NewRow();
                        dr 
= tmp1dt.Rows[i];
                        
if (ddl.SelectedValue.ToString() != null && ddl.SelectedValue.ToString() != "")
                        {
                            dr[
"unit"= ddl.SelectedValue.ToString();
                        }
                        tardt.Rows.Add(dr.ItemArray);
                    }
                }
            }
            gvMaterialDetail.DataSource 
= tardt;
            
this.gvMaterialDetail.DataKeyNames = new string[] { "unit""recordid""tmpid","billleftnumb","numb" };
            gvMaterialDetail.DataBind();
        }

 

        DataRow dr = tardt.NewRow();
                        dr = tmp1dt.Rows[i];
                        if (ddl.SelectedValue.ToString() != null && ddl.SelectedValue.ToString() != "")
                        {
                            dr["unit"] = ddl.SelectedValue.ToString();
                        }

                        tardt.Rows.Add(dr.ItemArray);

 

如果tardt.Rows.Add(dr)

就会产生该列已属于另一个表的错误

转载于:https://www.cnblogs.com/pchgo/archive/2009/01/06/1370021.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值