Question

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?

Was it helpful?

Solution

To filter a dataTable-

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

To Filter datatview-

dv.RowFilter = "customer_name = ''";

OTHER TIPS

Use Select method:

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

You can use Select method for DataTable:

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

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]);
       }
   }

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top