如何删除DataTable中所有已检查的名称行?

时间:2015-08-13 12:24:31

标签: c# datagridview items checkedlistbox

EDITED: 我会试着更清楚地解释一下。 我将 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

在我的程序中,我有 checkedlistbox ,其中包含组名&#34; name1&#34;,&#34; name2&#34;等。删除按钮中的我的代码删除第一个组名checkedlistbox +包含此名称组的所有行。我想修改我的按钮以删除所选的checkedlistbox项(组名)和包含该组名的所有行。我希望我更加慷慨解释。 这是我的删除按钮代码:

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

2 个答案:

答案 0 :(得分:0)

之后您还必须更新数据集 da.Update(hostsDataSet,&#34; Temp&#34;);

答案 1 :(得分:0)

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