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

Problem regarding Filtering with Google-Suggest Style

1 Answer 73 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Roland
Top achievements
Rank 1
Roland asked on 17 Feb 2009, 12:37 PM
Hi!

I tried to create a sample program which is able to filter items in an rad grid in google auto suggest style. My problem is that the ItemsRequested mehtod of the radcombobox is never fired. So i tried to add values in the box during creation, this works ok but if i click one item in the list it happens nothing. If i add no value in the box, it shows loading when i click in the RadComboxBox and nothing happens, i does not even fire a postback event.

Ajax Settings
<asp:ScriptManager ID="ScriptManager" runat="server" /> 
    <radg:RadAjaxManager ID="AjaxManager" runat="server"
        <AjaxSettings> 
            <radg:AjaxSetting AjaxControlID="RadGrid1"
                <UpdatedControls> 
                    <radg:AjaxUpdatedControl ControlID="RadGrid1" /> 
                </UpdatedControls> 
            </radg:AjaxSetting> 
        </AjaxSettings> 
    </radg:RadAjaxManager> 

Grid Declaration

<radg:radgrid id="RadGrid1" runat="server" autogeneratecolumns="False" 
            allowsorting="True" allowfilteringbycolumn="True" width="1024px"  
            Height="800px" skin="Telerik"  
             onitemcommand="RadGrid1_ItemCommand"  
            onneeddatasource="RadGrid1_NeedDataSource">   
                <MasterTableView AllowFilteringByColumn="True" />  
                <PagerStyle Mode="NumericPages" />  
                <ClientSettings> 
                    <Scrolling AllowScroll="true" /> 
                </ClientSettings> 
            </radg:radgrid> 

CustomFIlter Class

public class MyCustomFilteringColumn : GridBoundColumn 
    { 
        public MyCustomFilteringColumn() { } 
        public MyCustomFilteringColumn(string dataFieldName, string headerText) 
        { 
            this.DataField = dataFieldName; 
            this.HeaderText = headerText; 
        } 
 
        public static string connectionString 
        { 
            get 
            { 
                string dbPath = System.Web.HttpContext.Current.Server.MapPath("~\\Nwind.mdb"); 
                return ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbPath); 
            } 
        } 
 
        protected override void SetupFilterControls(TableCell cell) 
        { 
            base.SetupFilterControls(cell); 
            cell.Controls.RemoveAt(0); 
            RadComboBox combo = new RadComboBox(); 
            combo.ID = ("RadComboBox1" + this.DataField); 
            combo.ShowToggleImage = false
            combo.Skin = "Telerik"
            combo.EnableLoadOnDemand = true
            combo.AutoPostBack = true
            combo.MarkFirstMatch = true
            combo.Height = Unit.Pixel(100); 
            combo.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(this.list_ItemsRequested); 
            combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(this.list_SelectedIndexChanged); 
            cell.Controls.AddAt(0, combo); 
 
 
 
            List<ConfigurationInformation> infos = (List<ConfigurationInformation>)this.Owner.DataSource; 
 
            List<string> listsource = new List<string>(); 
 
            foreach (ConfigurationInformation info in infos) 
            { 
                if (this.DataField == "City"
                    listsource.Add(info.City); 
                else if (this.DataField == "CustomerName"
                    listsource.Add(info.CustomerName); 
                else if (this.DataField == "Country"
                    listsource.Add(info.Country); 
                else if (this.DataField == "OrderDate"
                    listsource.Add(info.OrderDate.ToString()); 
            } 
            combo.DataSource = listsource; 
            cell.Controls.RemoveAt(1); 
        } 
 
        protected override void SetCurrentFilterValueToControl(TableCell cell) 
        { 
            base.SetCurrentFilterValueToControl(cell); 
            RadComboBox combo = (RadComboBox)cell.Controls[0]; 
            if (this.CurrentFilterValue != string.Empty) 
            { 
                combo.Text = this.CurrentFilterValue; 
            } 
        } 
 
        protected override string GetCurrentFilterValueFromControl(TableCell cell) 
        { 
            RadComboBox combo = (RadComboBox)cell.Controls[0]; 
            return combo.Text; 
        } 
 
        private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e) 
        { 
            (o as RadComboBox).DataTextField = this.DataField; 
            (o as RadComboBox).DataValueField = this.DataField; 
 
            if (((RadComboBox)o).DataTextField == "CustomerName"
            { 
                List<ConfigurationInformation> test = (List<ConfigurationInformation>)this.Owner.DataSource; 
                (o as RadComboBox).DataSource = test.Distinct<ConfigurationInformation>(new CustomerNameComparer()); 
                /*this.Owner.DataSource*/ 
            } 
            else 
                (o as RadComboBox).DataSource = GetDataTable("SELECT DISTINCT " + this.UniqueName + " FROM Customers WHERE " + this.UniqueName + " LIKE '" + e.Text + "%'"); 
             
            (o as RadComboBox).DataBind(); 
        } 
 
        private void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e) 
        { 
            GridFilteringItem filterItem = (GridFilteringItem)(o as RadComboBox).NamingContainer; 
            if (this.UniqueName == "Index"
            { 
                filterItem.FireCommandEvent("Filter"new Pair("EqualTo"this.UniqueName)); 
            } 
            filterItem.FireCommandEvent("Filter"new Pair("Contains"this.UniqueName)); 
        } 
 
        public static DataTable GetDataTable(string query) 
        { 
            OleDbConnection MyOleDbConnection = new OleDbConnection(connectionString); 
            OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter(); 
            MyOleDbDataAdapter.SelectCommand = new OleDbCommand(query, MyOleDbConnection); 
            DataTable myDataTable = new DataTable(); 
            MyOleDbConnection.Open(); 
            try 
            { 
                MyOleDbDataAdapter.Fill(myDataTable); 
            } 
            finally 
            { 
                MyOleDbConnection.Close(); 
            } 
            return myDataTable; 
        } 
    } 

Default.aspx Code File

 private List<ConfigurationInformation> Configs = new List<ConfigurationInformation>(); 
         
        public static string connectionString 
        { 
            get 
            { 
                string dbPath = System.Web.HttpContext.Current.Server.MapPath("~\\Nwind.mdb"); 
                return ("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + dbPath); 
            } 
        } 
 
        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
        { 
            RadGrid1.DataSource = Configs; 
            //Configs.Distinct(new CustomerNameComparer()); 
        } 
 
        protected void Page_Load(object sender, System.EventArgs e) 
        { 
            if (!IsPostBack) 
            { 
                DataTable dt2 = GetDataTable("SELECT Customers.ContactName as CustomerName, Customers.City, Customers.Country, Orders.OrderDate FROM Customers INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID where Index < 10;"); 
                foreach (DataRow row in dt2.Rows) 
                    Configs.Add(new ConfigurationInformation( 
                        (string)row["CustomerName"], (string)row["Country"], (string)row["City"], (DateTime)row["OrderDate"])); 
 
                //ds = GetDataSet("SELECT Country, City, Index FROM Customers"); 
                this.RadGrid1.MasterTableView.Columns.Clear(); 
 
                this.RadGrid1.MasterTableView.Columns.Add(new MyCustomFilteringColumn("CustomerName""Customer Name")); 
                this.RadGrid1.MasterTableView.Columns.Add(new MyCustomFilteringColumn("City""City")); 
                this.RadGrid1.MasterTableView.Columns.Add(new MyCustomFilteringColumn("Country""Country")); 
                this.RadGrid1.MasterTableView.Columns.Add(new MyCustomFilteringColumn("OrderDate""Order Date")); 
            } 
        } 
 
        //protected void RadGrid1_ColumnCreating(object sender, GridColumnCreatingEventArgs e) 
        //{ 
        //    if (e.ColumnType == typeof(MyCustomFilteringColumn).Name) 
        //    { 
        //        e.Column = new MyCustomFilteringColumn(); 
        //    } 
        //} 
 
        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) 
        { 
            if (e.CommandName == "Filter"
            { 
                foreach (GridColumn column in e.Item.OwnerTableView.Columns) 
                { 
                    column.CurrentFilterValue = string.Empty; 
                    column.CurrentFilterFunction = GridKnownFunction.NoFilter; 
                } 
            } 
        } 
 
        protected void clrFilters_Click(object sender, System.EventArgs e) 
        { 
            foreach (GridColumn column in RadGrid1.MasterTableView.Columns) 
            { 
                column.CurrentFilterFunction = GridKnownFunction.NoFilter; 
                column.CurrentFilterValue = string.Empty; 
            } 
            RadGrid1.MasterTableView.FilterExpression = string.Empty; 
            RadGrid1.MasterTableView.Rebind(); 
        } 
 
        public static DataTable GetDataTable(string query) 
        { 
            OleDbConnection MyOleDbConnection = new OleDbConnection(connectionString); 
            OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter(); 
            MyOleDbDataAdapter.SelectCommand = new OleDbCommand(query, MyOleDbConnection); 
            DataTable myDataTable = new DataTable(); 
            MyOleDbConnection.Open(); 
            try 
            { 
                MyOleDbDataAdapter.Fill(myDataTable); 
            } 
            finally 
            { 
                MyOleDbConnection.Close(); 
            } 
            return myDataTable; 
        } 
 
        //public static DataSet GetDataSet(string query) 
        //{ 
        //    OleDbConnection MyOleDbConnection = new OleDbConnection(connectionString); 
        //    OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter(); 
        //    MyOleDbDataAdapter.SelectCommand = new OleDbCommand(query, MyOleDbConnection); 
        //    DataSet myDataSet = new DataSet("Customers"); 
        //    MyOleDbConnection.Open(); 
        //    try 
        //    { 
        //        MyOleDbDataAdapter.Fill(myDataSet); 
        //    } 
        //    finally 
        //    { 
        //        MyOleDbConnection.Close(); 
        //    } 
        //    return myDataSet; 
        //} 
    } 

Can you please help me to get the event fired? I also have a second question regarding the ColumnCreating event, is it nessercery to implement that event and what funcionality is required in there?

greetings
Martin


1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 20 Feb 2009, 07:49 AM
Hello Martin,

Unfortunately if you are creating custom column at runtime you should create it on RadGrid.ColumnCreating event.

You can also create this column in markup of RadGrid or on Page.PreInit which is essentially the same as adding it into markup.

Another option is to use new feature of RadGrid: ASP.NET AJAX grid filter template

Kind regards,
Nikolay
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
Roland
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or