Results 1 to 6 of 6

Thread: search a value from datagrid w/ the use of textbox value.

  1. #1

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    search a value from datagrid w/ the use of textbox value.

    hi to everyone,
    i have this problem.how can i search to the datagrid w/ the use of textbox value.
    lets assumed that the datagrid were bound a data.

    f.e. when the user input a name in the textbox and press the enterkey it will get focus to the datagrid on what value is in the textbox.

    a sort of comboboxautocomplete but not as good as that.

    hope i explain it will
    thanks in advance guys.
    and more power.

    sorry im english is weak.

  2. #2
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263
    Hi

    Is that not just a simple text search on the datagrid? This would then show the row selected (ie have focus)

    Danny

  3. #3

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    yes. i want to search to the datagrid on what value is in the textbox.
    f.e. the value of textbox = Gangster.
    so..in the datagrid it will get focus to the Gangster value.

    i cant figure it out.
    pls.help its very important to me.

    thanks in advance and more power.

  4. #4
    Hyperactive Member
    Join Date
    Feb 2003
    Posts
    263
    Hi Mate

    I would assume you would do something similar to the following!

    Dim sStr As String
    Dim sTestStr As String
    Dim bfound as boolean

    sTestStr = textbox.text.ToUpper

    For i = 0 to datagrid.col
    for j = 0 to datagrid.row
    sStr = CType(datagrid.item(i,j), String).ToUpper
    if sStr = sTestStr then
    bfound = true
    datagrid.row = j
    exit for
    end if
    next
    if bfound then exit for
    next

    The syntax maybe a little off but you should be able to modify it!

    Cheers

    Danny

  5. #5

    Thread Starter
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416
    hi mate...thanks for the reply

    i tried your code but it wont work.
    VB Code:
    1. Dim s, ss As String
    2.         Dim f As Boolean
    3.         Dim i, j As Integer
    4.         ss = TextBox1.Text.ToUpper
    5.         For j = 0 To DataGrid1.CurrentCell.ColumnNumber
    6.             For i = 0 To DataGrid1.CurrentCell.RowNumber
    7.                 s = CType(DataGrid1.Item(i, j), String).ToUpper
    8.                 If s = ss Then
    9.                     f = True
    10.                     Exit For
    11.                 End If
    12.             Next
    13.             If f Then Exit For
    14.         Next

    what's wrong w/ it?

    thanks in advance mate.

  6. #6
    Member
    Join Date
    Sep 2002
    Location
    Cincinnati, OH
    Posts
    44
    Take this, put it in a class such as a utility class pass it the value of the text box as well as the other specified values. It will do dates or strings.

    Public Sub searchDatagrid(ByVal dg As DataGrid, ByVal rowCount As Integer, ByVal txt As String, ByVal col As Integer, Optional ByVal all As Boolean = False)
    'Searches for and selects an item in the datagrid based on the sent text.
    Dim r As Integer
    Dim s As Integer
    Dim found As Boolean
    Dim csc As GridColumnStylesCollection
    Dim gcs As DataGridColumnStyle
    Dim w As Integer

    csc = dg.TableStyles(0).GridColumnStyles

    Try
    dg.UnSelect(dg.CurrentRowIndex)

    If txt.Length = 0 Then
    dg.CurrentRowIndex = 0
    dg.Select(dg.CurrentRowIndex)
    Exit Sub
    End If

    If Not all Then
    w = csc(col).Width
    csc(col).Width = 1
    End If

    If UCase(txt) <= UCase(Mid(dg.Item(dg.CurrentRowIndex, col), 1, Len(txt))) _
    Or all = True Then
    s = 0
    Else
    's = cm.Position
    End If
    For r = s To rowCount - 1
    If all = True Then
    'This looks in all columns
    'csc = dg.TableStyles(0).GridColumnStyles
    col = 0
    For Each gcs In csc
    Try
    If dg.Item(r, col).GetType.ToString = "System.DateTime" Then
    found = Format(dg.Item(r, col), "MM/dd/yyyy") Like UCase(txt) & "*"
    Else
    found = UCase(dg.Item(r, col)) Like UCase(txt) & "*"
    End If
    If found Then
    dg.CurrentRowIndex = r
    Exit For
    End If
    col += 1
    Catch
    End Try
    Next
    If found Then Exit For
    Else
    Try
    If dg.Item(r, col).GetType.ToString = "System.DateTime" Then
    found = Format(dg.Item(r, col), "MM/dd/yyyy") Like UCase(txt) & "*"
    Else
    found = UCase(dg.Item(r, col)) Like UCase(txt) & "*"
    End If
    If found Then
    dg.CurrentRowIndex = r
    Exit For
    End If
    Catch ex As Exception
    MsgBox(ex.Message)
    End Try
    End If
    Next
    If Not all Then
    csc(col).Width = w
    End If
    dg.Select(dg.CurrentRowIndex)
    Catch ex As Exception
    End Try
    End Sub


    Any ?'s let me know. And yes, it works.
    Jim Webster

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width