Home - Forums-.NET - FlyGrid.Net (Windows Forms) - Nested grid search

FlyGrid.Net (Windows Forms)

.NET Datagrid - Fast, highly customizable, industry standards .NET data grid control for WinForms

This forum related to following products: FlyGrid.Net

Nested grid search
Link Posted: 05-Mar-2006 08:20
When I didn't have nested grids (no hierarchy columns) i used
this function to look through the nods in my grid and focus the node when
it found one.
Private Function Find()
Dim items As NodeCollection = gridKorisnici.Rows.Items
        Dim item As NodeBase
        For Each item In items            
            If item.Item(1).ToString.ToLower.IndexOf(txtFind.Text.ToString.ToLower)  -1 Then
                gridKorisnici.Focus()
                gridKorisnici.Selected = item
                Exit Function
            End If
        Next
End Function
Now when i have nested grids, this function hooks up the application (it doesen't respond, giving no errors).

Any feedback appriciated.
Link Posted: 07-Mar-2006 16:39
I need more details - that type of nested grids you're using: data bound, virtual or using real mode?
In the Virtual and databound modes you'll need to expand node before searching in the nested grid, as in these modes FlyGrid doesn't stores all nodes in the nested grids, only in expanded nodes.
Also:
Use following code to be notified about reasons of errors:

Private Function Find()
  Dim items As NodeCollection = gridKorisnici.Rows.Items
  Dim item As NodeBase
  Try
    For Each item In items
      If item.Item(1).ToString.ToLower.IndexOf(txtFind.Text.ToString.ToLower)  -1 Then
        gridKorisnici.Focus()
        gridKorisnici.Selected = item
        Exit Function
      End If
    Next
  Catch e as Exception
    System.Diagnostics.Debug.WriteLine("Exception: " + e.Message)
    System.Diagnostics.Debug.WriteLine("Stack Trace: " + e.StackTrace)
  End Try
End Function


Code in Catch clause helps to more clearly understand problem.
Link Posted: 08-Mar-2006 05:18
I use real mode

Dim data As Object()
            data = New Object() {objKorisnik.Ime, objKorisnik.Prezime, objKorisnik.Odjel, objKorisnik.Ured, objKorisnik.Limit, objKorisnik.Point, objKorisnik.Mobitel, objKorisnik.ID}
            Dim n As NodeBase = AddNestedTo(gridKorisnici.Rows.Items, data)
            AddItemsTo(n.Items, objKorisnik.ID)

Private Sub AddItemsTo(ByVal nodes As NodeCollection, ByVal id As Long)
        Dim veza As New Veza
        Dim objVeze As VezaCollection = veza.GetInfo(id)
        Dim objVeza As Veza
        For Each objVeza In objVeze
            If objVeza.Broj  0 Then
                Dim data As Object()
                data = New Object() {Format(CInt(objVeza.Broj), "0000000000"), Format(objVeza.Aktivirano, "dd.MM.yyyy"), IIf(objVeza.Suspendirano.Year = 1, String.Empty, Format(objVeza.Suspendirano, "dd.MM.yyyy")), objVeza.VezaID}
                nodes.Add(New Node(Data))
            End If
            If Not objVeze Is Nothing Then objVeze = Nothing
        Next
    End Sub

Private Function AddNestedTo(ByVal nodes As NodeCollection, ByVal obj As Object) As NodeBase
        Dim nested As NodeBase = New NestedGridNode
        nested.Value = obj
        nodes.Add(nested)
        Return nested
    End Function

but i use the Find function ti search only items in column(1).
In fact i just noticed that the Find function is not causing the problem but the application hangs when i press F to put focus in my Textbox (in gridKorisnici_KeyDown Handler). It does not produce errors, it just suddenly takes all the CPU usage and i can't do anything but force close it. Further more when i DON'T use nested grids, all this works wihtout any problem.

Private Sub gridKorisnici_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gridKorisnici.KeyDown
        If e.KeyCode = Keys.F5 Then LoadKorisnici()
        If e.KeyCode = Keys.Enter Then IzmjeniKorisnika()
        If e.KeyCode = Keys.Delete Then btnObrisi_Click(Nothing, Nothing)
        If e.Control = True And e.KeyCode = Keys.A Then gridKorisnici.Rows.SelectAll()
              If e.KeyCode = Keys.F Then
            txtFind.Focus()
            txtFind.SelectAll()          
        End If
    End Sub
Link Posted: 08-Mar-2006 18:50
Please download latest FlyGrid.Net and use following code for text/data search in the nested grids:
[VB.Net]
    Public Class SearchData
      Public FoundCol As Integer
      Public FoundNode As NodeBase
      Public ReadOnly SearchText As String
      Public Sub New(ByVal searchString As String)
        Me.SearchText = searchString
      End Sub
    End Class

    Private Function Find(ByVal nodes As NodeCollection, ByVal data As SearchData) As Boolean
      If (Not nodes Is Nothing) Then
        For Each child As NodeBase In nodes
          Dim val As Object() = child.Value
          If (Not val Is Nothing) Then
            For i As Integer = 0 To val.Length - 1
              Dim cellValue As String = val.GetValue(i)
              If (Not cellValue Is Nothing And cellValue.Equals(data.SearchText)) Then
                data.FoundNode = child
                data.FoundCol = i
                Return True
              End If
            Next
          End If
          If (child.HasChildren) Then
            If Find(child.Items, data) Then
              Return True
            End If
          End If
        Next
      End If
      Return False
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      Dim data As SearchData = New SearchData("Some text for search")
      If (Find(nestedGrid.Rows.Items, data)) Then
        nestedGrid.Selected = data.FoundNode
      End If
    End Sub