Как по коду переместить BindingSource в конкретную запись

Используя datagridview, привязанный к элементу управления BindingSource, связанному с классом LINQ to SQL , мне интересно, как расположить BindingSource в определенной записи, то есть когда я набираю название продукта в текстовом поле, источник привязки должен перейти к этот конкретный продукт. Вот мой код:

В моей форме FrmFind:

    NorthwindDataContext dc;
    private void FrmFind_Load(object sender, EventArgs e)
    {
        dc = new NorthwindDataContext();

        var qry = (from p in dc.Products
                   select p).ToList();

        FindAbleBindingList list = new FindAbleBindingList(qry);

        productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;

        int index = productBindingSource.Find("ProductName", tb.Text);

        if (index >= 0)
        {
            productBindingSource.Position = index;
        }
    }

В классе программы:

    public class FindAbleBindingList : BindingList
    {

        public FindAbleBindingList()
            : base()
        {
        }

        public FindAbleBindingList(List list)
            : base(list)
        {
        }

        protected override int FindCore(PropertyDescriptor property, object key)
        {
            for (int i = 0; i < Count; i++)
            {
                T item = this[i];
                //if (property.GetValue(item).Equals(key))
                if (property.GetValue(item).ToString().StartsWith(key.ToString()))
                {
                    return i;
                }
            }
            return -1; // Not found
        }
    }

Как я могу реализовать метод find, чтобы он работал?

6
задан Peter Mortensen 9 June 2014 в 11:27
поделиться