Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a result grid view screen. The source of which is a datatable.

Datatable query = select project_type ,tc_id,details,dom,planned_execution_start_date,planned_execution_end_date,actual_execution_start_date,actual_execution_end_date,test_prime,status,'View Details' as view_details, 'View History' as view_history from project_details;

Now, I have to search grid using project_type,tc_id,test_prime

How to do this?

What I have tried:

protected void btnSubmit_Click(object sender, EventArgs e)
       {
           string searchText;
           searchText = txtbxSearch.Text;
           bindSearchData(searchText);
       }

       protected void bindSearchData(string searchText)
       {

           System.Data.DataTable dt = providers.GetDataTable("select project_type ,tc_id,details,dom,planned_execution_start_date,planned_execution_end_date,actual_execution_start_date,actual_execution_end_date,test_prime,status,'View Details' as view_details, 'View History' as view_history from project_details", "VTAS");

           if (searchText != null)
           {

               //DataView dv = new DataView(dt);
               //dv.RowFilter = "project_type in ('"+searchText+"')"  ;

               //dv.RowFilter = "test_prime in ('"+searchText+"')";
               //dv.RowFilter = "tc_id in ('" + searchText + "')";
               //Grid1.DataSource = dv;
               //Grid1.DataBind();
               //DataRow[] Result = dt.Select("project_type in ('" + searchText + "') or test_prime  in ('" + searchText + "') or tc_id  in ('" + searchText + "')");
               DataRow[] Result = dt.Select("project_type in ('" + searchText + "') or  tc_id  in ('" + searchText + "')");
               Grid1.DataSource = Result;
               Grid1.DataBind();

           }
           else
           {
               Grid1.DataSource = dt;
               Grid1.DataBind();
           }
       }
Posted
Updated 14-Mar-18 9:41am

Well, a filterExpression parameter of DataTable.Select method[^] creates a DataColumn.Expression[^].
There's a list of applicable functions, operators, which can be used to define expression. IN is also available and you have to pass values as a comma separated string. Take a look at example:
C#
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("project_type", typeof(int)));
dt.Columns.Add(new DataColumn("tc_id", typeof(int)));

Random r = new Random();
dt = Enumerable.Range(1, 50)
	.Select(x=> dt.LoadDataRow(new object[]{r.Next(x)+1,r.Next(x+1)+1}, false))
	.CopyToDataTable();
//filter criteria:
string tcs = "1,2,3";
string pts = "5,8,10";
var filtered = dt.Select(string.Format("project_type IN({0}) OR tc_id IN({1})", pts, tcs));


Above code returns, for example:
project_type tc_id
1            1 
2            2 
3            2 
3            2 
3            3 
4            2 
4            2 
5            1 
5            2 
5            16 
6            3 
8            9 
8            21 
10           15 
10           18 
10           34 
15           1 
26           2


The way a datatable has been filled is not important, but - as you can see - filterExpression works fine ;)

There's at least few ways to filter data:
1. On server (database) level (via SQL query), - strongly recommended!
2. On client level, through filetering datatable, views, etc., using:
- DataColumn expressions (Select method)
- LINQ to DataSet | Microsoft Docs[^]

Good luck!
 
Share this answer
 
You already found DataTable.Select() and that standard SQL syntax does not always work with this function.
The problem seems to be in the IN clause, here is an example that uses a custom IN function which might work as you expect: IN clause in datatable.select[^]
 
Share this answer
 
v2
Comments
Maciej Los 14-Mar-18 14:43pm    
OP is using Select method ;)
RickZeeland 14-Mar-18 15:02pm    
Thanks, you are right as usual, updated my answer !
Maciej Los 14-Mar-18 15:44pm    
Better... I agree with this statement: The problem seems to be in the [IN] clause. I tried to explain how to create filerExpression using [IN] operator. Please, see my answer.
Member 13614228 15-Mar-18 3:24am    
I am getting some different exceptions.. Attached screenshot for exception
RickZeeland 15-Mar-18 3:36am    
I'm afraid you can only attach screenshots in articles, not in questions sadly, but maybe you can use DropBox, OneDrive or something like that ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900