Private Shadows Sub OnKeyUp(sender As Object, e As KeyEventArgs) Handles TxtBulkShortDesc.KeyUp
Dim tb = DirectCast(sender, TextBox)
If tb.Equals(TxtBulkShortDesc) Then
If e.KeyCode.Equals(Keys.Enter) Then
DataGridViewStringsBulk.SearchAndSelect(tb.Text, CBBulkSearchDesc.SelectedItem, True)
tb.Clear()
e.Handled = True
End If
End If
End Sub
Public Function ConvertToDataTable(Of T)(ByVal source As IEnumerable(Of T)) As DataTable
Dim properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(T))
Dim table As DataTable = New DataTable()
For i As Integer = 0 To properties.Count - 1
Dim [property] As PropertyDescriptor = properties(i)
If [property].PropertyType.IsGenericType AndAlso [property].PropertyType.GetGenericTypeDefinition().Equals(GetType(Nullable)) Then
table.Columns.Add([property].Name, [property].PropertyType.GetGenericArguments()(0))
Else
table.Columns.Add([property].Name, [property].PropertyType)
End If
Next
Dim values(properties.Count - 1) As Object
For Each item As T In source
For i As Integer = 0 To properties.Count - 1
values(i) = properties(i).GetValue(item)
Next
table.Rows.Add(values)
Next
Return table
End Function
<Extension()>
Public Sub SearchAndSelect(grid As DataGridView, term As String, selector As String, Optional clearSelected As Boolean = False)
If grid IsNot Nothing AndAlso grid.Rows.Count < 1 Then Return
If String.IsNullOrWhiteSpace(term) Then Return
grid.BeginEdit(True)
Dim counter = 0
If clearSelected Then
For Each row In grid.SelectedRows.Cast(Of DataGridViewRow)()
row.Selected = False
Next
End If
For Each row In grid.Rows.Cast(Of DataGridViewRow)()
If selector <> "- All Columns -" Then
If row.Cells(selector).Value IsNot Nothing AndAlso row.Cells(selector).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Continue For
End If
Else
For Each column In grid.Columns.Cast(Of DataGridViewColumn)()
If row.Cells(column.Name).Value IsNot Nothing AndAlso row.Cells(column.Name).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Exit For
End If
Next
End If
Next
grid.EndEdit()
If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
End Sub
If (DataGridViewStrCommon.SelectedRows.Count > 0) Then
DataGridViewStrCommon.FirstDisplayedScrollingRowIndex = DataGridViewStrCommon.Rows.IndexOf(DataGridViewStrCommon.SelectedRows(0))
End If
<Extension()>
Public Sub SearchAndSelect(grid As DataGridView, term As String, selector As String, Optional clearSelected As Boolean = False)
If grid IsNot Nothing AndAlso grid.Rows.Count < 1 Then Return
If String.IsNullOrWhiteSpace(term) Then Return
grid.BeginEdit(True)
Dim counter = 0
If clearSelected Then
For Each row In grid.SelectedRows.Cast(Of DataGridViewRow)()
row.Selected = False
Next
End If
For Each row In grid.Rows.Cast(Of DataGridViewRow)()
If selector <> "- All Columns -" Then
If row.Cells(selector).Value IsNot Nothing AndAlso row.Cells(selector).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Continue For
End If
Else
For Each column In grid.Columns.Cast(Of DataGridViewColumn)()
If row.Cells(column.Name).Value IsNot Nothing AndAlso row.Cells(column.Name).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Exit For
End If
Next
End If
Next
grid.EndEdit()
If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
If grid.SelectedRows.Count > 0 Then
grid.FirstDisplayedScrollingRowIndex = grid.SelectedRows.Cast(Of DataGridViewRow).Last().Index
End If
End Sub
<Extension()>
Public Sub SearchAndSelect(grid As DataGridView, term As String, selector As String, Optional clearSelected As Boolean = False)
If grid IsNot Nothing AndAlso grid.Rows.Count < 1 Then Return
If String.IsNullOrWhiteSpace(term) Then Return
grid.BeginEdit(True)
Dim counter = 0
If clearSelected Then
For Each row In grid.SelectedRows.Cast(Of DataGridViewRow)()
row.Selected = False
Next
End If
For Each row In grid.Rows.Cast(Of DataGridViewRow)()
If selector <> "- All Columns -" Then
If row.Cells(selector).Value IsNot Nothing AndAlso row.Cells(selector).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Continue For
End If
Else
For Each column In grid.Columns.Cast(Of DataGridViewColumn)()
If row.Cells(column.Name).Value IsNot Nothing AndAlso row.Cells(column.Name).Value.ToString().IndexOf(term, StringComparison.OrdinalIgnoreCase) <> -1 Then
row.Selected = True
counter += 1
Exit For
End If
Next
End If
Next
grid.EndEdit()
If counter = 0 Then MessageBox.Show(String.Format("No rows were found that matched - {0}", term))
If grid.SelectedRows.Count > 0 Then
grid.FirstDisplayedScrollingRowIndex = grid.SelectedRows.Cast(Of DataGridViewRow).Reverse()(0).Index
End If
End Sub
-saige-
The DataGridView has a property called FirstDisplayedScrollingRow
Open in new window