Simon Brown
Simon Brown

Reputation: 11

Search Datagridview with Excel as data

I have extracted data from an excel sheet to a datagridview which is great, but I have made a search box and I am unable to get it to work.

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "Site Name LIKE '%" + textBox2.Text + "%'";
        dataGridView1.DataSource = dv.ToTable();
    }
}

I'm getting error on the dv.Rowfilter saying

Syntax error: Missing operand after 'Name' operator.

Upvotes: 1

Views: 67

Answers (1)

Amit Joshi
Amit Joshi

Reputation: 16389

String "Site Name" is not correct column name. You must be having something like "Site_Name" may be; just correct the same.

dv.RowFilter = "Site_Name LIKE '%" + textBox2.Text + "%'";

Alternatively, enclose the column name in square brackets like below:

dv.RowFilter = "[Site Name] LIKE '%" + textBox2.Text + "%'";

Refer this: http://www.csharp-examples.net/dataview-rowfilter/


By the way, it seems you are loading all your data in DataTable and then filtering it. Is this really needed? You can consider using OLEDB provider and use filters while loading data from Excel to DataTable itself. This way, whatever you get in DataTable is itself filtered. Refer this : https://www.aspsnippets.com/Articles/Read-Excel-file-using-OLEDB-Data-Provider-in-C-Net.aspx

Upvotes: 1

Related Questions