如何删除DataTable中所有选中的名称行

本文关键字:删除 DataTable 何删除 | 更新日期: 2023-09-27 18:07:50

我有一个xml文件加载到我的datagridview。加载到datagridview中的文件如下所示:

<?xml version="1.0" standalone="yes"?>
<program>
  <group active="1" name="name3">
    <item active="Active">
      <id>name3</id>
      <ip>223.26.0.0</ip>
      <names>jakas strona</names>
      <comment>komentarz</comment>
    </item>
    <item active="Active">
      <id>name3</id>
      <ip>223.26.0.0</ip>
      <names>jakas strona</names>
      <comment>komentarz</comment>
    </item>
  </group>
</program

在我的程序中,我有 checkkedlistbox ,其中包含组名"name1", "name2"等。我的代码在删除按钮删除第一个组名称上的checklistbox +包含这个名称组的所有行。我想修改我的按钮,以删除选中的checklistbox项目(组的名称)和所有行的包含这个组的名称。我希望我解释得更清楚。这是我的删除按钮代码:

private void deleteButton_Click(object sender, EventArgs e)
{
    if (checkedListBox1.SelectedIndex == 1)
    {
        // string groupName = group.Attribute("name").Value;
        hostsDataSet.Tables["group"].Rows[0].Delete();
    }              
}

如何删除DataTable中所有选中的名称行

您还必须在此之后更新数据集哒。更新(hostsDataSet,"临时工");

List<DataRow> rows_to_remove = new List<DataRow>();
//first add the match item to List like 
foreach (DataRow row1 in dt1.Rows)
{
    foreach (DataRow row2 in dt2.Rows)
    {
        if (row1["Name"].ToString() == row2["Name"].ToString())// change here as check box conditions
        {
            rows_to_remove.Add(row1);
        }
    }
}

    enter code here
foreach (DataRow row in rows_to_remoenter code hereve)
{
    dt1.Rows.Remove(row);
    dt1.AcceptChanges();
}