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

Searching on hierarchical Gridview

6 Answers 227 Views
GridView
This is a migrated thread and some comments may be shown as answers.
muralidhar
Top achievements
Rank 1
muralidhar asked on 10 Jan 2012, 08:41 AM
We are displaying data in Gridview control in hierarchical manner (like tree view). We also have requirement to search the data in grid to display related records (child and parent).  We have seed existing example and code but it is not matching above requirements.

 

Could you please provide more information on this?

6 Answers, 1 is accepted

Sort by
0
Julian Benkov
Telerik team
answered on 12 Jan 2012, 06:18 PM
Hi muralidhar,

Thank you for writing.

To search in RadGridView control in hierarchy mode you can use ChildRows collection. Here is a simple example:
private List<GridViewRowInfo> Find(string text)
{
    List<GridViewRowInfo> found = new List<GridViewRowInfo>();
    Stack<GridViewChildRowCollection> rowStack = new Stack<GridViewChildRowCollection>();
    rowStack.Push(this.radGridView1.ChildRows);
    while (rowStack.Count > 0)
    {
        GridViewChildRowCollection rows = rowStack.Pop();
        foreach (var row in rows)
        {
            foreach (var col in this.radGridView1.Columns)
            {
                if (row.Cells[col.Index].Value == text)
                {
                    found.Add(row);
                }
            }
 
            if (row.ChildRows.Count > 0)
            {
                rowStack.Push(row.ChildRows);
            }
        }
    }
 
    return found;
}

I hope this helps. Let me know if you need further assistance.

Greetings,
Julian Benkov
the Telerik team

SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

0
Transics
Top achievements
Rank 1
answered on 13 Feb 2012, 09:14 AM
Hi Julian,

This is only working on the first 2 levels of my hierarchy grid. I can have 4 levels in total on my grid.

So when I search for a text that is present on my 3rd level, the search returns nothing.

Below is the relation I've made for my grid:

this.radGridView.Relations.AddSelfReference(this.radGridView.MasterTemplate, "pLevel""parentId");

As my grid can have 4 levels I implemented as follows:

pLevel will contain the ID of the current level.
parentId will contain the ID of the parent.

Am I doing something wrong in my relation? Or do I need to add something extra to the "Find" function Julian?
0
Julian Benkov
Telerik team
answered on 15 Feb 2012, 06:28 PM
Hi Muralidhar,

The current Find method implementation depends on the root level Columns collection. You can extend it to support the inner hierarchy level for which another template with different columns schema is used. Here is a new implementation:

private List<GridViewRowInfo> Find(string text)
       {
           List<GridViewRowInfo> found = new List<GridViewRowInfo>();
           Stack<GridViewChildRowCollection> rowStack = new Stack<GridViewChildRowCollection>();
           rowStack.Push(this.gridView.ChildRows);
           while (rowStack.Count > 0)
           {
               GridViewChildRowCollection rows = rowStack.Pop();
               foreach (var row in rows)
               {
                   GridViewColumnCollection columns = row.ViewTemplate.Columns;
                   foreach (var col in columns)
                   {
                       if (row.Cells[col.Index].Value.ToString() == text)
                       {
                           found.Add(row);
                       }
                   }
 
                   if (row.ChildRows.Count > 0)
                   {
                       rowStack.Push(row.ChildRows);
                   }
               }
           }
 
           return found;
       }

The self-reference hierarchy mode does not depend on this change and the previous implementation should work without any issues. Please verify your data for level availability and search criteria. 

I hope this helps.

Regards,
Julian Benkov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
muralidhar
Top achievements
Rank 1
answered on 23 Feb 2012, 12:04 PM

Hi,

This is working fine only for the first row of the hierarchy, if we provide the text that is matching
the data in the rows from the second level then it is not retuning all the rows of the hierarchy.

Please provide the solution which can return the rows(complete hierarchy) if the text matches with any cell with in the hierarchy.

Thanks
Muralidhar

 

0
Julian Benkov
Telerik team
answered on 27 Feb 2012, 01:25 PM
Hi Muralidhar,

The provided solution will return all rows from any level that contain the searched text, but will not include the parent row for every found row in the loop. If you want to get the parent row of a found row, you can use Parent property of GridViewRowInfo. If this does not help, please give us a more details about your scenario and what you want to achieve in the UI using this Find method. You can also send us a sample application to investigate it locally and find best solution for you. Please note that you have to open a new support ticket in order to be able to attach your project.

I am looking forward to your reply. 

Greetings,
Julian Benkov
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Accepted
Eric
Top achievements
Rank 1
answered on 05 Mar 2012, 09:55 PM
Just for reference, if you need to search ONLY within the current hierarchy as we do you can use the following code:

Private Sub FindRow(match As String)
 
          If gridView.CurrentRow Is Nothing Then Return
 
          Dim iCol As Integer
          Dim iStartRow As Integer
          If gridView.CurrentCell IsNot Nothing Then
              iCol = gridView.CurrentCell.ColumnIndex
              iStartRow = gridView.CurrentCell.RowIndex + 1
          End If
 
          Dim rows As GridViewChildRowCollection
          If gridView.CurrentRow.Parent IsNot Nothing Then
              'somewhat circular reference in order to find the appropriate row collection of siblings of the selected row
              rows = gridView.CurrentRow.Parent.ChildRows
          Else
              rows = gridView.ChildRows
          End If
 
          'For Each row As GridViewRowInfo In rows
          Dim row As GridViewRowInfo
          For iRow As Integer = iStartRow To rows.Count - 1
              row = rows(iRow)
              If row.Cells(iCol).Value IsNot Nothing _
              AndAlso row.Cells(iCol).Value.ToString().StartsWith(match) Then
 
                  gridView.CurrentRow = row
                  Return
 
              End If
          Next
 
      End Sub


So this will only search within the context of the current table level... basically the row's siblings only... and in my case will move the user's selection to that row.
Tags
GridView
Asked by
muralidhar
Top achievements
Rank 1
Answers by
Julian Benkov
Telerik team
Transics
Top achievements
Rank 1
muralidhar
Top achievements
Rank 1
Eric
Top achievements
Rank 1
Share this question
or