简体   繁体   中英

How to convert datatable column type to perform filter

So i'm trying to filter my data with BindingSource from my datagridview but my Column (Balance and Inactive Days) type is string also have Null value and i'm trying to use comparison operators <=, >=

but error shown System.Data.EvaluateException: 'Cannot perform '>=' operation on System.String and System.Int32.'

i've tried to change valueType of my datagridview, dgv.Columns["Balance"].ValueType = typeof(Decimal); //it doesnt work dgv.Columns["Balance"].ValueType = typeof(Decimal); //it doesnt work

and tried to clone my datatable and set new type for my cloned datatable

DataTable dtCloned = dt.Clone();
dtCloned.Columns["Balance"].DataType = typeof(Decimal);
dtCloned.Columns["Inactive Days"].DataType = typeof(Int32);
foreach (DataRow row in dt.Rows)
{
    dtCloned.ImportRow(row);
}

// error shown
System.ArgumentException: 'Input string was not in a correct format.Couldn't store <> in Debit Inactive Days Column.  Expected type is Int32.'

Here is my filter code:

public void dgv_tabPage_content(DataGridView dgv, TabPage tabpage)
{
   BindingSource bs = new BindingSource();
   bs.DataSource = dgv.DataSource;

   string filter_DD1 = "[Acct Type] LIKE '%DD%' AND [Balance] <= 6000.00 AND [Inactive Days] >= 5";
   string filter_DD2 = "[Acct Type] LIKE '%DD%' AND [Balance] >= 6000.00 AND [Inactive Days] >= 3";

   bs.Filter = string.Format("{1} OR {2}", filter_DD1, filter_DD2);
   dgv.DataSource = bs;
}

How can i filter my string typed column with null value with comparison operators?

我不确定你的意思是什么,但为什么不使用where is not null然后你的过滤器关闭。

string filter = string.Format("[Task] is not null and LIKE '%{0}%'", tabpage.Text);

I think you mean to convert the type in the filter expression. If this is what you mean, you can use the Convert function of the filter Expression: Convert(expression, type)

string filter_DD1 = "[Acct Type] LIKE '%DD%' AND Convert([Balance], System.Decimal) <= 6000.00 AND [Inactive Days] >= 5";

For more Info click here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM