如何使用gridview中的复选框?

时间:2013-04-08 11:03:58

标签: c# asp.net

我有一个asp.net表单,使用网格视图显示Oracle数据库中的用户详细信息。我甚至设法使用以下模板在网格视图中获取一个复选框字段..

    <asp:TemplateField>
    <ItemTemplate>
      <asp:CheckBox runat="server" ID="chck"/>
    </ItemTemplate>
    </asp:TemplateField>

我希望管理员能够使用复选框选择多个条目并对它们执行特定操作。我尝试使用以下

进行删除操作
for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            if (Convert.ToBoolean(GridView1.Rows[i].Cells[1].Value == true))
                GridView1.Rows.RemoveAt[i];
        }

但是,这显示错误。显然,每行的每个复选框都必须有自己唯一的索引。我怎么做到的? 任何形式的帮助将不胜感激。 Thanx:)

3 个答案:

答案 0 :(得分:0)

您需要使用findControl

在gridview行中找到控件
for (int i = 0; i < GridView1.Rows.Count; i++)
{
     CheckBox cb1 = (CheckBox)GridView1.Rows[i].FindControls("chck");
        if(cb1.Checked)
            GridView1.Rows.RemoveAt[i];
}

答案 1 :(得分:0)

使用find control获取复选框:

for (int i = 0; i < GridView1.Rows.Count; i++)
   {

        CheckBox chck = (CheckBox)GridView1.Rows[i].FindControl("chck");

       if (chck  != null)
       {
         if (chck.Checked)
         {
            GridView1.Rows.RemoveAt[i];
         }
       }
  }

答案 2 :(得分:0)

这是Aspx FIle:

  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    BackColor="White" BorderColor="#336699" BorderStyle="Solid" BorderWidth="1px"  
    CellPadding="0" CellSpacing="0" DataKeyNames="CategoryID" Font-Size="10"
    Font-Names="Arial" GridLines="Vertical" Width="40%">

            <Columns>            
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkStatus" runat="server"
                            AutoPostBack="true" OnCheckedChanged="chkStatus_OnCheckedChanged"
                            Checked='<%# Convert.ToBoolean(Eval("Approved")) %>'
                            Text='<%# Eval("Approved").ToString().Equals("True") ? " Approved " : " Not Approved " %>' />
                    </ItemTemplate>                   
                </asp:TemplateField>

                <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />                   
                <asp:BoundField DataField="CategoryName" HeaderText="CategoryName"  />
            </Columns>

    <HeaderStyle BackColor="#336699" ForeColor="White" Height="20" />

</asp:GridView>

这是CS文件:

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    LoadData();
  }
}

private void LoadData()
{
  string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;";
  string query = @"SELECT CategoryID, CategoryName, Approved FROM Categories";

  SqlDataAdapter da = new SqlDataAdapter(query, constr);
  DataTable table = new DataTable();
  da.Fill(table);

  GridView1.DataSource = table;
  GridView1.DataBind();
}

public void chkStatus_OnCheckedChanged(object sender, EventArgs e)
{
    CheckBox chkStatus = (CheckBox)sender;
    GridViewRow row = (GridViewRow)chkStatus.NamingContainer;


    string cid = row.Cells[1].Text;
    bool status = chkStatus.Checked;


    string constr = @"Server=.\SQLEXPRESS;Database=TestDB;uid=waqas;pwd=sql;";
    string query = "UPDATE Categories SET Approved = @Approved WHERE CategoryID = @CategoryID";

    SqlConnection con = new SqlConnection(constr);
    SqlCommand com = new SqlCommand(query, con);


    com.Parameters.Add("@Approved", SqlDbType.Bit).Value = status;
    com.Parameters.Add("@CategoryID", SqlDbType.Int).Value = cid;


    con.Open();
    com.ExecuteNonQuery();
    con.Close();

    LoadData();
}