Datagridview: filenames with bindingsource filter

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
I have a datagridview1 and search textbox1 on my form

On form load I need to fill datagridview1
  • with absolute path+filename of certain filetypes like *.jpg,png,gif,bmp
  • in a specific directory like Application.StartupPath & "\Images"

When user types in search textbox1
  • datagridview1 contents need to be continuously filtered to match searchstring

Can anyone please tell me what is the best method to follow to achieve this.

Should filenames be stored in a temptable, bindingsource filtered by textbox and datagridview datasource set to bindingsource?

I'd appreciate any sample code.

Thanks
 
yes, but i recommend that you dont continuously filter after every keypress because if you have a lot of names it can make the ui very unresponsive

Instead, every time the user presses a key, start a timer (whose interval is set to 100) and reset a counter to 5
Every timer tick, decrement the counter by 1. If the counter is 0, then run the search

This way the search only runs half a second afte rthe last time the user pressed the key. Most users can type faster than one key per half second, so you dont end up with a UI that makes 5 list refinements if the user types 5 chars..

You can see wy this is good if you use winamp and have tens of thousands of songs. Calling up the F3 search and start to type.. uh, something like "arctic"..
10,000 tunes
type A
app searches on A
list contains 9500 entries, took 1 second
type R
app searches on AR
list contains 6500 entries


can you see how repeatedly searching on very short terms makes for a not nice user experience?
 
Agreed. Search should not start until len(searchstring)>2. The timer idea is very interesting. Please do you have some code/pseudo ?

Also I'd appreciate any help with the actual datagridview populating/filtering
 
well i gave you the pseudo, but here we go:

VB.NET:
Private actionCounter as Integer = 5

Sub TextBox1_TextChanged(whatever) Handles TextBox1.TextChanged
  actionCounter = 5
  timer1.Enabled = True
End Sub

Sub Timer1_Tick(whatever) Handles Timer1.Tick
  actionCounter -= 1
  If actionCounter > 0 Then Return

  timer1.Enabled = false 'stop firing events now user stopped typing

  dgvBS.Filter = String.Format("[FileName] LIKE '*.{0}'", extensionTextbox.Text)

End Sub
 
Thanks cjard.

Can I ask you what is the most efficient way to populate the datagridview with filenames of only certain filetypes say, image files, in a specified directory ?
 
Directory.GetXXX() methods allow you to get a list of files based on na wildcard. Windows does extension name determination of file types, other O/S use mime types or file contents so in windows you really need to know the extensions of all files youre after:

Directory.GetFiles("C:\", "*.jpg")
Directory.GetFiles("C:\", "*.png")
Directory.GetFiles("C:\", "*.gif")


If it did mime, which it doesnt, then it might have been simpler, like "image/*"
 
If it did mime, which it doesnt, then it might have been simpler, like "image/*"
Since .Net has a limited image repertoire this wouldn't be of more help.
MSDN said:
GDI+ supports the following file formats: BMP, GIF, EXIG, JPG, PNG and TIFF.
 

Latest posts

Back
Top