I cannot get the radgrid to filter at all. Additionally, I am trying to filter by multiple columns and I don't think that is currently
possible with the way it is written now. I have to have this working by Friday, so any help is GREATLY appreciated. I took this
template from another post that incuded a textbox for the filter. Thank you. Code below.
ASPX Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="Rad" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.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" />
<asp:Label ID="l" runat="server" />
<Rad:RadMenu ID="TabMenu" runat="server" OnItemClick="TabMenu_ItemClick" style="position:absolute;left:0px;top:103px;z-index:0;">
<Items>
<Rad:RadMenuItem Text="Choice 1">
</Rad:RadMenuItem>
<Rad:RadMenuItem Text="Choice 2">
</Rad:RadMenuItem>
</Items>
</Rad:RadMenu>
<Rad:RadGrid ID="RadGrid1" runat="server" Skin="Default" Gridlines="None" AllowSorting="True" EnableLinqExpressions="False"
AutoGenerateColumns="True" style="position:absolute;top:129px;left:0px;" OnItemCreated="RG1_ItemCreated">
<PagerStyle Mode="NextPrevAndNumeric" />
<MasterTableView AutoGenerateColumns="True" TableLayout="Fixed" />
<ClientSettings ReorderColumnsOnClient="True" AllowColumnsReorder="True">
<Scrolling AllowScroll="True" UseStaticHeaders="True" SaveScrollPosition="True" />
</ClientSettings>
</Rad:RadGrid>
<asp:SqlDataSource ID="SqlDS1" runat="server"
ConnectionString=..........................."
SelectCommand="SELECT * FROM [Table1]" />
<asp:SqlDataSource ID="SqlDS2" runat="server"
ConnectionString=".........................."
SelectCommand="SELECT * FROM [Table2]" />
</form>
</body>
</html>
Code Behind
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
public partial class _Default : System.Web.UI.Page
{
static string tester;
static RadGrid TestGrid;
static string expression;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TabMenu_ItemClick(object sender, RadMenuEventArgs e)
{
l.Text = expression;
switch (e.Item.Text)
{
case "Choice 1":
RadGrid1.DataSourceID = "SqlDS1";
break;
case "Choice 2":
RadGrid1.DataSourceID = "SqlDS2";
break;
}
}
protected void RG1_ItemCreated(object sender, GridItemEventArgs e)
{
if (e.Item is GridHeaderItem)
{
foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
{
if (!column.Visible)
continue;
((GridHeaderItem)e.Item)[column.UniqueName].Controls.Clear();
Label GridHeadLbl1 = new Label();
GridHeadLbl1.Text = column.HeaderText;
GridHeadLbl1.Style["float"] = "left";
((GridHeaderItem)e.Item)[column.UniqueName].Controls.Add(GridHeadLbl1);
RadMenu RadMenu1 = new RadMenu();
RadMenu1.Style["border"] = "0px";
RadMenu1.Style["float"] = "right";
RadMenu1.Style["margin-right"] = "10px";
RadMenu1.Items.Add(new RadMenuItem(""));
RadMenu1.Items[0].PostBack = false;
RadMenuItem MenuItem1 = new RadMenuItem("Sort Asc");
MenuItem1.Value = column.UniqueName;
RadMenu1.Items[0].Items.Add(MenuItem1);
RadMenuItem MenuItem2 = new RadMenuItem("Sort Des");
MenuItem1.Value = column.UniqueName;
RadMenu1.Items[0].Items.Add(MenuItem2);
RadMenuItem MenuItem3 = new RadMenuItem("Filter");
MenuItem1.Value = column.UniqueName;
RadMenu1.Items[0].Items.Add(MenuItem3);
RadMenuItem MenuSubItem1 = new RadMenuItem();
tester = column.UniqueName;
TestGrid = RadGrid1;
MenuSubItem1.ItemTemplate = new MyTemplate();
MenuSubItem1.Width = Unit.Pixel(150);
MenuSubItem1.Value = column.UniqueName;
MenuItem3.Items.Add(MenuSubItem1);
((GridHeaderItem)e.Item)[column.UniqueName].Controls.Add(RadMenu1);
}
}
}
public class MyTemplate : ITemplate
{
#region Itemplate Members
RadComboBox Combo1;
public void InstantiateIn(Control container)
{
Combo1 = new RadComboBox();
Combo1.AutoPostBack = true;
Combo1.AppendDataBoundItems = true;
Combo1.DataSourceID = TestGrid.DataSourceID;
Combo1.DataTextField = tester;
Combo1.DataValueField = tester;
Combo1.ItemDataBound += new RadComboBoxItemEventHandler(Combo1_ItemDataBound);
Combo1.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(Combo1_SelectedIndexChanged);
// Combo1.SelectedValue = RadGrid1.OwnerTableView.GetColumn(tester).CurrentFilterValue;
container.Controls.Add(Combo1);
}
public void Combo1_SelectedIndexChanged(object sender, EventArgs e)
{
RadMenuItem item = ((RadComboBox)sender).Parent as RadMenuItem;
expression = "";
RadGrid RadGrid1 = (RadGrid)item.Page.FindControl("RadGrid1");
GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(item.Value);
if (column.DataType == typeof(string))
{
expression = String.Format(@"{0}.Contains(""{1}"")", item.Value, ((RadComboBox)sender).SelectedValue);
}
else
{
expression = String.Format("{0} == {1}", item.Value, ((RadComboBox)sender).SelectedValue);
}
RadGrid1.MasterTableView.FilterExpression = expression;
RadGrid1.Rebind();
}
public void Combo1_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
{
if (Combo1.FindItemByText(e.Item.Text).ClientID != e.Item.ClientID)
{
e.Item.Visible = false;
}
}
#endregion
}
}