Search Data in DataGridView using Textbox

6 posts, 1 answers
  1. Hiralkumar
    Hiralkumar avatar
    3 posts
    Member since:
    Oct 2012

    Posted 23 Nov 2012 Link to this post

    I wanted to search in the datagrideview using a textbox
    as i write in textbox automatically that value matched with particular row
    is displayed

    https://dl.dropbox.com/u/10717574/project%20problems/search.png

    here is the image 
    here u can see as i type "Hi" Automatically all the rows that contains the "Hi" are filterd and show...
    i want to do like this
    plzzz give me suggestion
  2. Answer
    Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 28 Nov 2012 Link to this post

    Hi Hiralkumar,

    Thank you for writing.

    To achieve your scenario, you need to add a FilterDescriptor to the grid and change its value according to the text box.

    Here is a small sample:
    public Form1()
    {
        InitializeComponent();
     
        Random r = new Random();
        DataTable table = new DataTable();
        table.Columns.Add("ID", typeof(int));
        table.Columns.Add("Name", typeof(string));
        table.Columns.Add("Bool", typeof(bool));
        table.Columns.Add("DateColumn", typeof(DateTime));
     
        for (int i = 0; i < 10; i++)
        {
            table.Rows.Add(i, "Row " + i, r.Next(10) > 5 ? true : false, DateTime.Now.AddHours(i));
        }
     
        this.radGridView1.DataSource = table;
     
        radTextBoxControl1.TextChanged += radTextBoxControl1_TextChanged;
        radTextBoxControl1.Text = "";
     
        radGridView1.EnableFiltering = true;
        FilterDescriptor desc = new FilterDescriptor("Name", FilterOperator.Contains, null);
        radGridView1.FilterDescriptors.Add(desc);
    }
     
    void radTextBoxControl1_TextChanged(object sender, EventArgs e)
    {
        if (radGridView1.FilterDescriptors.Count > 0)
        {
            FilterDescriptor desc = radGridView1.FilterDescriptors[0];
     
            if (string.IsNullOrEmpty(radTextBoxControl1.Text))
            {
                desc.Value = null;
            }
            else
            {
                desc.Value = radTextBoxControl1.Text;
            }
        }
    }

    More information about filtering in RadGridView is available here: http://www.telerik.com/help/winforms/gridview-filtering-setting-filters-programmatically-simple-descriptors.html.

    I hope this helps.
     
    Kind regards,
    Stefan
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 24 Mar in reply to Stefan Link to this post

    Hello,

      I would like some help in filtering.  I am doing the following but can't get the filtering to work;

    private void searchimput_TextChanged(object sender, EventArgs e)
            {
                FilterDescriptor filterX = radGridView1.FilterDescriptors.FirstOrDefault(f => f.PropertyName == "FieldX");
                if (filterX == null) //Create it.
                {              
                    if ( !String.IsNullOrEmpty(searchimput.Text.Trim()))
                    {
                        filterX = new FilterDescriptor("FieldX", FilterOperator.Contains, searchimput.Text.Trim());
                        radGridView1.FilterDescriptors.Add(filterX );
                    }
                }
                else //Update the value.
                {
                    if (!String.IsNullOrEmpty(searchimput.Text.Trim()))
                    {
                        patref.Value = searchimput.Text.Trim();
                    }
                    else
                    {
                        patref.Value = null;
                    }
                }
            }

    I have enabled the filtering;

      grid.EnableFiltering = true;
      grid.MasterTemplate.EnableFiltering = true;  

    I have verified that the filtering does work on the grid using the following;

    CompositeFilterDescriptor compositeFilter = new CompositeFilterDescriptor();
                        foreach (var b in lstcheckedbutton)
                        {
                            compositeFilter.FilterDescriptors.Add(new FilterDescriptor("Status", FilterOperator.IsEqualTo, ((SomeStatus)b.Tag).ToString()));
     
                        }
                        compositeFilter.LogicalOperator = FilterLogicalOperator.Or;
                        this.radGridView1.Columns["Status"].FilterDescriptor = compositeFilter;

    But now I need to also filter using a textbox.

    Thanks

    - Francisco

  5. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 24 Mar in reply to Francisco Link to this post

    Correction for variable name misspell (above)...

    private void searchimput_TextChanged(object sender, EventArgs e)
            {
                FilterDescriptor filterX = radGridView1.FilterDescriptors.FirstOrDefault(f => f.PropertyName == "FieldX");
                if (filterX == null) //Create it.
                {             
                    if ( !String.IsNullOrEmpty(searchimput.Text.Trim()))
                    {
                        filterX = new FilterDescriptor("FieldX", FilterOperator.Contains, searchimput.Text.Trim());
                        radGridView1.FilterDescriptors.Add(filterX );
                    }
                }
                else //Update the value.
                {
                    if (!String.IsNullOrEmpty(searchimput.Text.Trim()))
                    {
                        filterX.Value = searchimput.Text.Trim();
                    }
                    else
                    {
                        filterX.Value = null;
                    }
                }
            }

     

  6. Francisco
    Francisco avatar
    17 posts
    Member since:
    Mar 2016

    Posted 24 Mar in reply to Francisco Link to this post

    I figured out that my issue was that I was not using the correct names.  When setting the propertyName for the filterDescriptor one has to use the "unique Name" of the column and not the binding field.

    The solution is working now.

     

  7. Dess
    Admin
    Dess avatar
    1605 posts

    Posted 25 Mar Link to this post

    Hello Francisco,

    Thank you for writing.

    I am glad that the problem you were facing is now resolved. You can refer to our Demo application >> GridView >> Custom Filtering example which demonstrates a sample approach how to use a text box to filter the grid. The Demo application is usually located in the following path: C:\Program Files (x86)\Telerik\UI for WinForms Q1 2016\Examples\QuickStart\Bin.

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Back to Top
UI for WinForms is Visual Studio 2017 Ready