C# How To Filter DataTable

Do you like this?

Summary:
This post shows you an example how to sort DataTable in C#.


Content:

Let's say your DataTable object contains two columns which are ItemNumber and ItemName. You want to search in the column ItemName for a string.

DataTable dt= GetDataTable(); //your own method to extract data
string expression= "ItemName LIKE 'abc*'";
if (dt != null){
	try
	{
		dt.DefaultView.RowFilter = expression;
	}
	catch (EvaluateException e) {
		Console.WriteLine("Your search contains invalid data."
			+ " More Information: " + e.Message);
	}

}
Now you might encounter this problem when you have spaces in columns' names such as "Item Name". At that time you need to use "[Item Name] LIKE 'abc*'".

 
comments powered by Disqus