使用复选框从GridView控件中选择单个行 [英] selecting individual rows from gridview using check box

查看:156
本文介绍了使用复选框从GridView控件中选择单个行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含模板字段里面一个复选框一个gridview。我想检索其中复选框被选中,所以我可以通过数据库更新它们的行的值。这里是我的code:

I have a gridview that contains a check box inside the template field. I want to retrieve the values of the rows where the check box is checked so I can update them through the database. Here's my code:

    protected void approve_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (((CheckBox)GridView1.Rows[i].FindControl("Select")).Checked) 
            {

//我觉得如果循环发现选中的复选框,将执行以下到该行:

//I thought if the loop finds a checked check box, it will execute the following to that row:

                con.Open();

                string approve = "update table set status ='Approved' where ID=" + GridView1.Rows[i].Cells[1].Text + "";

                SqlCommand scmapprove = new SqlCommand(approve, con);

                scmapprove.ExecuteNonQuery();

                view();

                con.Close();

            }

                            }

然而,它似乎并没有工作。比如我查五行从表中,它只更新的第一行。我应该怎么办?

However, it doesn't seem to work. For example I checked five rows from the table, It only updates the first row. What should I do?

推荐答案

您正在寻找一个检查一行之后重新绑定GridView的。绑定完成所有更新操作后:

You are rebinding the Gridview after finding a checked row. Bind it after completing all update operations:

protected void approve_Click(object sender, EventArgs e)
{
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        if (((CheckBox)GridView1.Rows[i].FindControl("Select")).Checked) 
        {
            //I thought if the loop finds a checked check box, it will execute the following to that row:
            con.Open();
            string approve = "update table set status ='Approved' where ID=" + GridView1.Rows[i].Cells[1].Text + "";

            SqlCommand scmapprove = new SqlCommand(approve, con);
            scmapprove.ExecuteNonQuery();

            //view(); //Donot rebind the gridview now.
            con.Close();
        }
    }

    view();
}

这篇关于使用复选框从GridView控件中选择单个行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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