ceranda
ceranda

Reputation: 61

C#, Datagrid, Search part of a string

According to my code below, I can filter datagridview when I start to enter the text in tbxSearch text box.

But it only works if i type the text in order.

Example: If I Started to type "D001-A" it is started to filtering the text starting from "D001-A".

If I enter "001-A" it is not filtering as the text is in middle of the string. Any advise please.

 private void tbxSearch_TextChanged(object sender, EventArgs e)
        {


                (dgvTracking.DataSource as DataTable).DefaultView.RowFilter = string.Format("DocumentNo LIKE '{0}%'", tbxSearch.Text);                

}

Upvotes: 0

Views: 300

Answers (1)

SᴇM
SᴇM

Reputation: 7213

Change string.Format("DocumentNo LIKE '{0}%'", tbxSearch.Text); to string.Format("DocumentNo LIKE '%{0}%'", tbxSearch.Text);

In your example it searching strings only starting with your parameter ('string%'), you need to add % from the begging, which will mean, that it will match result, if string will found in any place.

Upvotes: 1

Related Questions