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

Does Radcontrols have a searchbox/filter similar to JqGrid's?

3 Answers 92 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 23 Jul 2012, 06:07 PM
http://www.trirand.net/demoaspnetmvc.aspx - in the 'ship name' column. It has a nice auto complete filter which I can't see in the RadControls demos, the closest I found were: http://demos.telerik.com/aspnet-ajax/combobox/examples/functionality/filteringcombo/defaultcs.aspx

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Jul 2012, 04:48 AM
Hello Steve,

RadGrid supports google like filtering. Here is the sample code.
aspx:
<telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="false" AllowPaging="true" AllowSorting="true" AllowFilteringByColumn="true" Width="560px" runat="server" OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid1_NeedDataSource" oncolumncreating="RadGrid1_ColumnCreating">
            <PagerStyle Mode="NumericPages" />
</telerik:RadGrid>
C#:
DataTable dt;
protected void Page_Load(object sender, EventArgs e)
{
        if (!IsPostBack)
        {
            dt = GetDataTable("SELECT Country, City, PostalCode FROM Customers");
            this.RadGrid1.MasterTableView.Columns.Clear();
            foreach (DataColumn dataColumn in dt.Columns)
            {
                MyCustomFilteringColumnCS gridColumn = new MyCustomFilteringColumnCS();
                this.RadGrid1.MasterTableView.Columns.Add(gridColumn);
                gridColumn.DataField = dataColumn.ColumnName;
               gridColumn.HeaderText = dataColumn.ColumnName;
               
            }
        }
    }
    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        dt = GetDataTable("SELECT Country, City, PostalCode FROM Customers");
        this.RadGrid1.DataSource = dt;
    }
    
 
        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;
                }
            }
        }
        public static DataTable GetDataTable(string query)
        {
            string ConnString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString3"].ConnectionString;
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(query, conn);
 
            DataTable myDataTable = new DataTable();
 
            conn.Open();
            try
            {
                adapter.Fill(myDataTable);
            }
            finally
            {
                conn.Close();
            }
            return myDataTable;
        }
 
        protected void RadGrid1_ColumnCreating(object sender, GridColumnCreatingEventArgs e)
        {
            if ((e.ColumnType == typeof(MyCustomFilteringColumnCS).Name))
            {
                e.Column = new MyCustomFilteringColumnCS();
 
            }
        }
}
//class MyCustomFilteringColumnCS
 public class MyCustomFilteringColumnCS : GridBoundColumn
    {
 
        public static string ConnectionString
        {
            get { return ConfigurationManager.ConnectionStrings["NorthwindConnectionString3"].ConnectionString; }
        }
 
      
        protected override void SetupFilterControls(TableCell cell)
        {
            base.SetupFilterControls(cell);
            cell.Controls.RemoveAt(0);
            RadComboBox combo = new RadComboBox();
            combo.ID = ("RadComboBox1" + this.UniqueName);
            combo.ShowToggleImage = false;
            combo.Skin = "Office2007";
            combo.EnableLoadOnDemand = true;
            combo.AutoPostBack = true;
            combo.MarkFirstMatch = true;
            combo.Height = Unit.Pixel(100);
            combo.ItemsRequested += this.list_ItemsRequested;
            combo.SelectedIndexChanged += this.list_SelectedIndexChanged;
            cell.Controls.AddAt(0, combo);
            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)
        {
            ((RadComboBox)o).DataTextField = this.DataField;
            ((RadComboBox)o).DataValueField = this.DataField;
            ((RadComboBox)o).DataSource = GetDataTable("SELECT DISTINCT " + this.UniqueName + " FROM Customers WHERE " + this.UniqueName + " LIKE '" + e.Text + "%'");
            ((RadComboBox)o).DataBind();
        }
 
        private void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o).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)
        {
            SqlConnection conn = new SqlConnection(ConnectionString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            adapter.SelectCommand = new SqlCommand(query, conn);
 
            DataTable myDataTable = new DataTable();
 
            conn.Open();
            try
            {
                adapter.Fill(myDataTable);
            }
            finally
            {
                conn.Close();
            }
            return myDataTable;
        }
 
    }
Also take a look at the following demo which implements the same.
Grid / Google-like Filtering

Thanks,
Shinu.
0
Steve
Top achievements
Rank 1
answered on 24 Jul 2012, 05:51 PM
Hi, can you change the filter to match keywords "contains" rather than "begins with"?
0
Shinu
Top achievements
Rank 2
answered on 25 Jul 2012, 04:31 AM
Hi,

Try setting the filter function as shown below.
C#:
private void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
{
   GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o).NamingContainer;
   if ((this.UniqueName == "Index"))
   {
     filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
   }
}

Thanks,
Shinu.
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Steve
Top achievements
Rank 1
Share this question
or