数据表Select()方法

发布于 2024-12-11 06:46:46 字数 287 浏览 0 评论 0原文

我有一个 Datagridview,数据源dtCustomer 我只想根据搜索文本过滤网格视图的内容。 我尝试了以下代码

DataTable dtSearch =  dtCustomer;
dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
grvCustomer.DataSource = dtSearch;

但这不起作用。 如果有人知道解决方案,请分享。

I have a Datagridview and the Data Source is dtCustomer
I just want to filter the content of grid view based on a search text.
Itried the following code

DataTable dtSearch =  dtCustomer;
dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
grvCustomer.DataSource = dtSearch;

But this is not working.
If any body knows the solution please share.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

沉溺在你眼里的海 2024-12-18 06:46:46

试试这个:

dtSearch.DefaultView.RowFilter = "cust_Name like '" + txtSearch.Text + "%'";  

并检查是否有空间可以通过修剪文本来删除。

Try this:

dtSearch.DefaultView.RowFilter = "cust_Name like '" + txtSearch.Text + "%'";  

And check whatever there is space to be removed by triming the text.

安稳善良 2024-12-18 06:46:46

DataTable.Select 的返回值是一个 DataRow[] 数组。它返回匹配 DataRow 的列表。您的代码目前对这些行没有任何作用。

您可以设置带有过滤器的 DataView 并将网格的 DataSource 设置为 DataView :

DataView dv = new DataView(dtSearch);
dv.RowFilter = "...";
grvCustomer.DataSource = dv;

The return value for DataTable.Select is a DataRow[] array. It returns a list of matching DataRows. Your code does nothing with those rows at the moment.

You can setup a DataView with a filter and set the grid's DataSource to the DataView:

DataView dv = new DataView(dtSearch);
dv.RowFilter = "...";
grvCustomer.DataSource = dv;
╰沐子 2024-12-18 06:46:46

您可以尝试使用 DataView (代码未测试) -

DataView dv = new DataView(dtSearch);
dv.RowFilter = "cust_Name like '" + txtSearch.Text + "%'";
grvCustomer.DataSource = dv;

You could try using a DataView (code not tested) -

DataView dv = new DataView(dtSearch);
dv.RowFilter = "cust_Name like '" + txtSearch.Text + "%'";
grvCustomer.DataSource = dv;
错々过的事 2024-12-18 06:46:46

或者试试这个;

dataGridView.Datasource = datatable.Select("....").CopyToDataTable()

Or Try this;

dataGridView.Datasource = datatable.Select("....").CopyToDataTable()
北恋 2024-12-18 06:46:46
dtCustomer.Rows.Cast<DataRow>().Select(dr => (string)dr["cust_Name"].Startswith("zzz")).ToList()
dtCustomer.Rows.Cast<DataRow>().Select(dr => (string)dr["cust_Name"].Startswith("zzz")).ToList()
一个人的旅程 2024-12-18 06:46:46

你可以做这样的事情。

DataView dv1 = dtDefault.DefaultView; 
dv1.RowFilter = "CusGroupId=1 and CustomerCode LIKE '"+txtCustomer.Text +"%'";  
DataTable dt=dv1.ToTable();

You can do something like this.

DataView dv1 = dtDefault.DefaultView; 
dv1.RowFilter = "CusGroupId=1 and CustomerCode LIKE '"+txtCustomer.Text +"%'";  
DataTable dt=dv1.ToTable();
关于从前 2024-12-18 06:46:46

DataTable.Select 返回行数组,但您绑定的是整个数据表而不是筛选的行。使用这种方式或DataView

DataTable dtSearch =  dtCustomer;
var filter = dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
grvCustomer.DataSource = filter.ToList();

DataTable.Select returns array of row, but you are binding entire data table not filtered rows. use this way or DataView

DataTable dtSearch =  dtCustomer;
var filter = dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");
grvCustomer.DataSource = filter.ToList();
夜吻♂芭芘 2024-12-18 06:46:46

我想这就是你要找的?

//DataTable dtSearch =  dtCustomer;
//dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");


grvCustomer.DataSource = dtCustomer.Select("cust_Name like '" + txtSearch.Text + "%'");

当你想回到原始数据时

grvCustomer.DataSource = dtCustomer;

I think this is what you're looking for?

//DataTable dtSearch =  dtCustomer;
//dtSearch.Select("cust_Name like '" + txtSearch.Text + "%'");


grvCustomer.DataSource = dtCustomer.Select("cust_Name like '" + txtSearch.Text + "%'");

And when you want to go back to the original data

grvCustomer.DataSource = dtCustomer;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文