如果列中的几列空白,如何从数据表中删除行? [英] How Do I Remove Rows From A Datatable If Few Of The Columns Are Blank ?

查看:85
本文介绍了如果列中的几列空白,如何从数据表中删除行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的开发人员,





如果某些列为空,我想从数据表中删除一些行。

i尝试下面的代码,



Dear Developers,


I want to remove some rows from my datatable if some of the columns are blank.
i tried below code,

int rowcount = datatabletest.Rows.Count;

             for (int i = 0; i < rowcount; i++)
             {
                 if (datatabletest.Rows[i]["Mcolumn1"].ToString().Trim() == "" || dtExport.Rows[i]["column2"].ToString().Trim() == "")
                 {
                    datatabletest.Rows.Remove(datatabletest.Rows[i]);

                 }
             }





但这里的问题是如果第一行满足条件该行将从表中删除。这将减少一个行计数表。但是代码将检查最后一行,因为我们已将其写入循环内。但由于已经删除了一行,因此无法在该位置找到代码。它在那个地方抛出没有排的例外。



例如:

我的表包含3行。

第一行有空值所以它会是由代码删除。但代码将检查位置3的行,因为for循环将循环3次,因为行数为3.

但是第一行被删除所以现在表只有2行。因此,当代码检查第3行位置时,它找不到并抛出错误。





我试过每个循环但也没有工作。以下错误

System.InvalidOperationException:集合被修改;枚举操作可能无法执行。



所以请分享你的逻辑。如果问题不明确,请告诉我。



but the problem here is if the first row meets the condition the that row will be deleted from the table.this will reduce one row count table.but the code will check for the last row since we have written it inside the loop.But code can not find in that position since one row is already deleted.Its throwing No row on that place exception.

For example:
my table contains 3 rows.
First row has null values so it will be deleted by code.But the code will check for the row at position 3 since for loop will loop 3 times as row count is 3.
but the first row is deleted so now table has only 2 rows.So when code check for row 3 position it couldn't find and throwing error.


I tried with for each loop also but doesn't work.Got below error
System.InvalidOperationException: Collection was modified; enumeration operation might not execute.

so please share your logic. please let me know if question is not clear.

推荐答案

一个简单的解决方案可能就是在任何移除时调整行数和索引。



试试这个:



An easy solution may be to just adjust your row count and index on any removal.

Try this:

int rowcount = datatabletest.Rows.Count;

             for (int i = 0; i < rowcount; i++)
             {
                 if (datatabletest.Rows[i]["Mcolumn1"].ToString().Trim() == "" || dtExport.Rows[i]["column2"].ToString().Trim() == "")
                 {
                    datatabletest.Rows.Remove(datatabletest.Rows[i]);
                    rowcount--;
                    i--;
                 }
             }


您好,

查看以下代码,您可以使用对DataTable的LINQ查询 [ ^ ]。

Hi,
Review below code, where you can get result using LINQ query on a DataTable[^].
DataTable datatabletest = new DataTable();
DataTable dtNewTableTest = new DataTable();

var vDataTable = from vRow in datatabletest.AsEnumerable()
             where vRow.Field<string>("Column1") != "" || vRow.Field<string>("Column2") != ""
             select vRow;

dtNewTableTest = vDataTable.CopyToDataTable();



AsEnumerable()返回的IEnumerable<&的DataRow GT; 。如果您需要将 IEnumerable< DataRow> 转换为 DataTable ,请使用 CopyToDataTable() extension。


AsEnumerable() returns IEnumerable<DataRow>. If you need to convert IEnumerable<DataRow> to a DataTable, use the CopyToDataTable() extension.


您好,

您可以从选择查询本身中删除列。我认为这会更好。请试试。
Hi,
You can remove the columns from the selection query itself. That will be better I think. Please try it.


这篇关于如果列中的几列空白,如何从数据表中删除行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆