views:

267

answers:

1

I keep getting this error when I try to call Find()

public void findTxt(string text)
    {
        BindingSource src = new BindingSource();
        src.DataSource = dataGridView1.DataSource;
        src.Position = src.Find("p_Name", text);    // Specified method is not supported

        if (src.Position == 0 && dataGridView1.Rows[0].Cells[2].Value.ToString() == text)
        {
            MessageBox.Show("Item found!!");
            dataGridView1.CurrentCell = dataGridView1.Rows[src.Position].Cells[2];
        }
        else if (src.Position == 0 && dataGridView1.Rows[0].Cells[2].Value.ToString() != text)
        {
            MessageBox.Show("Item not found!!");
        }
        else
        {
            MessageBox.Show("Item found!!");
            dataGridView1.CurrentCell = dataGridView1.Rows[src.Position].Cells[2];
        }

    }

Edit:

I get that error when calling findText method from another form, However calling this method from the main form doesn't result in such an error.

+2  A: 

It is up to the underlying data-source what operations it supports. I believe that DataTable is the only one that out of the box supports this. You could check (in this case) via:

IBindingListView blv = yourDataSource as IBindingListView;
bool canSearch = blv != null && blv.SupportsSearching;

So; what is the underlying data source? A List<T> (or even BindingList<T>) won't provide this.

Marc Gravell