0
List<DataGridViewRow> rowsToDelete = new List<DataGridViewRow>();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;
    if (Convert.ToBoolean(chk.Value) == true)
        rowsToDelete.Add(row);
}
//loop through the list to delete rows added to list<T>:
foreach (DataGridViewRow row in rowsToDelete)
        dataGridView1.Rows.Remove(row);

我选择了 3 个项目,它删除了 2 个,但留下了 1 个。

我们如何解决它?

4

3 回答 3

2

更新

听起来您想删除未选中的项目。

你要做的就是从底部开始,然后到顶部,否则你会留下额外的物品。

例子:

for (int i = DataGridView1.SelectedRows.Count - 1; -1 < i; i--)
{
   object objChecked = DataGridView1.SelectedRows[i].Cells[0].Value;
   if ((objChecked  != null) && !(bool)objChecked)
   {
     DataGridView1.Rows.RemoveAt(i);
   }
}

更新 2:

根据您在下面的评论,这个版本看起来Rows而不是SelectedRows.

Rows在开始循环之前检查是否存在:

if ((0 < DataGridView1.Rows.Count) && (0 < DataGridView1.Columns.Count)) 
{
  for (int i = DataGridView1.Rows.Count - 1; -1 < i; i--)
  {
    var row = DataGridView1.Rows[i];
    if ((row.Cells[0].Value != null) && (row.Cells[0].Value != DBNull.Value))
    {
      bool isChecked = (bool)row.Cells[0].Value;
      if (isChecked)
      {
        DataGridView1.Rows.Remove(row);
      }
    }
  }
}

它在错误检查方面更加健壮,并且通过引用而不是索引来删除行。

于 2013-04-22T19:26:52.913 回答
1

已编辑

您可以通过 SelectedRows 属性删除。

确保该MultiSelect属性在您的数据网格上设置为 true。

然后,您可以SelectedRows根据自己的选择使用该物业:

这就是你需要的,试试这个代码:

for (int i = dataGridView1.Rows.Count -1; i >= 0 ; i--)
{
    if ((bool)dataGridView1.Rows[i].Cells[0].FormattedValue)
    {
        dataGridView1.Rows.RemoveAt(i);
    }
}

或者

foreach (DataGridViewRow row in DataGridView1.SelectedRows)
{
    DataGridView1.Rows.Remove(row);
}
于 2013-04-22T18:53:21.010 回答
0

页面:

<strong>Asp.Net : Delete Multiple Records form datagridview in one time<br />
        </strong>

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
            CellPadding="4" EnableModelValidation="True" ForeColor="Black">
            <Columns>
                <asp:TemplateField>
                    <EditItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="id" HeaderText="Sr No" />
                <asp:BoundField DataField="doc_name" HeaderText="Name" />
                <asp:BoundField DataField="doc_add" HeaderText="Address" />
                <asp:BoundField DataField="doc_mob" HeaderText="Mobile No" />
                <asp:BoundField DataField="doc_email" HeaderText="Email" />
            </Columns>
            <FooterStyle BackColor="#CCCC99" ForeColor="Black" />
            <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
        </asp:GridView>
        <br />
        <asp:Button ID="Button1" runat="server" Font-Size="12pt"
            onclick="Button1_Click1" Text="Delete" />
        <br />

页面背后的代码:

SqlConnection conn = new SqlConnection(@"server=server-pc; database=HMS; integrated security=true");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            load_data();
        }
    }

   public void load_data()
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from doc_master", conn);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
    }
   protected void Button1_Click1(object sender, EventArgs e)
   {
       CheckBox ch;
       for (int i = 0; i < GridView1.Rows.Count; i++)
       {
           ch = (CheckBox)GridView1.Rows[i].Cells[0].Controls[1];
           if (ch.Checked == true)
           {
      int id = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);
      SqlCommand cmd = new SqlCommand("delete from doc_master where ID=" + id + " ", conn);
      conn.Open();
      cmd.ExecuteNonQuery();
      conn.Close();
           }
       }

       load_data();
   }

详细代码访问: http ://www.gtuguide.com/2014/05/deleting-multiple-rows-in-gridview.html

于 2014-05-22T06:32:24.967 回答