How to filter datatable using between c #

I am using a data table with C # and I want to filter the data table using in between. But when I run my code, the following exception is thrown. "The expression contains an unsupported Between statement.

here is the code:

string str = "ITEM_ID BETWEEN  " + textEdit1.Text + " AND  " +    textCUS_COA_CODE.Text + " ";

DataTable dt = new DataTable();
dt = this.pAK_ASIADataSet.sp_STOCKS_report;

dt.DefaultView.RowFilter =str;
+5
source share
6 answers

I have not tried, but use "ITEM_ID >= bla AND ITEM_ID <= bla2"should work fine.

+6
source

Having looked at the MSDN forums, it seems that the BETWEENoperator does not support string filters:

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/1220e0f5-4bab-4b53-b8b6-a7a9e8420558/

You can replace AND:

ITEM_ID >= val_oneAND ITEM_ID <= val_two
+3
source

LINQ to DataTable:

var result = dt.AsEnumerable()
  .Where(row => {
                    var itemId = row.Field<int>("ITEM_ID");
                    var value1 = int.Parse(textEdit1.Text);
                    var value2 = int.Parse(textCUS_COA_CODE.Text);

                    return itemId >= value1 && itemId <= value2;
                });
+3

:

var rows = dt.Select(String.Format("ITEM_ID >= {0} AND ITEM_ID <= {1}", textEdit1.Text, textCUS_COA_CODE.Text))
+1

RowFilter datatable, <= >= , :

string str = "ITEM_ID <=" + textEdit1.Text + " AND  ITEM_ID>=" +    textCUS_COA_CODE.Text + " ";

DataTable dt = new DataTable();
dt = this.pAK_ASIADataSet.sp_STOCKS_report;

dt.DefaultView.RowFilter =str;
+1

:

String str = String.Format("ITEM_ID >= {0} AND ITEM_ID <= {0}", textEdit1.Text);

DataTable dt = new DataTable();
dt = this.pAK_ASIADataSet.sp_STOCKS_report;

dt.DefaultView.RowFilter =str;
+1
source

All Articles