-
Datagrid and Dataset
I have a form with a Datagrid, Dataset and a text box
The dataset contains 1 table with peoples names (2000 of them) and I have bound the dataset to the datagrid so the datagarid displays all of the names from the dataset using the "Datasource = dsclients.Tables("clients").defaultview" command.
I have formatted the columns of the datagrid to look nice etc etc. This is all fine.
What I am trying to do is that as soon as the user starts typing in the text box (using the click event) I want the datgrid to scroll down to the first line that matches the text in the text box. For example if start typing in "St" then I want the datagrid to scroll down and move the pointer the first line in the datagrid surname columns that starts with "St".
Please help?
-
Not sure if it's easier to intercept keystrokes with a KeyPress event handler for the DataGrid, or to just use ListView instead (I prefer ListView for non-edit-mode tabular display). If you don't need to allow the user to edit each field of the data then I suggest you check out the ListView control as an alternative - listview has the kind of functionality you're looking for built into it. Here's an example from a program I'm working on:
Code:
Private Sub DoItemListView()
Dim lvwColumn As ColumnHeader
Dim itmListItem As ListViewItem
Dim shtCntr As Short
Dim objRow As DataRow
lvwItems.Items.Clear()
lvwItems.BeginUpdate()
'Populate the listview items
Dim objGetItems() As DataRow
If mnuViewAll.Checked = True Then
If lvwCategories.SelectedItems.Count > 0 Then
'Check to see if apostrophe (') is present in string
If lvwCategories.SelectedItems(0).Text.IndexOf("'") > -1 Then
Dim strTemp As String
strTemp = Replace(lvwCategories.SelectedItems(0).Text, "'", "''")
For Each objRow In dsGurpsData.Tables("Skill").Select("Category = '" + _
strTemp + "'")
itmListItem = New ListViewItem()
itmListItem.Text = objRow.Item("Name")
itmListItem.SubItems.Add(objRow.Item("Level"))
itmListItem.SubItems.Add(objRow.Item("PointsSpent"))
itmListItem.SubItems.Add(objRow.Item("Difficulty"))
'For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
' itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
'Next
lvwItems.Items.Add(itmListItem)
Next
Else
For Each objRow In dsGurpsData.Tables("Skill").Select("Category = '" + _
lvwCategories.SelectedItems.Item(0).Text + "'")
itmListItem = New ListViewItem()
itmListItem.Text = objRow.Item("Name")
itmListItem.SubItems.Add(objRow.Item("Level"))
itmListItem.SubItems.Add(objRow.Item("PointsSpent"))
itmListItem.SubItems.Add(objRow.Item("Difficulty"))
'For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
' itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
'Next
lvwItems.Items.Add(itmListItem)
Next
End If
End If
ElseIf mnuViewSelected.Checked = True Then
For Each objRow In dsGurpsData.Tables("Skill").Select("LevelsTaken > 0")
itmListItem = New ListViewItem()
itmListItem.Text = objRow.Item("Name")
itmListItem.SubItems.Add(objRow.Item("Level"))
itmListItem.SubItems.Add(objRow.Item("PointsSpent"))
itmListItem.SubItems.Add(objRow.Item("Difficulty"))
'For shtCntr = 1 To objRow.ItemArray.GetUpperBound(0)
' itmListItem.SubItems.Add(objRow.ItemArray.GetValue(shtCntr))
'Next
lvwItems.Items.Add(itmListItem)
Next
End If
lvwItems.EndUpdate()
End Sub
You can also use a different loop structure to just populate the listview with listviewitem.subitems for every field in your records. Good luck.
-
DataSet and DataGrid
Thanks Slow-learner, your contribution was very helpful.
But is there a way to do it with the datagrid. VB6 did it quite easily???
-
I'm not very swift at programming in general and VB in particular, but there doesn't seem to be a very quick way to trick DataGrid to do that handy search-as-you-type thing. Maybe someone else has more info :)
-
Datagrid and Datset
Thanks Slow-learner. If anyone else could help I would appreciate the assistance.