使用复选框删除gridview中的多行
本文关键字:gridview 复选框 删除 | 更新日期: 2023-09-27 18:16:28
我已经使用以下代码来执行我的查询删除多个值在gridview使用复选框,但当我执行以下它说checkbox1。Checked是假的,但是我检查了。它不会删除我选择的值
这是我的代码和脚本部分
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grd_bnd();
}
}
private void grd_bnd()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
SqlCommand cmd = new SqlCommand("select * from student", con);
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
con.Close();
}
protected void Button3_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkbox1 = (CheckBox)row.FindControl("checkboxdelete");
if (checkbox1.Checked)
{
int rollno = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value.ToString());
//CheckBox checkbox1 = (CheckBox)row.FindControl("checkbox1");
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
SqlCommand cmd = new SqlCommand("delete from student where rollno = @rollno ", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@rollno", SqlDbType.Int).Value = rollno.ToString();
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Dispose();
}
}
grd_bnd();
}
}
,这里是脚本
<asp:GridView ID="GridView1" runat="server" DataKeyNames="rollno" AllowSorting="true" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" >
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="checkboxdelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblname" runat ="server" Text='<%#Eval("name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Roll No.">
<ItemTemplate>
<asp:Label ID="lblrollno" runat ="server" Text='<%#Eval("rollno") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Batch">
<ItemTemplate>
<asp:Label ID="lblbatch" runat ="server" Text='<%#Eval("batch") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Course">
<ItemTemplate>
<asp:Label ID="lblcourse" runat ="server" Text='<%#Eval("course") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</gridview>
<asp:Button ID="Button3" runat="server" Font-Bold="True" Text="Delete Selected" ForeColor="#000066" OnClick="Button3_Click" />
<asp:Label ID="Label1" runat="server" ForeColor="#666666"></asp:Label>
试试这个
for (int i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox checkboxdelete = ((CheckBox)GridView1.Rows[i].FindControl("checkboxdelete"));
if (checkboxdelete.Checked == true)
{
Label lblrollno = ((Label)GridView1.Rows[i].FindControl("lblrollno"));
int rolNo = Convert.ToInt32(lblrollno.Text);
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
SqlCommand cmd = new SqlCommand("delete from student where rollno = @rollno ", con);
cmd.CommandType = CommandType.Text;
SqlParameter param = new SqlParameter("@rollno", SqlDbType.Int);
param.Value = rolNo;
cmd.Parameters.Add(param);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
cmd.Dispose();
}
}
为我工作的是下面的代码按钮点击事件:
foreach (GridViewRow row in GridView1.Rows)
{
CheckBox checkboxdelete = (CheckBox)row.FindControl("checkboxdelete");
if (checkboxdelete.Checked == true)
{
using (SqlConnection scon = new SqlConnection(ConfigurationManager.ConnectionStrings["CS"].ConnectionString))
{
scon.Open();
Label LblId = (Label)row.FindControl("rollno");
int rollno = Convert.ToInt32(LblId.Text);
SqlCommand scmd = new SqlCommand("delete from student where rollno = @rollno", scon);
scmd.Parameters.AddWithValue("@rollno", rollno);
scmd.ExecuteNonQuery();
scon.Close();
BindGrid();
Label4.Text = "";
}
}
else
{
Label4.Text = "No record is selected to delete !";
Label4.ForeColor = System.Drawing.Color.Red;
}
}