c# - Filtering a GridView programatically with Checkboxes -
i'm creating project tracker asp.net wbsite c# codebehind. default view has gridview queries sql server db, , 1 of columns project status (projstatus), can have 5 different options (in progress, pending, complete, postponed, cancelled).
now, need create incremental filter using checkbox list. example, if select checkbox "in progress" want see "in progress" projects, if check "pending" checkbox want see both , forth.
i had 3 status (in progress, pending , complete) , had solved problem hard coding each checkbox combination using if statements, since there's 5 there's many possible combinations , take me long that. suggestions programatically in codebehind?
thank you!
edit:
i using radiobuttons had change earlier today @ manager's request. code using, combined filterexpression on sqldatasource
if (radiostatus.selecteditem.value == "active") { sqldatasource1.filterexpression = "[proj_status] = 'active'"; gridview1.databind(); } else if (radiostatus.selecteditem.value == "pending") { sqldatasource1.filterexpression = "[proj_status] = 'pending'"; gridview1.databind(); } else if (radiostatus.selecteditem.value == "postponed") { sqldatasource1.filterexpression = "[proj_status] = 'postponed'"; gridview1.databind(); } else if (radiostatus.selecteditem.value == "cancelled") { sqldatasource1.filterexpression = "[proj_status] = 'cancelled'"; gridview1.databind(); } else { sqldatasource1.filterexpression = "[proj_status] = 'complete'"; gridview1.databind(); }
your explanation lacks implementation details logic should relatively similar anyway. low number of checkboxes, believe use linq way without mapping checkboxes appropriate properties (in scenario many properties you'd keep code dry).
list<project> allprojects = yourquery.tolist(); // assuming "project" type list<project> filteredprojects = new list<project>(); if (inprogresscheckbox.ischecked) filteredprojects.addrange(allprojects.where(p => p.inprogress)); if (pendingcheckbox.ischecked) filteredprojects.addrange(allprojects.where(p => p.pending)); if (completecheckbox.ischecked) filteredprojects.addrange(allprojects.where(p => p.complete)); if (postponedcheckbox.ischecked) filteredprojects.addrange(allprojects.where(p => p.postponed)); if (cancelledcheckbox.ischecked) filteredprojects.addrange(allprojects.where(p => p.cancelled)); filteredprojects = filteredprojects.distinct();
this not optimized, should job enough. you'd have bind filteredprojects
grid's datasource.
Comments
Post a Comment