通过外部按钮,如何将gridview选中的内容批量插入数据库 ,不含不符合未选择的

左手青春右手年华 2016-01-27 11:39:11

按钮在模板里单条选中插入的情况已经OK。
按钮不在模板里的情况,如截图所示功能。 for循环行不通,会把展示的内容都插入。
...全文
232 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 2 楼 ymq_2012 的回复:
点击保存按钮的时候去循环处理gridview中选中的数据,根据前面的checkbox是否选中来判断数据是否插入
正解 CheckBox cb; for (int i = 0; i < GridView1.Rows.Count; i++) { cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); if (cb.Checked == true) { //FindControl value string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString(); …… //Exec Sql } else { cb.Checked = false; //Do nothing }
  • 打赏
  • 举报
回复
大家给个思路都可以,谢谢
  • 打赏
  • 举报
回复
引用 4 楼 jxetv 的回复:
[quote=引用 2 楼 ymq_2012 的回复:] 点击保存按钮的时候去循环处理gridview中选中的数据,根据前面的checkbox是否选中来判断数据是否插入
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string storageid = this.DropDownList1.SelectedValue;
        string yhr = this.DropYangHuRen.SelectedItem.Text;
        string yhdt = this.txtyanghurq.Text;
        string cmd = e.CommandName;
        int i = Convert.ToInt32(e.CommandArgument);
     
        if (cmd == "save")
        {
          

            string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
            string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
            string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
            string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
            string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
            string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
            string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
            string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
            string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
            string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
            string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
            string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
            string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
            string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
            string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
            string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();

            string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
            string sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','" + pname + "','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" + ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" + baozhuang + "','" + zhiliang + "','" + pcuoshi + "','" + jieguo + "','" + pbeizhu + "','" + yhr + "','" + yhdt + "')";
            Class1.ExecSql(sql);
            this.WriteLog("添加器械养护记录成功");                    
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);
             
        }
        
            



    }//RowCommand结尾
上述这段代码单独对当前行的插入操作没有任何问题,因为linkbutton在模板里面。 现在button1在gridview之外,所以就不好弄。因为这个不好控制所选行的索引了。具体这部分代码如下:
  protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.GridView1.Rows.Count <= 0)
            {
                this.lblMessage.Visible = true;       
                this.lblMessage.Text = "请选择有商品的仓库";
                return;
            }
            else
            {
                this.lblMessage.Visible = false;              
            }

            string storageid = this.DropDownList1.SelectedValue;
            string yhr = this.DropYangHuRen.SelectedItem.Text;
            string yhdt = this.txtyanghurq.Text;
            string str_check = "(";
            string sql;
            CheckBox cb;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                cb.Checked = true;

                if (cb.Checked == true)
                {
                   
                    str_check = str_check + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";

                    // str_check = str_check.Substring(0, str_check.Length - 1) + ")";

                    string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
                    string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
                    string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
                    string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
                    string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
                    string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
                    string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
                    string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
                    string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
                    string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
                    string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
                    string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
                    string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
                    string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
                    string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
                    string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();

                    //去掉最后的逗号,并且加上右手号  ★示例★
                   // str_check = str_check.Substring(0, str_check.Length - 1) + ")";                  
                    //if noexists (select * from table1 where mid='aa') INSERT INTO table1(mid,name,msg) VALUES('aa','bb','cc')
                    string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
                    sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','"+pname+"','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" +ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" +baozhuang + "','" + zhiliang + "','" + pcuoshi + "','"+jieguo+"','"+pbeizhu+"','"+yhr+"','"+yhdt+"')";
                    Class1.ExecSql(sql);
                 
                    }
                    else
                    {
                        cb.Checked = false;

                    }
            }

            this.WriteLog("添加器械养护记录成功");                   
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);

        }
        catch(Exception exc)
        {
            this.lblMessage.Visible = true;
            this.lblMessage.Text = exc.ToString();
        }
    }
}
[/quote] cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1"); cb.Checked = true; 你这块为什么直接复制为True,这样的话你的条件一直是成立的,把cb.Checked=true取消掉
  • 打赏
  • 举报
回复
引用 2 楼 ymq_2012 的回复:
点击保存按钮的时候去循环处理gridview中选中的数据,根据前面的checkbox是否选中来判断数据是否插入
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string storageid = this.DropDownList1.SelectedValue;
        string yhr = this.DropYangHuRen.SelectedItem.Text;
        string yhdt = this.txtyanghurq.Text;
        string cmd = e.CommandName;
        int i = Convert.ToInt32(e.CommandArgument);
     
        if (cmd == "save")
        {
          

            string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
            string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
            string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
            string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
            string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
            string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
            string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
            string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
            string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
            string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
            string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
            string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
            string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
            string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
            string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
            string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();

            string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
            string sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','" + pname + "','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" + ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" + baozhuang + "','" + zhiliang + "','" + pcuoshi + "','" + jieguo + "','" + pbeizhu + "','" + yhr + "','" + yhdt + "')";
            Class1.ExecSql(sql);
            this.WriteLog("添加器械养护记录成功");                    
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);
             
        }
        
            



    }//RowCommand结尾
上述这段代码单独对当前行的插入操作没有任何问题,因为linkbutton在模板里面。 现在button1在gridview之外,所以就不好弄。因为这个不好控制所选行的索引了。具体这部分代码如下:
  protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.GridView1.Rows.Count <= 0)
            {
                this.lblMessage.Visible = true;       
                this.lblMessage.Text = "请选择有商品的仓库";
                return;
            }
            else
            {
                this.lblMessage.Visible = false;              
            }

            string storageid = this.DropDownList1.SelectedValue;
            string yhr = this.DropYangHuRen.SelectedItem.Text;
            string yhdt = this.txtyanghurq.Text;
            string str_check = "(";
            string sql;
            CheckBox cb;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                cb = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1");
                cb.Checked = true;

                if (cb.Checked == true)
                {
                   
                    str_check = str_check + Convert.ToInt32(GridView1.DataKeys[i].Value) + ",";

                    // str_check = str_check.Substring(0, str_check.Length - 1) + ")";

                    string pbh = ((Label)GridView1.Rows[i].FindControl("labbm")).Text.ToString();
                    string pname = ((Label)GridView1.Rows[i].FindControl("labname")).Text.ToString();
                    string ppihao = ((Label)GridView1.Rows[i].FindControl("labpihao")).Text.ToString();
                    string pzhuce = ((Label)GridView1.Rows[i].FindControl("labzhuce")).Text.ToString();
                    string pxiaoqi = ((HiddenField)GridView1.Rows[i].FindControl("HiddenField1")).Value.ToString();
                    string pnum = ((Label)GridView1.Rows[i].FindControl("labnumber")).Text.ToString();
                    string punitname = ((Label)GridView1.Rows[i].FindControl("labdanwei")).Text.ToString();
                    string pspec = ((Label)GridView1.Rows[i].FindControl("labspec")).Text.ToString();
                    string pcompany = ((Label)GridView1.Rows[i].FindControl("labcompany")).Text.ToString();
                    string psupplier = ((Label)GridView1.Rows[i].FindControl("labsupplier")).Text.ToString();
                    string pcuoshi = ((TextBox)GridView1.Rows[i].FindControl("TextBox1")).Text.ToString();
                    string pbeizhu = ((TextBox)GridView1.Rows[i].FindControl("TextBox2")).Text.ToString();
                    string waiguan = ((DropDownList)GridView1.Rows[i].FindControl("DropWaiGuan")).SelectedItem.Text.ToString();
                    string baozhuang = ((DropDownList)GridView1.Rows[i].FindControl("DropBaoZhuang")).SelectedItem.Text.ToString();
                    string zhiliang = ((DropDownList)GridView1.Rows[i].FindControl("DropZhiLiang")).SelectedItem.Text.ToString();
                    string jieguo = ((DropDownList)GridView1.Rows[i].FindControl("DropResult")).SelectedItem.Text.ToString();

                    //去掉最后的逗号,并且加上右手号  ★示例★
                   // str_check = str_check.Substring(0, str_check.Length - 1) + ")";                  
                    //if noexists (select * from table1 where mid='aa') INSERT INTO table1(mid,name,msg) VALUES('aa','bb','cc')
                    string sqlfield = "[storageid],[pcode],[p_title],[p_reg],[p_spec],[p_manufactor],[p_unit],[p_count],[p_pihao],[p_xiaoqi],[p_supplier],[waiguan],[baozhuang],[zhiliang],[cuoshi],[result],[beizhu],[yanghu_emp],[yanghu_dt]";
                    sql = "insert into Gsp_YangHu(" + sqlfield + ")values('" + storageid + "','" + pbh + "','"+pname+"','" + pzhuce + "','" + pspec + "','" + pcompany + "','" + punitname + "','" + pnum + "','" +ppihao + "','" + pxiaoqi + "','" + psupplier + "','" + waiguan + "','" +baozhuang + "','" + zhiliang + "','" + pcuoshi + "','"+jieguo+"','"+pbeizhu+"','"+yhr+"','"+yhdt+"')";
                    Class1.ExecSql(sql);
                 
                    }
                    else
                    {
                        cb.Checked = false;

                    }
            }

            this.WriteLog("添加器械养护记录成功");                   
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, this.GetType(), "click", "alert('添加成功,退出请点击下方【关闭该窗口】')", true);

        }
        catch(Exception exc)
        {
            this.lblMessage.Visible = true;
            this.lblMessage.Text = exc.ToString();
        }
    }
}
  • 打赏
  • 举报
回复
你的for循环处理的不对
  • 打赏
  • 举报
回复
点击保存按钮的时候去循环处理gridview中选中的数据,根据前面的checkbox是否选中来判断数据是否插入

62,050

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧