怎么循环向list添加数据

时间:2021-08-27 19:36:34
string sb = String.Empty;
        List<Sell_Product> list=new List<Sell_Product>();
        //一条一条的显示,循环
        for (int i = 0; i <this.GridView1.Rows.Count; i++)
        {
        //获取复选框的值
            CheckBox cb = (GridView1.Rows[i].FindControl("Chk1")) as CheckBox;
        //判断为true的话就是选中了
            if (cb.Checked == true)
            {
               
                sb = (GridView1.Rows[i].FindControl("Label1") as Label).Text;

                    // 调用方法,下面有这个方法的ChangeCatagory(sb)
                         //根据编码获得数据一条一条想list添加
                            list = new add_product_BLL().Sel_Product_id(sb);

               
            }
        }
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();

这里面。调用了Sel_Product_id这个方法是查到数据了,但是一条一条向list中添加的时候,为什么会一条一条给覆盖了。
我该如何向list中添加我查到得不同数据?

4 个解决方案

#1


list = new add_product_BLL().Sel_Product_id(sb); 
改为list.Add(new add_product_BLL().Sel_Product_id(sb))

#2


编译错误 
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 

编译器错误消息: CS1502: 与“System.Collections.Generic.List<DXCerp.Model.erp.Sell_Product>.Add(DXCerp.Model.erp.Sell_Product)”最匹配的重载方法具有一些无效参数

源错误:

 

行 251:                    // 调用方法,下面有这个方法的ChangeCatagory(sb)
行 252:
行 253:                li.Add(new add_product_BLL().Sel_Product_id(sb));
行 254:                //list.Add(li);
行 255:
 

#3


for (int i = 0; i <this.GridView1.Rows.Count; i++) 
        { 
        //获取复选框的值 
            CheckBox cb = (GridView1.Rows[i].FindControl("Chk1")) as CheckBox; 
        //判断为true的话就是选中了 
            if (cb.Checked == true) 
            { 
              
                sb = (GridView1.Rows[i].FindControl("Label1") as Label).Text; 

                    // 调用方法,下面有这个方法的ChangeCatagory(sb) 
                        //根据编码获得数据一条一条想list添加 
                            list = new add_product_BLL().Sel_Product_id(sb); 
                         《在上面NEW 过一次之后就不要每次都重新初始化了,判断一下如果list不为空的话,直接用list.Add(new add_product_BLL().Sel_Product_id(sb)) 就可以了》      
                                
            } 
        } 

#4


我试试先。这个问题,我跳过了,完成后面的先,谢你了。

#1


list = new add_product_BLL().Sel_Product_id(sb); 
改为list.Add(new add_product_BLL().Sel_Product_id(sb))

#2


编译错误 
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 

编译器错误消息: CS1502: 与“System.Collections.Generic.List<DXCerp.Model.erp.Sell_Product>.Add(DXCerp.Model.erp.Sell_Product)”最匹配的重载方法具有一些无效参数

源错误:

 

行 251:                    // 调用方法,下面有这个方法的ChangeCatagory(sb)
行 252:
行 253:                li.Add(new add_product_BLL().Sel_Product_id(sb));
行 254:                //list.Add(li);
行 255:
 

#3


for (int i = 0; i <this.GridView1.Rows.Count; i++) 
        { 
        //获取复选框的值 
            CheckBox cb = (GridView1.Rows[i].FindControl("Chk1")) as CheckBox; 
        //判断为true的话就是选中了 
            if (cb.Checked == true) 
            { 
              
                sb = (GridView1.Rows[i].FindControl("Label1") as Label).Text; 

                    // 调用方法,下面有这个方法的ChangeCatagory(sb) 
                        //根据编码获得数据一条一条想list添加 
                            list = new add_product_BLL().Sel_Product_id(sb); 
                         《在上面NEW 过一次之后就不要每次都重新初始化了,判断一下如果list不为空的话,直接用list.Add(new add_product_BLL().Sel_Product_id(sb)) 就可以了》      
                                
            } 
        } 

#4


我试试先。这个问题,我跳过了,完成后面的先,谢你了。