【问题标题】:How to delete all checked name rows in DataTable?如何删除 DataTable 中所有选中的名称行?
【发布时间】:2015-08-13 12:24:30
【问题描述】:

我有一个 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,其中包含组名“name1”、“name2”等。我在删除按钮中的代码删除了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();
    }              
}

【问题讨论】:

    标签: c# datagridview items checkedlistbox


    【解决方案1】:

    您还必须在此之后更新数据集 da.Update(hostsDataSet, "Temp");

    【讨论】:

    • 尝试 hostsDataSet.Tables["group"].AcceptChanges();
    【解决方案2】:
    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();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-28
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-25
      • 1970-01-01
      • 2011-07-14
      • 2014-11-10
      • 2011-10-24
      相关资源
      最近更新 更多