如何过滤 DataTable 中的空字符串?
我需要过滤一列(比如名称为 string.Empty 的客户名称)
我试过这个,但我无法进入正确的方式..
我需要通过DataView.RowFilter
.. 过滤 DataView 那么如何为string.Empty
..提供过滤器字符串
对此有任何想法吗?
过滤数据表-
dt.Select("customer_name = ''");
过滤数据视图-
dv.RowFilter = "customer_name = ''";
使用 Select 方法:
DataRow[] foundRows = dt.Select("MyColumn = ''");
您可以使用Select
方法DataTable
:
//selects all customers which name is empty
var rows = dtData.Select("CustomerName = ''");
请参阅下面的代码,可能会有所帮助。我正在回答,因为这个问题有一个标签 RowFilters
private void GetRowsByFilter()
{
DataTable table = DataSet1.Tables["YourTable"];
// Presuming the DataTable has a column named Date.
string expression = "Column_name = ''";
// Sort descending by column named CompanyName.
string sortOrder = "ColumnName DESC";
DataRow[] foundRows;
// Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression, sortOrder);
// Print column 0 of each returned row.
for(int i = 0; i < foundRows.Length; i ++)
{
Console.WriteLine(foundRows[i][0]);
}
}
试试下面的代码:
DataTable dt=new DataTable();
DataRow dr;
dr=dt.NewRow();
if(dr["CustomerName"]==null)
{
put some code here.........
}
我希望这段代码对你有帮助 4