如何过滤DataTable中的空字符串?
我需要过滤一个列(比如名称为string.Empty的客户名称)
我尝试了这个,但我不能正确地进行..
我需要通过DataView.RowFilter
过滤DataView ..所以如何为string.Empty
提供过滤字符串..
对此有什么想法吗?
答案 0 :(得分:3)
过滤dataTable-
dt.Select("customer_name = ''");
过滤数据视图 -
dv.RowFilter = "customer_name = ''";
答案 1 :(得分:1)
使用选择方法:
DataRow[] foundRows = dt.Select("MyColumn = ''");
答案 2 :(得分:0)
您可以对Select
使用DataTable
方法:
//selects all customers which name is empty
var rows = dtData.Select("CustomerName = ''");
答案 3 :(得分:0)
请参阅下面的代码,可能是一个帮助。我正在回答,因为问题有一个标签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]);
}
}
答案 4 :(得分:0)
尝试以下代码:
DataTable dt=new DataTable();
DataRow dr;
dr=dt.NewRow();
if(dr["CustomerName"]==null)
{
put some code here.........
}
我希望这段代码可以帮助你4