如何在数据表的选择方法中使用过滤器 [英] How to use Filter in Select Method of DataTable

查看:259
本文介绍了如何在数据表的选择方法中使用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我已经创建了一个C#应用程序.
该应用程序运行正常,但停留在某一时刻.
我正在使用DataTable"Items"从数据库中获取数据.
目前,我的DataTable具有两列Item_Code,Item_Desc,并且它从数据库中获取约500行.现在我只需要选择对应于Item_Code =''BO97''的行,如果找到该行,那么它将自动存储在DataTable中.总的来说,我只希望在数据表项目"中显示一行.


在此先感谢

Hello there

I have created a C# application.
The application runs fine but got stuck at one point.
I am using DataTable "Items" to fetch data from the database.
Currently my DataTable having two columns Item_Code,Item_Desc and it fetches around 500 rows from the database. Now I need to select only row corresponding to Item_Code =''BO97'' and if this row is found, then it will be automatically store in the DataTable . Overall I want only one single row to be display in the DataTable "Items",if found.


Thanks in Advance

推荐答案

DataTable Select 方法在这里解释了
http://msdn.microsoft.com/en-us/library/b5c0xc84.aspx [ ^ ]
可以用于过滤DataTable Rows ,如下所示:
The Select method of DataTable explained here
http://msdn.microsoft.com/en-us/library/b5c0xc84.aspx[^]
can be used to filter the Rows of the DataTable as follows:
DataRow[] rows = items.Select("Item_Code ='BO97'",string.Empty, DataViewRowState.CurrentRows);


第二个参数可用于Sort 方法返回的行.
Select 方法返回DataRowsArray .因此,DataTable 中的所有Columns 都自动出现在返回的行中.

最好使用DataViewRowState.CurrentRows 作为该方法的第三个参数,这样它就不会返回已被Row.Delete()方法删除的rows .否则,它也会返回这些行,并且在对这些行执行操作时将引发错误.
如果找到单行,则


The second parameter can be used to Sort the rows returned by the method.
The Select method returns an Array of DataRows. Hence all Columns of DataTable are automatically present in the returned rows.

It is preferable to use DataViewRowState.CurrentRows as the third argument to the method, so that it will not return the rows which have been deleted by Row.Delete() method. Otherwise, it returns these rows also and while performing an operation on these rows an error will be thrown.
If the single row is required if found then

if (rows.Length > 0) 
    //use rows[0]


另外,也可以使用DataView


Alternatively, the DataView can be used

DataView itemsView = items.DefaultView();
itemsView.RowFilter= "Item_Code='BO97'";
if itemsView.Count > 0 then
    //itemsView[0].Row; can be used


您必须修改选择查询.
使用类似这样的内容.
You have to modify your select query.
Use something like this.
select * from Items where Item_Code=''B097''




如果您要过滤数据表,则
尝试遵循代码


Hi,

if u want to filter datatable then
try following Code


DataRow[] drarray= objDataTable.Select("Item_Code='BO97'");



希望对您有帮助
祝你好运
快乐编码:)



Hope this help u
Best Luck
Happy Coding:)


这篇关于如何在数据表的选择方法中使用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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