c# datasource search

N

nologo

Hi,

abit a vague question, but i'm looking for a process to search a
displayed datagrid for a value. For example, an application displays
a datagrid..I wish to be able to type in a value into a text box and
display that row from the datasource.

Cheers in advance!
 
N

Nicholas Paldino [.NET/C# MVP]

If you are doing this for any DataGridView, then you will have to cycle
through all of the rows and columns to check for your value.

If you know specifically the type of the data source that you are
dealing with, then you could work with that.

The only generalization I can suggest making is checking the data source
to see if it implements IBindingListView. If it does, then you can apply a
filter (through the Filter property) to the data source, and the grid should
update accordingly. However, if that's not there, then you will need to
either resort to some other pattern/logic based on the type (if you know
it), or cycle through the values that the grid is displaying.
 
M

Marc Gravell

Minor additional; related to IBindingListView, there is also an
IBindingList.SupportsSearching/IBindingList.Find pair, but a: they
aren't implemented by all sources (BindingList<T>, for example,
doesn't - although DataView does), and b: it probably isn't worth it ;-
p

Probably the most pragmatic approach is (as Nicholas mentions) a
simple loop - either over the grid cells (since they are likely to
already be text), or over the properties (might need some
System.ComponentModel work here...). Just look at the rows etc of the
grid...?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top