Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is there an easier way to filter rows in the DataGridView of this way ?

C#
private void Reread()
{
    string nameFilter = txtSearch.Text;
    string addressFilter = comboBox1.SelectedIndex > 0 ? comboBox1.Text : "";
    string depFilter = comboBox2.SelectedIndex > 0 ? comboBox2.Text : "";
    DateTime? birthdate = chkBirthdate.Checked ? dateTimePicker1.Value.Date : (DateTime?) null;
    DateTime? fromDate = chkRange.Checked ? dateTimePicker2.Value.Date : (DateTime?)null;
    DateTime? toDate = chkRange.Checked ? dateTimePicker3.Value.Date : (DateTime?)null;

    using (DbDataContext db = new DbDataContext())
    {
        var result = from p in db.Persons
                     where
                        (nameFilter.Length > 0 && p.FullName.Contains(nameFilter) || nameFilter.Length == 0)
                     && (addressFilter.Length > 0 && p.Address == addressFilter || addressFilter.Length == 0)
                     && (depFilter.Length > 0 && p.Department == depFilter || depFilter.Length == 0)
                     && (birthdate == null || (birthdate != null && p.Birthdate == birthdate))
                     && ((fromDate == null || toDate == null) || (fromDate != null && toDate != null && p.Birthdate >= fromDate && p.Birthdate <= toDate))

                     select p;

        var resultData = result.ToList();

        dataGridView1.DataSource = resultData;
    }
Posted
Comments
Sinisa Hajnal 5-Feb-15 2:48am    
There is, put filters as parameters to storeed procedure and call it after changing parameters to return only the data you need from the filter instead of getting every person in the table as datasource :)

Alternative, not necessarily better, but easier to format is to get the data (db.Persons) into datatable object and use DataTable.Select method for which you can prepare filter with all the conditions before using it (more readable).
Ahmed Dabas 5-Feb-15 3:44am    
mr. Sinisa Hajnal , can u give me a real example how can i put filters as parameters to storeed

1 solution

C#
//Build the query in stages taking advantage of deferred execution.
         //select all Persons
         var query = from p in db.Persons select p;
         //apply nameFilter if valid
         if (!string.IsNullOrEmpty(nameFilter))
         {
             query = query.Where(
                 n => n.fullName.Contains(nameFilter));
         }
         //apply addressFilter, if valid
         if (!string.IsNullOrEmpty(addressFilter))
         {
             query = query.Where(a => a.Address == addressFilter);
         }
         //apply departmentFilter, if valid
         if (!string.IsNullOrEmpty(depFilter))
         {
             query = query.Where(d => d.Department == depFilter);
         }
         //apply birthdateFilter, if valid
         if (birthdate != null)
         {
             query = query.Where(b => b.Birthdate == birthdate);
         }
         //apply dateRangeFilter, if valid
         if (birthdate != null && toDate != null && fromDate != null)
         {
             query = query.Where(dr.Birthdate >= fromDate && dr.Birthdate <= toDate);
         }
         //run the query
         var results = query.ToList();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900