当该行中的任何列为空时,从数据文件中删除行 [英] Delete row from datatale when any column in that row is empty
本文介绍了当该行中的任何列为空时,从数据文件中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在数据表中显示从excel表中获取的数据。如果任何行为空或任何单元格(列值)为空,那么我想删除那个受尊重的行。
string strNewPath = Server.MapPath( 〜/ VendorUploads / + strusername + 。xls);
strConnection = Provider = Microsoft.Jet.OLEDB.4.0; Data Source = + strNewPath + ;扩展属性= \Excel 8.0; HDR =是; IMEX = 1 \跨度>;
strQuery = SELECT FROM [Sheet1 $];
oleDbConnection = new OleDbConnection(strConnection);
if (oleDbConnection.State == ConnectionState.Closed)oleDbConnection.Open();
oleDbCommand = new OleDbCommand(strQuery,oleDbConnection);
oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
oleDbDataAdapter.Fill(dataTable);
dataTable = dataTable.Rows.Cast< DataRow>()。其中(row = > !row.ItemArray.All(field = > 字段是 System.DBNull || string .Compare ((field as string )。Trim(), string .Empty)== 0 ))。CopyToDataTable();
现在我正在使用一个会删除空行的代码。但我想删除行中的一行尊重行中的列值为空
任何人都可以请我
解决方案
;
oleDbConnection = new OleDbConnection(strConnection);
if (oleDbConnection.State == ConnectionState.Closed) oleDbConnection.Open();
oleDbCommand = new OleDbCommand(strQuery,oleDbConnection);
oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
oleDbDataAdapter.Fill(dataTable);
dataTable = dataTable.Rows.Cast< DataRow>()。Where(row = > !row.ItemArray.All(field = > field 是 System.DBNull || string .Compare((field as string ).Trim(), string .Empty)== 0 ))。CopyToDataTable();
现在我正在使用一个会删除空行的代码。但我想删除行中的一行尊重行中的列值为空
任何人都可以请我
更改row.ItemArray.All
到row.ItemArray。任何
dataTable = dataTable.Rows.Cast< DataRow>()。其中(row = >
!row.ItemArray.Any(field = > 字段 System.DBNull || string .IsNullOrEmpty(字段 as string )))。CopyToDataTable();
i want to display data in datatable which is get from excel sheet . if any row is empty or any cell(column value ) is empty then i want to delete that respected row.
string strNewPath = Server.MapPath("~/VendorUploads/" + strusername + ".xls");
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strNewPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\"";
strQuery = "SELECT FROM [Sheet1$]";
oleDbConnection = new OleDbConnection(strConnection);
if (oleDbConnection.State == ConnectionState.Closed) oleDbConnection.Open();
oleDbCommand = new OleDbCommand(strQuery, oleDbConnection);
oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
oleDbDataAdapter.Fill(dataTable);
dataTable = dataTable.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();
Right now i am using a code which will delete empty row .But i want to delete the row when one of the column value in respected row is empty
Can any one please heip me
解决方案
"; oleDbConnection = new OleDbConnection(strConnection); if (oleDbConnection.State == ConnectionState.Closed) oleDbConnection.Open(); oleDbCommand = new OleDbCommand(strQuery, oleDbConnection); oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand); oleDbDataAdapter.Fill(dataTable); dataTable = dataTable.Rows.Cast<DataRow>().Where(row => !row.ItemArray.All(field => field is System.DBNull || string.Compare((field as string).Trim(), string.Empty) == 0)).CopyToDataTable();
Right now i am using a code which will delete empty row .But i want to delete the row when one of the column value in respected row is empty
Can any one please heip me
changerow.ItemArray.All
torow.ItemArray.Any
dataTable = dataTable.Rows.Cast<DataRow>().Where(row => !row.ItemArray.Any(field => field is System.DBNull || string.IsNullOrEmpty(field as string))).CopyToDataTable();
这篇关于当该行中的任何列为空时,从数据文件中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文