Datatable Select() Method

87,171

Solution 1

Try this:

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

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

Solution 2

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;

Solution 3

Or Try this;

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

Solution 4

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

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

Solution 5

dtCustomer.Rows.Cast<DataRow>().Select(dr => (string)dr["cust_Name"].Startswith("zzz")).ToList()
Share:
87,171
Nithesh Narayanan
Author by

Nithesh Narayanan

I am Nithesh Adukkam Narayanan, a Full stack developer and Architect with 11+ years of experience in software development, design, best practices, and mentoring. My primary skillset in the .Net tech stack (C#, .net, .net core, Web API, MVC) along with the front end (React JS, JavaScript, Typescript, webpack). Been an innovator and played a key role in R&amp;D projects using python, image processing, and ICR. Proven expertise in cutting-edge technologies like cloud (azure, aws), event-driven, micro-service architecture, micro front end architecture, Docker, and containers. Hands-on in CI/CD (Azure DevOps) and agile methodologies.

Updated on July 09, 2022

Comments

  • Nithesh Narayanan
    Nithesh Narayanan almost 2 years

    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.