简体   繁体   English

从数据表中删除包含值的行

[英]Remove row from DataTable that contains value

I want to have a row with column CtryCode equal to "MM" deleted.我想删除一行 CtryCode 等于“MM”的行。 Is this logic above doesn't do it?上面这个逻辑不行吗?

if (dataTableCopy.Rows[index]["CtryCode"].ToString().Trim().ToUpper().Contains("MM"))
{
    response.DataTable.Rows.RemoveAt(index + offset--);
}

Breakpoint is not hitting the response statement above and I could see MM row after the execution of this, strange?断点没有命中上面的响应语句,执行后我可以看到MM行,奇怪吗? Please help.请帮忙。

To delete row that contains value try this要删除包含值的行试试这个

DataRow[] result = dt2.Select("CtryCode = 'mm'");
foreach (DataRow row in result)
{
    if (row["CtryCode"].ToString().Trim().ToUpper().Contains("MM"))
    dt2.Rows.Remove(row);
}

If you know the index of that row, You can delete the row like this,如果您知道该行的索引,您可以像这样删除该行,

for(int i=0;i<dataTableCopy.Rows.Count-1;i++)
   {        
        if (dataTableCopy.Rows[i]["CtryCode"].ToString().Trim().ToUpper().Contains("MM"))
    {
         dataTableCopy.Rows[i].Delete();
    }
  }

Please find the code:请找到代码:

int id=98;
DataTable dt = new DataTable();
foreach (DataRow row in dt.Rows)
{
    if(row["Id"].ToString().Trim().Contains(id.ToString()))
        dt.Rows.Remove(row);
}

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

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