在数据表中搜索C# [英] Search in data table C#

查看:64
本文介绍了在数据表中搜索C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个结果网格视图屏幕。它的来源是一个数据表。



数据表的查询=选择project_type,tc_id,细节,DOM,planned_execution_start_date,planned_execution_end_date,actual_execution_start_date,actual_execution_end_date,test_prime,状态,观详细信息'作为view_details,'查看历史'作为来自project_details的view_history;



现在,我必须使用project_type搜索网格,tc_id,test_prime



怎么做?



我尝试过:



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

保护无效bindSearchData(串SEARCHTEXT)
{

System.Data.DataTable DT = providers.GetDataTable(选择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 []结果= dt.Select('+ SEARCHTEXT + 在(project_type ')或test_prime在(' + SEARCHTEXT + ')或tc_id在(' + SEARCHTEXT +' ));
DataRow [] Result = dt.Select(project_type in('+ searchText +')或tc_id in('+ searchText +'));
Grid1.DataSource =结果;
Grid1.DataBind();

}
else
{
Grid1.DataSource = dt;
Grid1.DataBind();
}
}

解决方案

好吧, filterExpression 参数 DataTable.Select方法 [< a href =https://msdn.microsoft.com/en-us/library/det4aw50(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]创建 DataColumn.Expression [< a href =https://docs.microsoft.com/en-us/dotnet/api/system.data.datacolumn.expression?view=netframework-4.7.1target =_ blanktitle =New Window> ^ ]。

有一个适用的函数列表,运算符,可用于定义表达式。 IN 也可用,您必须以逗号分隔的字符串传递值。看看例子:

 DataTable dt =  new  DataTable(); 
dt.Columns.Add( new DataColumn( project_type typeof int )));
dt.Columns.Add( new DataColumn( tc_id typeof int )));

随机r = Random();
dt = Enumerable.Range( 1 50
。选择(x = > dt.LoadDataRow( new object [] {r.Next(x)+ 1,r.Next(x + 1)+1}, false ))
.CopyToDataTable() ;
// 过滤标准:
string tcs = 1,2,3;
string pts = 5,8, 10\" ;
var filtered = dt.Select( string .Format( project_type IN({0})或tc_id IN({1}),pts,tcs));





以上代码返回,例如:

 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





填充数据表的方式并不重要,但是 - 正如您所见 - filterExpression 工作正常;)



至少有几种方法可以过滤数据:

1.在服务器(数据库)上级别(通过SQL查询), - 强烈推荐!

2.在客户端级别,通过使用以下方式编辑数据表,视图等:

- DataColumn表达式(选择方法)

- LINQ到DataSet | Microsoft Docs [ ^ ]



祝您好运!


您已找到 DataTable.Select()并且该标准SQL语法并不总是与此函数一起使用。

问题似乎在 IN 子句中,这是一个使用自定义IN的示例功能可能会按预期工作: datatable.select中的IN子句 [ ^

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

解决方案

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:

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!


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[^]


这篇关于在数据表中搜索C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆