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

Find in GridView

1 Answer 70 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Josip
Top achievements
Rank 1
Josip asked on 05 Dec 2011, 04:31 PM
Hi,

I was asked to implement feature to find values in grid (user wants to see all rows so i cant use filters)
user wants (picture)
(1) field to input search text
(2) button to find all rows (kind of bookmark)
(3) button to iterate in find items (find next)

is there built in Search in grid (without filters) ?
I made this feature with this code:
private void btnFind_Click(object sender, RoutedEventArgs e)         {             btnFind.IsEnabled = false;             scrollIntoFindIndex = 0;             string searchText = txtFind.Text;             dgDynGrid.SelectedItems.Clear();             foreach (object o in dgDynGrid.Items)             {                 if (o is DataRowView)                 {                     for (int z = 0; z < (o as DataRowView).Row.ItemArray.Count(); z++)                     {                         try                         {                             string t = (o as DataRowView).Row.ItemArray[z].ToString();                             if (t.IndexOf(searchText, StringComparison.InvariantCultureIgnoreCase) >= 0)                             {                                 dgDynGrid.SelectedItems.Add(o);                                 break;                             }                         }                         catch                         {                             //if it cant be converted to string, i will not find in this field                         }                     }                 }             }             btnFind.IsEnabled = true;             ScrollIntoFind(0);         }         int scrollIntoFindIndex = 0;         private void ScrollIntoFind(int i)         {             if (dgDynGrid.SelectedItems.Count > i)             {                 dgDynGrid.ScrollIntoView(dgDynGrid.SelectedItems[i]);             }             else             {                 scrollIntoFindIndex = 0;                 MessageBox.Show("Završila je pretraga, idučim klikom se pretražuje od početka""Kraj pretrage"MessageBoxButton.OK, MessageBoxImage.Information);             }         }         private void btnFindNext_Click(object sender, RoutedEventArgs e)         {             scrollIntoFindIndex++;             ScrollIntoFind(scrollIntoFindIndex);         }

but I am sure there must be better way to do this.
I would appreciate any hint

Thank you

1 Answer, 1 is accepted

Sort by
0
Nedyalko Nikolov
Telerik team
answered on 08 Dec 2011, 09:47 AM
Hello Josip,

The solution looks pretty good. You can use ScrollIndexIntoView() instead of ScrollIntoView(). For more information you can take a look at this help topic.

Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Josip
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Share this question
or