Question : Filter Grid

Hi

I have a grid with textbox.

I am populating the grid with following code where datasourceaccountnature has the data. Now if when user starts entering the data in grid I want to filter the grid based on text entered by user. How do I do this ? at the same time, when user starts pressing backspace and makes the textbox empty the all records should appear in grid as it was at the time of loading the grid

 private void PopulateGrid()
        {
            var datasourceaccountnature = new EntityCollection<AccountnatureEntity>(new AccountnatureEntityFactory());
            var bucketaccountnature = new RelationPredicateBucket();

            using (var adapteraccountnature = new DataAccessAdapter())
            {
                bucketaccountnature.PredicateExpression.Add(AccountnatureFields.Flag == StandardFlag.recordvalidflag);

                adapteraccountnature.FetchEntityCollection(datasourceaccountnature, bucketaccountnature, 0,
                                                           new SortExpression(AccountnatureFields.Description |
                                                                              SortOperator.Ascending));

                dgridaccountnature.DataSource = datasourceaccountnature;
            }
        }

Answer : Filter Grid

How to filter your data source depends on what type of datasource you use...

For DataTable, you can use BindingSource.Filter or DataView or in some cases SqlDataSourceView.FilterExpression.

From your code above, it looks like the datasource you use (datasourceaccountnature) is a collection (of EntityCollection<T>) type.

Typically for collections as datasource in gridview, the filtering can be done as shown in the code below:

See if this helps.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
public class YourClass
        {
            EntityCollection<AccountnatureEntity> datasourceaccountnature;

            private void PopulateGrid()
            {
                // datasourceaccountnature is now a class member

                datasourceaccountnature = new EntityCollection<AccountnatureEntity>(new AccountnatureEntityFactory());

                var bucketaccountnature = new RelationPredicateBucket();

                ... // other code not shown here
            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                // datasourceaccountnature is the one you used in PopulateGrid method in your code.
                // Make datasourceaccountnature as member of the class so that we can use it here.

                dataGridView1.DataSource = datasourceaccountnature.FindAll(FilterList);
            }

            private bool FilterList(AccountnatureEntity a)
            {
                // to filter by a field of AccountnatureEntity
                // return true if that field value contains the text entered by user in textBox1
                // otherwise return false.

                // for example if AccountnatureEntity has a field named 'Title', then 
                return a.Title.Contains(textBox1.Text);
            }
        }
Random Solutions  
 
programming4us programming4us