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

Search Data in DataGridView using Textbox

5 Answers 2603 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Hiralkumar
Top achievements
Rank 1
Hiralkumar asked on 24 Nov 2012, 05:34 AM
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

5 Answers, 1 is accepted

Sort by
0
Accepted
Stefan
Telerik team
answered on 28 Nov 2012, 02:54 PM
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.
0
Francisco
Top achievements
Rank 1
answered on 24 Mar 2016, 06:46 PM

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

0
Francisco
Top achievements
Rank 1
answered on 24 Mar 2016, 06:48 PM

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;
                }
            }
        }

 

0
Francisco
Top achievements
Rank 1
answered on 24 Mar 2016, 06:59 PM

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.

 

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 25 Mar 2016, 12:06 PM
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 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.
Tags
GridView
Asked by
Hiralkumar
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Francisco
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or