C# 我想使用复选框删除datagridview上的多个数据

C# 我想使用复选框删除datagridview上的多个数据,c#,asp.net,C#,Asp.net,尝试下面的代码 private void button3_Click(object sender, EventArgs e) { foreach (DataGridViewRow item in dataGridView1.Rows) { if (bool.Parse(item.Cells[0].Value.ToString())) { MessageBox.Show("Selected Rows : " + i

尝试下面的代码

private void button3_Click(object sender, EventArgs e)
{
    foreach (DataGridViewRow item in dataGridView1.Rows)
    {
        if (bool.Parse(item.Cells[0].Value.ToString()))
        {
                MessageBox.Show("Selected Rows  :  " + item.Cells[0].RowIndex.ToString());
        }
    }
}
如果您想了解更多信息,请参阅下面的链接


单击按钮3时,将执行以下事件处理程序。它首先使用LINQ查询从DataGridView获取选中(选定)行,然后显示确认消息框

如果用户单击“是”按钮,则在选定(选中)行上执行循环,并使用Id字段从数据库表中逐个删除记录

注意:这里的数据库查询是为了让OP了解他是如何实现的,因为OP在他的帖子中没有提到任何东西,这将是一种更好的解释方式

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow gvrow in gvDetails.Rows)
        {
            //Finiding checkbox control in gridview for particular row using below syntax
            CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkSelect");
            //Condition to check checkbox selected or not
            if (chkdelete.Checked)
            {
                //Getting UserId of particular row using datakey value
                int usrid = Convert.ToInt32(gvDetails.DataKeys[gvrow.RowIndex].Value);
                using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("delete from UserDetails where UserId=" + usrid, con);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        BindUserDetails();
    }
private void按钮3\u单击(对象发送者,事件参数e)
{
列出selectedRows=(来自dataGridView1.Rows.Cast()中的行)
其中Convert.ToBoolean(row.Cells[“checkBoxColumn”].Value)=true
选择row.ToList();
if(MessageBox.Show(string.Format(“是否要删除{0}行?”,selectedRows.Count),“确认”,MessageBoxButtons.YesNo)=DialogResult.Yes)
{
foreach(选定行中的DataGridViewRow行)
{
使用(SqlConnection con=newsqlconnection(ConnectionString))
{
使用(SqlCommand cmd=newsqlcommand(“从CustomerId=@CustomerId的客户中删除”,con))
{
cmd.CommandType=CommandType.Text;
cmd.Parameters.AddWithValue(“@CustomerId”,row.Cells[“CustomerId”].Value);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
这个.BindGrid();
}
}

当您删除一行时,首先删除该行,datagrid行大小的新值总计为-1,因此我使用I--。为什么要在条件“if(i 您可以使用try-catch而不是conditional

祝你好运

   private void button3_Click(object sender, EventArgs e)
    {
        List<DataGridViewRow> selectedRows = (from row in dataGridView1.Rows.Cast<DataGridViewRow>()
                                                where Convert.ToBoolean(row.Cells["checkBoxColumn"].Value) == true
                                                select row).ToList();
        if (MessageBox.Show(string.Format("Do you want to delete {0} rows?", selectedRows.Count), "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
            foreach (DataGridViewRow row in selectedRows)
            {
                using (SqlConnection con = new SqlConnection(ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand("DELETE FROM Customers WHERE CustomerId = @CustomerId", con))
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.Parameters.AddWithValue("@CustomerId", row.Cells["CustomerId"].Value);
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }

            this.BindGrid();
        }
    }
private void按钮3\u单击(对象发送者,事件参数e)
{

对于(int i=0;i请尝试此代码,它对我有效:

    private void button3_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= dataGridView1.Rows.Count -1; i++)
        {
            if ((bool)dataGridView1.Rows[i].Cells[0].Value == true)
            {
                dataGridView1.Rows.Remove(dataGridView1.Rows[i]);
                if (i < dataGridView1.Rows.Count -1)
                {
                    i--;
                }

            }
        }
    }
intj=0;
int countRows=dataGridView1.Rows.Count;
对于(int i=0;i
Op询问的是DataGridview而不是Gridview。这是一个Windows窗体。你应该在帖子中添加更多信息,而不仅仅是提供一个写得不好的标题和格式更差的代码。@cfrozendath只是一个例子,我不能给出,即使他给出了他的数据库也不能使用它并给出解决方案,我想帮助的唯一方法是他如何执行这是一个工作示例,这样OP就可以实现同样的功能,而OP的数据要少得多,这是唯一的方法(以我自己的工作代码为例)并向他解释如何实现。如果我错了,请纠正我。请添加一个说明,说明您认为这是答案的原因,并解释您的代码。您应该反向循环,以避免丢失连续选定行的删除-即从最后一行循环到第一行,我--。Pradeep Kumar,是的,这是可能的,但您的值将为+1datGridView1.Rows.Count不可接受,也可以使用try-catch解决问题。请测试它!。
            int j = 0;
            int countRows = dataGridView1.Rows.Count;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToBoolean(dataGridView1.Rows[i]
                                      .Cells[0].Value) == true)
                {
                    dataGridView1.Rows.RemoveAt(i);
                }

                i = i - 1;
                j = j + 1;

                if (j == countRows)
                {
                    break;
                }
            }