First let's create a table in our database that's called Person.
Now drag a TextBox control to your design and change the AutoCompleteMode property. We'll be using the Suggest option.
Next, change the AutoCompleteSource to CustomSource.
Ok, great!
Now, using Entity Framework 4, we'll retrieve a list of Names from the database.

And that wraps it up!
You now have an autocomplete textbox.
I'd like to ask the community where would the best place to actually retrieve the information be? On KeyDown? But that would hammer the database a lot. Maybe on a Timer, refresh the list of known names?
Let's brainstorm!
create table Person( PersonID int primary key identity(1,1), Name nvarchar(256) )
Now drag a TextBox control to your design and change the AutoCompleteMode property. We'll be using the Suggest option.
Next, change the AutoCompleteSource to CustomSource.
Ok, great!
Now, using Entity Framework 4, we'll retrieve a list of Names from the database.
AutoCompleteStringCollection names = new AutoCompleteStringCollection();
private void MainForm_Load(object sender, EventArgs e)
{
OurEntities db = new OurEntities();
var peopleFromDatabase = db.People;
foreach (var p in peopleFromDatabase)
{
names.Add(p.Name);
}
textBox1.AutoCompleteCustomSource = names;
}

And that wraps it up!
I'd like to ask the community where would the best place to actually retrieve the information be? On KeyDown? But that would hammer the database a lot. Maybe on a Timer, refresh the list of known names?
Let's brainstorm!
4 Comments On This Entry
Page 1 of 1
Core
01 February 2011 - 02:56 PM
One drawback of your method is that you are loading an entire collection locally. What if there are 1,000 suggestions? What about 7,000,000? That would have a performance footprint on the app that will manage the storage. There is nothing wrong with querying a database as long as you optimize the query and it extracts exactly the words needed with a char limit, so that you won't have to load hundreds of results at once.
Nakor
01 February 2011 - 05:51 PM
better yet, create a property that allows you to set the minimum number of chars needed to start fetching results.
Nakor
01 February 2011 - 05:52 PM
you could also set a size limit on the number of items returns, like only return the top 100 results
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
About Me

Bienvenidos! I'm a USA ex-pat living in Bolivia for the past 10 years. Web development is my forte with a heavy lean for usability and optimization. I'm fluent in both English and Spanish. I guest write for the popular Python website Python Central. Visit my website.
My Blog Links
Recent Entries
-
-
-
DevOps for dummies - VPS Configuration from scratch - Rails, Nginx, PostgreSQL.
on May 22 2013 02:36 PM
-
-
Recent Comments
-
Sergio Tapia on May 24 2013 12:33 PM
DevOps for dummies - VPS Configuration from scratch - Rails, Nginx, PostgreSQL.
-
Lemur on May 23 2013 04:53 PM
DevOps for dummies - VPS Configuration from scratch - Rails, Nginx, PostgreSQL.
-
laytonsdad on Apr 30 2013 11:30 AM
Dream.In.Code Badge Generator! Share your flair on your site or blog.
-
-
Jstall on Nov 04 2012 09:18 AM
The Pragmatic Bookshelf mega blowout sale - 40% off select Ruby on Rails books.



4 Comments








|