使用gridview中的复选框获取删除的行数

时间:2014-08-05 08:51:00

标签: c# asp.net gridview

在这里,我希望我已经检查并删除了标签中的颜色

我使用以下代码执行查询,使用复选框

删除gridview中的多个值

这是我的代码和脚本部分

 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>

我想获得上面标签lable1中删除的实体数量。

2 个答案:

答案 0 :(得分:2)

您可以使用ExecuteNonQuery的返回值:

int numDeleted = cmd.ExecuteNonQuery();

MSDN 返回受影响的行数。

如果您希望总行数使用变量:

int totalNumDeleted = 0;
foreach (GridViewRow row in GridView1.Rows)
{
    // ...
    int numDeleted = cmd.ExecuteNonQuery();
    totalNumDeleted += numDeleted;
}
Label1.Text = string.Format("{0} students were deleted successfully.", totalNumDeleted);

答案 1 :(得分:2)

只需在循环之前添加计数

protected void Button3_Click(object sender, EventArgs e)
{
    int rows = 0;

    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();
            rows += cmd.ExecuteNonQuery(); //Can return 0 if no rows were deleted!
            con.Close();
            cmd.Dispose();
        }
    }
    grd_bnd();
    MessageBox.Show(String.Format("Rows affected {0}", rows);
}