如何转换数据表列类型以执行过滤器

时间:2019-05-21 07:05:46

标签: c# filter datatable

因此,我正在尝试使用BindingSource中的datagridview过滤数据 但是我的列(平衡和不活动天数)类型为string也有Null value,我正在尝试使用比较运算符<=, >=

但显示错误System.Data.EvaluateException: 'Cannot perform '>=' operation on System.String and System.Int32.'

我试图更改datagridview的valueType, dgv.Columns["Balance"].ValueType = typeof(Decimal); //it doesnt work

并尝试克隆我的数据表并为克隆的数据表设置新类型

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.'

这是我的过滤器代码:

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

如何使用比较运算符过滤具有空值的字符串类型的列?

1 个答案:

答案 0 :(得分:0)

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

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