Hi,
I'm trying to filter a grid from by selecting the filter from a combo.
Let's assume a SQL table with 2 columns: Id (int), Date (DateTime, could be null). The table has rows where Date is null and other rows where it isn't.
I'd like to filter the Date column with the follwing: NoFilter, IsNull, NotIsNull.
I see the content of the grid change but not as expected. The IsNull and NotIsNull filters doesn't work as I'm expecting.
Here's my code:
Any ideas?
Thanks,
Pascal
I'm trying to filter a grid from by selecting the filter from a combo.
Let's assume a SQL table with 2 columns: Id (int), Date (DateTime, could be null). The table has rows where Date is null and other rows where it isn't.
I'd like to filter the Date column with the follwing: NoFilter, IsNull, NotIsNull.
I see the content of the grid change but not as expected. The IsNull and NotIsNull filters doesn't work as I'm expecting.
Here's my code:
| <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication4._Default" %> |
| <%@ Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %> |
| <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> |
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head runat="server"> |
| <title>Untitled Page</title> |
| </head> |
| <body> |
| <form id="form1" runat="server"> |
| <asp:ScriptManager ID="ScriptManager1" runat="server" /> |
| <div> |
| <table> |
| <tr> |
| <td> |
| <asp:Label ID="blFilter" runat="server" Text="Filter:" Width="75" /> |
| </td> |
| <td> |
| <asp:DropDownList ID="DropDownListFilter" runat="server" Width="120" OnSelectedIndexChanged="Filter_OnSelectedIndexChanged" |
| AutoPostBack="True" Style="margin-bottom: 0px"> |
| <asp:ListItem Value="NoFilter">All</asp:ListItem> |
| <asp:ListItem Value="NotIsNull">WithDate</asp:ListItem> |
| <asp:ListItem Value="IsNull" Selected="True">DateIsNull</asp:ListItem> |
| </asp:DropDownList> |
| </td> |
| </tr> |
| </table> |
| </div> |
| <div> |
| <telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" GridLines="None" |
| OnPreRender="RadGrid1_PreRender" AllowFilteringByColumn="True"> |
| <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1"> |
| <RowIndicatorColumn Visible="False"> |
| <HeaderStyle Width="20px"></HeaderStyle> |
| </RowIndicatorColumn> |
| <ExpandCollapseColumn Visible="False" Resizable="False"> |
| <HeaderStyle Width="20px"></HeaderStyle> |
| </ExpandCollapseColumn> |
| <Columns> |
| <telerik:GridBoundColumn DataField="Id" DataType="System.Int64" HeaderText="Id" SortExpression="Id" |
| UniqueName="Id"> |
| </telerik:GridBoundColumn> |
| <telerik:GridBoundColumn DataField="Date" DataType="System.DateTime" HeaderText="Date" |
| SortExpression="Date" UniqueName="Date"> |
| </telerik:GridBoundColumn> |
| </Columns> |
| <EditFormSettings> |
| <PopUpSettings ScrollBars="None"></PopUpSettings> |
| </EditFormSettings> |
| </MasterTableView> |
| </telerik:RadGrid> |
| </div> |
| <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDbConnectionString %>" |
| SelectCommand="SELECT [Id], [Date] FROM [Table_1]"></asp:SqlDataSource> |
| </form> |
| </body> |
| </html> |
| public partial class _Default : System.Web.UI.Page |
| { |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| } |
| protected void Filter_OnSelectedIndexChanged(object sender, EventArgs e) |
| { |
| ListItem item = DropDownListFilter.Items[DropDownListFilter.SelectedIndex]; |
| Filter(item.Value); |
| } |
| protected void RadGrid1_PreRender(object sender, EventArgs e) |
| { |
| // Filter on initial load |
| if (!Page.IsPostBack) |
| { |
| Filter("IsNull"); |
| } |
| } |
| private void Filter(string filter) |
| { |
| GridFilteringItem filterItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem; |
| filterItem.FireCommandEvent("Filter", new Pair(filter, "Date")); |
| } |
| } |
Any ideas?
Thanks,
Pascal