使用CheckBox将数据插入GridView

时间:2011-10-31 10:20:04

标签: c# asp.net asp.net-controls

我有一个GridView,我在其所有单元格中放置了复选框。我想做以下事情: 如果用户选中了复选框,则表示是,将存储在数据库中 否则,如果用户未选中该复选框,则表示“否”,无需向数据库发送任何内容。

我知道我现在需要识别每个已选中的复选框,并知道此复选框的复选框位于其下方。

有关如何做到这一点的任何想法?有人能给我这样做的基本代码吗?

2 个答案:

答案 0 :(得分:0)

我使用类似的东西:

    foreach (GridViewRow row in gvYourGridView.Rows)
    {
        CheckBox ck = ((CheckBox)row.FindControl("YourCheckBoxName"));

        if (ck.Checked)
        {
            //If checked is true, update database.
        }
    }

答案 1 :(得分:0)

            int counter = 0;
            foreach (GridViewRow rowitem in gvYourGridView.Rows)
            {
                if (((CheckBox)rowitem.Cells[0].FindControl("chk")).Checked == true)//i consider that the check box is in the first column index ---> 0
                {
                    counter++;
                }
            }
            /////////////////////////////////////////////////////////////
            if(counter == 0) //no checks
            {

             //show some message box to clarify that no row has been selected.

            }
            /////////////////////////////////////////////////////////////
            if (counter == 1) //one check
            {
                //Do something
            }
            /////////////////////////////////////////////////////////////
            if (counter > 1) //more than one check
            {
               //Do something    
            }

            gvYourGridView.DataBind();