Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8309857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:15:06+00:00 2026-06-08T19:15:06+00:00

Using datagridview bound to BindingSource control bound to a LINQ to SQL class, I wonder how

  • 0

Using datagridview bound to BindingSource control bound to a LINQ to SQL class, I wonder how to position the bindingSource to a specific record, that is, when I type a Product name in a textbox, the bindingsource should move to that specific product. Here is my code:

In my form 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<Product> list = new FindAbleBindingList<Product>(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;
        }
    }

In the program class:

    public class FindAbleBindingList<T> : BindingList<T>
    {

        public FindAbleBindingList()
            : base()
        {
        }

        public FindAbleBindingList(List<T> 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
        }
    }

How can I implement the find method to make it work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:15:08+00:00Added an answer on June 8, 2026 at 7:15 pm

    You can combine the BindingSource.Find() method with the Position property.

    For example, if you have something like this in your TextBox changed event handler:

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;
        int index = bs.Find("Product", tb.Text);
    
        if (index >= 0)
        {
            bs.Position = index;
        }
    }
    

    This of course will depend on a lot of things like the particular implementation of the Find method the data source for the binding source has.

    In a question you asked a little while ago I gave you an implementation for Find which worked with full matches. Below is a slightly different implementation that will look at the start of the property being inspected:

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

    Do note that the above method is case sensitive – you can change StartsWith to be case insensitive if you need.


    One key thing to note about the way .Net works is that the actual type of an object is not sufficient all the time – the declared type is what consuming code knows about.

    This is the reason why you get a NotSupported exception when calling the Find method, even though your BindingList implementation has a Find method – the code that receives this binding list doesn’t know about the Find.

    The reason for that is in these lines of code:

    dc = new NorthwindDataContext();
    
    var qry = (from p in dc.Products
               select p).ToList();
    
    FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
    
    productBindingSource.DataSource = list.OrderBy(o => o.ProductName);
    

    When you set the data source for the binding source you include the extension method OrderBy – Checking this shows that it returns IOrderedEnumerable, an interface described here on MSDN. Note that this interface has no Find method, so even though the underlying FindableBindingList<T> supports Find the binding source doesn’t know about it.

    There are several solutions (the best is in my opinion to extend your FindableBindingList to also support sorting and sort the list) but the quickest for your current code is to sort earlier like so:

    dc = new NorthwindDataContext();
    
    var qry = (from p in dc.Products
               select p).OrderBy(p => p.ProductName).ToList();
    
    FindAbleBindingList<Product> list = new FindAbleBindingList<Product>(qry);
    
    productBindingSource.DataSource = list;
    

    In WinForms there are no entirely out of the box solutions for the things you are trying to do – they all need a little bit of custom code that you need to put together to match just your own requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Windows forms and linq to Sql, I bound a datagridview to Products Table,
I am using a DataGridView in a Win Forms app that is bound to
My DataGridView control currently sorts using the Sort property of the bound data, but
I've got a DataGridView (DGV) that is not bound to a table in SQL
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I have a windows form where I am using DataGridView control. It is bind
I am using the DataGridView and Wrap=True . But when I type in text
I am using a DataGridView control in my app at the moment and I
I was wondering if when using the datagridview control you can remove the thing
I'm running into an issue using a DataGridView bound to a iBindingListView implementation (third

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.