Filtering a empty string in DataTable

白昼怎懂夜的黑 提交于 2019-12-23 21:46:53

问题


How do filter a empty String in DataTable?

I need to filter a column (say Customer Name where the name is string.Empty)

I tried this but i cant get into right way..

I need to filter the DataView through DataView.RowFilter.. so how to give filter string for string.Empty..

Any idea on this?


回答1:


To filter a dataTable-

dt.Select("customer_name = ''"); 

To Filter datatview-

dv.RowFilter = "customer_name = ''";



回答2:


Use Select method:

DataRow[] foundRows = dt.Select("MyColumn = ''");



回答3:


You can use Select method for DataTable:

//selects all customers which name is empty
var rows = dtData.Select("CustomerName = ''"); 



回答4:


See the code below, might be a help. I am answering as the question has a tag 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]);
       }
   }



回答5:


Try below code:

DataTable dt=new DataTable();
DataRow dr;
dr=dt.NewRow();
if(dr["CustomerName"]==null)
{
put some code here.........
}

i Hope This code will help 4 u



来源:https://stackoverflow.com/questions/17564004/filtering-a-empty-string-in-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!