Hello,
I'm using the following to provide checkboxes for row selection in one of my grids:
<telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"> |
<HeaderStyle Width="20px"></HeaderStyle> |
</telerik:GridClientSelectColumn> |
I'd like to be able to filter the contents of the grid based upon whether or not a row is selected. E.g. display all rows, only those selected, or only those not selected. I'm figuring it's not as simple as just setting AllowFilteringByColumn="true", but would like to know if it's possible through built-in means or if I have to do it through some custom code. As simple as this seems, I couldn't find anything about it in the forums.
Currently, when setting AllowFilteringByColumn to true, I get the filter boxes on all of my other columns, but nothing on this one as for filter options.
I'm using Q32008 .NET 3.5 version of the controls.
Any help is greatly appreciated.
Thanks in advance,
Marty
14 Answers, 1 is accepted
Try the following code snippet for achieving the functionanlity.
ASPX:
<FilterTemplate> |
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" > |
<asp:ListItem Text="Selected"></asp:ListItem> |
<asp:ListItem Text="Not Selected"></asp:ListItem> |
<asp:ListItem Text="All"></asp:ListItem> |
</asp:DropDownList> |
</FilterTemplate> |
CS:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) |
{ |
DropDownList dropdown = (DropDownList)sender; |
if (dropdown.SelectedValue == "Selected") |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
// CheckBox chk = (CheckBox)item["Select"].Controls[0]; |
if (!item.Selected) |
{ |
item.Display = false; |
} |
} |
} |
else if (dropdown.SelectedValue == "Not Selected") |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
// CheckBox chk = (CheckBox)item["Select"].Controls[0]; |
if (item.Selected) |
{ |
item.Display = false; |
} |
} |
} |
else |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
item.Display = true; |
} |
} |
} |
Thanks,
Princy.
I've pasted the entire grid, with your code incorporated below, just in case I have something else wrong causing it not to show. The grid is embedded in a table, inside of a RadPanelBar, if that matters. Is there maybe an option I need to set to true, to allow custom filter templates?
<telerik:RadGrid OnDetailTableDataBind="GrdSysApps_DetailTableDataBind" ID="GrdSysApps" |
runat="server" OnNeedDataSource="GrdSysApps_OnNeedDataSource" AllowMultiRowSelection="True" |
ClientSettings-Scrolling-AllowScroll="true" AutoGenerateColumns="False" Width="99%" |
ClientSettings-Scrolling-ScrollHeight="125" GridLines="None" ShowGroupPanel="true" |
ClientSettings-Scrolling-SaveScrollPosition="true" ClientSettings-Scrolling-UseStaticHeaders="true" |
AllowFilteringByColumn="true"> |
<MasterTableView DataKeyNames="SystemApplicationID"> |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
<Columns> |
<telerik:GridBoundColumn DataField="SystemApplicationID" EmptyDataText="&nbsp;" |
UniqueName="column" Visible="False"> |
</telerik:GridBoundColumn> |
<telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" FilterControlWidth="50px"> |
<HeaderStyle Width="20px"></HeaderStyle> |
<FilterTemplate> |
<asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> |
<asp:ListItem Text="Selected"></asp:ListItem> |
<asp:ListItem Text="Not Selected"></asp:ListItem> |
<asp:ListItem Text="All"></asp:ListItem> |
</asp:DropDownList> |
</FilterTemplate> |
</telerik:GridClientSelectColumn> |
<telerik:GridBoundColumn DataField="Name" EmptyDataText="&nbsp;" |
HeaderText="Name" UniqueName="column2"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Description" EmptyDataText="&nbsp;" HeaderText="Description" |
UniqueName="column3"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="ManagerDel" EmptyDataText="&nbsp;" HeaderText="Mgr Approver" |
UniqueName="column5"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="DirectorDel" EmptyDataText="&nbsp;" HeaderText="Dir Approver" |
UniqueName="column9"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Director2Del" EmptyDataText="&nbsp;" HeaderText="Dir2 Approver" |
UniqueName="column6"> |
</telerik:GridBoundColumn> |
</Columns> |
<DetailTables> |
<telerik:GridTableView DataKeyNames="SystemApplicationID" HierarchyLoadMode="ServerOnDemand" |
AllowSorting="false"> |
<ParentTableRelation> |
<telerik:GridRelationFields MasterKeyField="SystemApplicationID" DetailKeyField="SystemApplicationID" /> |
</ParentTableRelation> |
<Columns> |
<telerik:GridBoundColumn HeaderText="Default System Manager" HeaderButtonType="TextButton" |
DataField="Manager"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn HeaderText="Default System Director" HeaderButtonType="TextButton" |
DataField="Director"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn HeaderText="Default System 2nd Director" HeaderButtonType="TextButton" |
DataField="Director2"> |
</telerik:GridBoundColumn> |
</Columns> |
</telerik:GridTableView> |
</DetailTables> |
</MasterTableView> |
<ClientSettings> |
<Selecting AllowRowSelect="True" /> |
</ClientSettings> |
<FilterMenu EnableTheming="True"> |
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> |
</FilterMenu> |
</telerik:RadGrid> |
Indeed, GridClientSelectColumn does not support filtering, and that is why your filter template is not displayed.
However in order to have filterable ClientSelectColumn, you could create your custom column and which inherits from GridClientSelectColumn and override the SupportsFiltering method. Note that, you will need to handle the new column filtering by yourself then.
Check it out and let me know if this works for you.
Regards,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Once implmented, this is working perfectly, exactly as I need it to. Thank you for the example and detailed description.
Marty
Lana/Anybody,
I'd like to ask one more question re: this grid. I need to access the DropDownList embedded in my filtertemplate from code-behind from outside of the event handlers for it. I've tried every way I know of to get a reference to it, but none seem to work. So, if I have a reference to the grid, how do I go about getting a reference to the control in my FilterTemplate (cboGrdSysAppsFilter) in C# code-behind? Grid as follows:
<telerik:RadGrid OnDetailTableDataBind="GrdSysApps_DetailTableDataBind" ID="GrdSysApps" |
runat="server" OnNeedDataSource="GrdSysApps_OnNeedDataSource" AllowMultiRowSelection="True" |
ClientSettings-Scrolling-AllowScroll="true" AutoGenerateColumns="False" Width="99%" |
ClientSettings-Scrolling-ScrollHeight="150" GridLines="None" ShowGroupPanel="true" |
ClientSettings-Scrolling-SaveScrollPosition="true" ClientSettings-Scrolling-UseStaticHeaders="true" |
AllowFilteringByColumn="true" AllowSorting="true" ClientSettings-AllowColumnsReorder = "false"> |
<MasterTableView DataKeyNames="SystemApplicationID" ClientDataKeyNames="SystemApplicationID" > |
<RowIndicatorColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</RowIndicatorColumn> |
<ExpandCollapseColumn> |
<HeaderStyle Width="20px"></HeaderStyle> |
</ExpandCollapseColumn> |
<Columns> |
<telerik:GridBoundColumn DataField="SystemApplicationID" EmptyDataText="&nbsp;" |
UniqueName="column" Visible="False"> |
</telerik:GridBoundColumn> |
<custom:CustomClientSelectColumn UniqueName="SelectedRows"> |
<filtertemplate> |
<asp:DropDownList Font-Size="X-Small" ID="cboGrdSysAppsFilter" AutoPostBack="true" runat="server" OnSelectedIndexChanged="cboSelected_SelectedIndexChanged"> |
<asp:ListItem>All</asp:ListItem> |
<asp:ListItem>Selected</asp:ListItem> |
<asp:ListItem>Not Selected</asp:ListItem> |
</asp:DropDownList> |
</filtertemplate> |
</custom:CustomClientSelectColumn> |
<telerik:GridBoundColumn DataField="Name" EmptyDataText="&nbsp;" HeaderText="Name" |
UniqueName="column2"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Description" EmptyDataText="&nbsp;" HeaderText="Description" |
UniqueName="column3"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="ManagerDel" EmptyDataText="&nbsp;" HeaderText="Mgr Approver" |
UniqueName="column5"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="DirectorDel" EmptyDataText="&nbsp;" HeaderText="Dir Approver" |
UniqueName="column9"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn DataField="Director2Del" EmptyDataText="&nbsp;" HeaderText="Dir2 Approver" |
UniqueName="column6"> |
</telerik:GridBoundColumn> |
</Columns> |
<DetailTables> |
<telerik:GridTableView DataKeyNames="SystemApplicationID" HierarchyLoadMode="ServerOnDemand" |
AllowSorting="false" AllowFilteringByColumn="false" Name="OriginalOwners"> |
<ParentTableRelation> |
<telerik:GridRelationFields MasterKeyField="SystemApplicationID" DetailKeyField="SystemApplicationID" /> |
</ParentTableRelation> |
<Columns> |
<telerik:GridBoundColumn HeaderText="Default System Manager" HeaderButtonType="TextButton" |
DataField="Manager"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn HeaderText="Default System Director" HeaderButtonType="TextButton" |
DataField="Director"> |
</telerik:GridBoundColumn> |
<telerik:GridBoundColumn HeaderText="Default System 2nd Director" HeaderButtonType="TextButton" |
DataField="Director2"> |
</telerik:GridBoundColumn> |
</Columns> |
</telerik:GridTableView> |
</DetailTables> |
</MasterTableView> |
<ClientSettings AllowDragToGroup="true"> |
<Selecting AllowRowSelect="true" /> |
<ClientEvents OnRowSelected="GrdSysApps_RowSelected" OnRowDeselected="GrdSysApps_RowDeselected" |
OnRowCreated="GrdSysApps_RowCreated" OnCommand="RaiseCommand"/> |
</ClientSettings> |
<FilterMenu EnableTheming="True"> |
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation> |
</FilterMenu> |
</telerik:RadGrid> |
Thanks,
Marty
You could access the DropDownList through the grid FilteringItem. This could be on ItemCreated/ItemDataBound grid events on in other event by getting GridFilteringItem as below:
<asp:Button ID="Button1" runat="server" Text="Get Filter DropDownList value" |
OnClick="Button1_Click" /> |
protected void Button1_Click(object sender, EventArgs e) |
{ |
GridItem[] filterItems = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem); |
foreach (GridItem item in filterItems) |
{ |
DropDownList filterDropDwnList = (item as GridFilteringItem)["SelectedRows"].FindControl("cboGrdSysAppsFilter") as DropDownList; |
this.form1.Controls.Add(new LiteralControl(filterDropDwnList.SelectedValue)); |
} |
} |
Find more about accessing grid cells and rows here.
Give it a try and let me know how it goes.
Sincerely yours,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
I used a slight variation of that, and it works. Thank you very much for your time and useful advice.
The following code acheived my goal:
foreach (GridFilteringItem filterItem in grdSysAppsRef.MasterTableView.GetItems(GridItemType.FilteringItem)) |
{ |
DropDownList combo = (DropDownList)filterItem.FindControl("cboGrdSysAppsFilter"); |
Marty
I have one more question about this.
Since I'm using a custom FilterTemplate as described above, I'm having a hard time figuring out how to make the other filters on the other grid columns recognize my FilterTeplate.
I'm trying to make sure that the whatever setting I've set on my custom filter is retained, and considered, when doing sorting/grouping/filtering on the other columns. I figure I can use a Session variable to store its selected value whenever changed, however, at what point in the Event lifecycle will all the data be available for me to parse through all of my items to apply my custom filter? Also, how would I make sure that when I've filtered based on my custom filter, that applying another filter will only consider the currently visible items? Would it involve the PreRender event? I've tried using it, but the results are not quite right.
I hope this makes sense, and thank you again.
Marty
Indeed, the desired behavior is the default behavior of RadGrid. When you filter by any column, then filtering on other column filters only record returned after the first filtering.
Please check out this demo for getting better idea on how the filtering with FilterTemplates work.
All the best,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
I'd like to suggest however, that perhaps for a future release, that something like this be integrated with the clientSelectColumn. I've spent the entire week figuring out a way to implement filtering on this column (only for selected/not selected/all items) and finally got it but don't think that I'm the only person that might find such functionality useful.
Thanks again.
I am happy to hear you found a solution for your case.
And yes, I will forward your suggestion for implementation or at least will add new code library implementing filtering for GridClientSelectColumn.
If any issues arise, do not hesitate to type again.
Greetings,
Iana
the Telerik team
Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Unknown server tag 'custom:MyClientSelectColumn'.
Yes I have it registerd
<%
@ Register Namespace="EA_Project" TagPrefix="custom" %>
<custom:MyClientSelectColumn>
<filtertemplate>
<asp:DropDownList ID="dropDownList" runat="server">
<asp:ListItem>Selected</asp:ListItem>
<asp:ListItem>Not Selected</asp:ListItem>
</asp:DropDownList>
</filtertemplate>
</custom:MyClientSelectColumn>
Please when you give examples, make sure all the code is in the project and it runs, Your example does not work either for me.
Thanks for your help.
If you are discussing this online demo, I have to say that it behaves properly on my end and presents the correct approach for adding FilterTemplate to RadGrid columns.
Please let me know what the exact issue you are facing is, and why you are finding the demo unpleasant?
Regards,
Maria Ilieva
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.