如何使用GridView中的复选框插入数据? [英] how to insert data using checkbox in gridview.?

查看:61
本文介绍了如何使用GridView中的复选框插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设,
我有一个带有复选框的网格视图.
当我选中多个复选框时,这些值存储在数据库中...

我有带有选中复选框的gridview,并且我想在数据库中插入该选中的行. 如果我检查了一行,那么它将在数据库中插入该行..如果我检查了两行,那么它将在数据库中插入这两行...

suppose,
i have one grid view with check box.
when i checked multiple check box those values are store in database...

i have gridview with checked check box and i want to insert that checked row in database..
if i checked one row then it will insert that row in the database.. if i checked two rows then it will insert those two rows in the database...

推荐答案

您好,
试试这个
Hi ,
Try this
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if (((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox1")) is CheckBox)
        {
            using (
                SqlConnection con =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["testConnectionString"].ConnectionString))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand("UPDATE dbo.cate  SET cate = @cate , chk = @chk  where id  = @id", con);
                cmd.Parameters.AddWithValue("@cate",
                                            ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text);

                cmd.Parameters.AddWithValue("@chk",
                                            ((CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBox1")).Checked);

                cmd.Parameters.AddWithValue("@id",
                                            ((Label)GridView1.Rows[e.RowIndex].FindControl("Label1")).Text);

                cmd.CommandType = System.Data.CommandType.Text;
                cmd.ExecuteNonQuery();
                cmd.Dispose();

                GridView1.EditIndex = -1;
                bind();
            }
        }



最好的问候
米特瓦里(M.Mitwalli)



Best Regards
M.Mitwalli


这篇关于如何使用GridView中的复选框插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆