using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;

namespace _1413854_GridCustomFiltering
{
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
        }

        private void RadForm1_Load(object sender, EventArgs e)
        { 
            this.customersTableAdapter.Fill(this.nwindDataSet.Customers);

            this.radGridView1.BestFitColumns();

            this.radGridView1.EnableFiltering = true;
            this.radGridView1.EnableCustomFiltering = true;
            this.radGridView1.ShowFilteringRow = false;
            this.radGridView1.CustomFiltering += radGridView1_CustomFiltering;
        }

        private void radGridView1_CustomFiltering(object sender, GridViewCustomFilteringEventArgs e)
        {
            if (string.IsNullOrEmpty(this.filterTextBox.Text)) 
            { 
                this.radGridView1.BeginUpdate(); 
                e.Visible = true; 
                for (int i = 0; i < this.radGridView1.ColumnCount; i++) 
                { 
                    e.Row.Cells[i].Style.Reset();                    
                }
 
                this.radGridView1.EndUpdate(false); 
                return; 
            }
            this.radGridView1.BeginUpdate(); 
            e.Visible = false; 
            for (int i = 0; i < this.radGridView1.ColumnCount; i++) 
            { 
                string text = e.Row.Cells[i].Value.ToString(); 
                if (text.IndexOf(this.filterTextBox.Text, 0, StringComparison.InvariantCultureIgnoreCase) >= 0) 
                { 
                    e.Visible = true; 
                    e.Row.Cells[i].Style.CustomizeFill = true; 
                    e.Row.Cells[i].Style.DrawFill = true; 
                    e.Row.Cells[i].Style.BackColor = Color.FromArgb(201, 252, 254); 
                }
                else 
                { 
                    e.Row.Cells[i].Style.Reset();                     
                }
 
                this.radGridView1.EndUpdate(false); 
            }
        }

        private void filterTextBox_TextChanged(object sender, EventArgs e)
        {
            this.radGridView1.MasterTemplate.Refresh();
        }
    }
}