繁体   English   中英

从我的 DataGridView 中删除多行会留下 1

[英]Deleting multiple rows from my DataGridView leaves 1 behind

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 个。

我们如何修复它?

更新

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

您将要做的是从底部开始并到达顶部,否则您将剩下多余的物品。

例子:

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);
      }
    }
  }
}

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

已编辑

您可以通过 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);
}

ASPX页面:

<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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM