English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What I have is a datagrid on my form populated with various pieces of
information. What I would like is to be able to start typing and have
the datagrid move to the thing I'm typing if it exists.

For example given the list:

Ant
Cat
Dog
Dolphin
Puppy
Snake
Snail
Zebra

If I started typing "Do" then the list would move to Dog..and then if I
typed a "Dol" the list would move to Dolphin. It's the same way all
the file searching works in windows.

Any ideas how I would do this guys?

2007-03-12 22:59:01 · 2 answers · asked by aruntech 1 in Computers & Internet Programming & Design

2 answers

refer this field to a data source...
listen, check the MSDN, u can find everything there

2007-03-12 23:04:29 · answer #1 · answered by abd 5 · 0 0

Hello, i'm sure there many ways to skin a cat when it comes to programming.. one way of doing it.. would be.. first create a dataset because it supports Select statement then add datatable to it with all your names. after that set datasource to the table.. and monitor keypress in text box.. long story short.

Public Class Form1

Private DTS As New DataSet

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DT As New DataTable("tblData")
DT.Columns.Add("Name")
DT.Columns.Add("Index")

With DT.Rows
.Add("Ant", "0")
.Add("Cat", "1")
.Add("Dog", "2")
.Add("Dolphin", "3")
.Add("Puppy", "4")
.Add("Snake", "5")
.Add("Snail", "6")
.Add("Zebra", "7")
End With

DataGridView1.DataSource = DT
DataGridView1.ClearSelection()
DTS.Tables.Add(DT)
End Sub

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim DR() As DataRow
Dim Index As Integer

DR = DTS.Tables("tblData").Select("[Name] LIKE '" & TextBox1.Text & "*'")

If DR.Length > 0 And TextBox1.Text.Length > 0 Then
DataGridView1.ClearSelection()
Index = DR(0)("Index")
DataGridView1.Rows(Index).Selected = True
Else
DataGridView1.ClearSelection()
End If
End Sub
End Class

in this case, i assigned an index to each name, it will select only the first match, you can modify it to select all matches.

another one, simply add that to your textbox keyup event

Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
Dim iFound As Integer = -1
Dim myRow As DataGridViewRow

DataGridView1.AllowUserToAddRows = False
DataGridView1.ClearSelection()

For Each myRow In DataGridView1.Rows
iFound = myRow.Cells("Name").Value.ToString.IndexOf(TextBox1.Text)
If iFound >= 0 And TextBox1.Text.Length > 0 Then
myRow.Selected = True
Exit Sub
End If
Next

DataGridView1.ClearSelection()
DataGridView1.AllowUserToAddRows = False
End Sub

you can modify it so it's not case sensative.

2007-03-14 16:40:35 · answer #2 · answered by Zlavzilla 3 · 0 0

fedest.com, questions and answers