This is a migrated thread and some comments may be shown as answers.

RadComboBox Filter

3 Answers 252 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 13 May 2009, 06:37 PM
Hello.  Trying to implement a radcombobox in a radmenu in a radgrid header.  I have everything displaying correctly, however,
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
    }
}

3 Answers, 1 is accepted

Sort by
0
Yavor
Telerik team
answered on 18 May 2009, 08:03 AM
Hi Josh,

One possible option in this case would be to use a filter template. This is demonstrated in the following example.
I hope it gets you started properly.

Sincerely yours,
Yavor
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.
0
Josh
Top achievements
Rank 1
answered on 18 May 2009, 12:18 PM
I have actually got the drop down filtering to work by modifying the filter expression as the
combobox values change, however, the problem I am having is that the comboboxes dont
retain their selected value on postback.  I dont know if them being added dynamically has
anything to do with it.  Any ideas?  Thanks.
0
Yavor
Telerik team
answered on 22 May 2009, 05:46 AM
Hello Josh,

You can store the combo box selected value in session/ViewState, and the in the PreRender event handler for the grid, get a reference to it, and restore its value.

Best wishes,
Yavor
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.
Tags
Grid
Asked by
Josh
Top achievements
Rank 1
Answers by
Yavor
Telerik team
Josh
Top achievements
Rank 1
Share this question
or