需求:在gridview中添加一列radio 并能将其选中的行删除!

解决方案 »

  1.   

    foreach (GridViewRow gr in this.Gridview1.Rows)  
      {  
      if (((RadioButton)gr.FindControl("RadioButton1")).Checked)  
      {}  
      }  
      

  2.   

    楼上正解,如果是要点击radio 的时候删除,可以给RadioButton1加个事件
      

  3.   


     <asp:TemplateField HeaderText="选中状态">
          <HeaderTemplate>
               <asp:CheckBox ID="chkAll" OnCheckedChanged="SelectAll" runat="server" Text="全选" AutoPostBack="true"/>
          </HeaderTemplate>
          <ItemTemplate>
                    <asp:CheckBox ID="cb" runat="server"/>
          </ItemTemplate>
    </asp:TemplateField> //删除选中项
     for (int i = 0; i < Gridview1.Rows.Count; i++)
     {
          CheckBox _cb = (CheckBox)Gridview1.Rows[i].FindControl("cb");
          if (_cb.Checked)
          {
                 int _no = int.Parse(Gridview1.DataKeys[i].Value.ToString());
                 //执行删除
          }    
      }    //全选/反选
        protected void SelectAll(object sender, EventArgs e)
        {
            bool isChecked = ((CheckBox)(Gridview1.HeaderRow.Cells[0].FindControl("chkAll"))).Checked;
            foreach (GridViewRow gvRow in Gridview1.Rows)
            {
                ((CheckBox)(Gridview1.Cells[0].FindControl("cb"))).Checked = isChecked;
            }
        }
      

  4.   

    上上楼 我是radiobutton 而不是checkbox
      

  5.   

    foreach (GridViewRow gr in this.Gridview1.Rows)   
      {   
      if (((RadioButton)gr.FindControl("RadioButton1")).Checked)   
      {
     sqlConnection con = new SqlConnection("server=local;database=Northwind;uid=sa;pwd=123");
            con.Open();
            string deleteid = GridView1.DataKeys[e.RowIndex].Value.ToString();
            string cmdtext = "delete from orders where orderid='" + deleteid + "'";
            SqlCommand cmd = new SqlCommand(cmdtext, con);
            try
            {
                cmd.ExecuteNonQuery();
                Response.Write("<script>alert('删除成功');</script>");
            }
            catch
            {
                return;
            }
            banddelete();
            con.Close();
     }  
      }