简体   繁体   中英

How to filter particular column in datatable

I Have to filter a particular column in my data table.

here is my code

 string Groupname = txtGroup.Text;
 DataTable dt1 = _objGetDataProcess.Getgroupname(Groupname);

In dt1 i am getting table like this 在此处输入图片说明

here i have to filter Item column alone.

You can use Linq-To-DataTable :

var filteredRows = dt1.AsEnumerable()
    .Where(row => row.Field<string>("Item") == itemTextToFilter);

... case-insensitive:

var filteredRows = dt1.AsEnumerable()
    .Where(row => string.Equals(row.Field<string>("Item"), itemTextToFilter,StringComparison.CurrentCultureIgnoreCase));

If you need a new DataTable use filteredRows.CopyToDataTable() .

If you want to find also sub-strings:

var filteredRows = dt1.AsEnumerable()
    .Where(row => row.Field<string>("Item").Contains(itemTextToFilter));

... case-insensitive:

var filteredRows = dt1.AsEnumerable()
    .Where(row => row.Field<string>("Item").IndexOf(itemTextToFilter, StringComparison.CurrentCultureIgnoreCase) >= 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM