Dan Hall
Dan Hall

Reputation: 1534

Filtering datatable number column

i would like to filter a datatable by a column which contains number data, i'm trying the following:

string selected = colSelect.GetItemText(colSelect.SelectedItem);
if (filterText.Text.Length == 0) 
{
     data_table.DefaultView.RowFilter = string.Empty;
}
else
{
     data_table.DefaultView.RowFilter = string.Format("Price Like '%{0}%'", filterText.Text);

I've tried casting the second value to a string but no luck, i get the error:

Cannot perform 'Like' operation on System.Decimal and System.String

The data entered would be any number or text, but based on the data only relevant number values would show with the filter.

Upvotes: 2

Views: 1637

Answers (1)

Soner Gönül
Soner Gönül

Reputation: 98740

Looks like Price column is decimal typed but you supplied a string which is filterText.Text to filter it.

One solution might be using CONVERT which is explained in DataColumn.Expression documentation like;

data_table.DefaultView.RowFilter = "CONVERT(Price, 'System.String') like '%" + filterText.Text + "%'";

I didn't tried this one but you can parse your filterText.Text to decimal (if it is valid one) and use it like;

data_table.DefaultView.RowFilter = "Price Like '%" + decimal.Parse(filterText.Text) + "%'";

But as I said, I an not %100 sure for the second one.

Upvotes: 2

Related Questions