我已经能够在Form上填充DataGridView的内容。
我已将BindingSource
和BindingNavigator
添加到表单中。 Bindingsource
控件具有一个我可以从中选择文本框的元素。
我希望是在运行时,我可以在BindingSource
中键入该文本框,它将搜索DataGridView的内容并将其突出显示。
下面是我尝试过并在下面出错的代码
Dim dt As DataTable
dt = DataGridView1.DataSource
dt.DefaultView.RowFilter = String.Format("Field = '{0}'", txtdatagrid.Text)
我收到此错误:
无法将类型为“ System.Windows.Forms.BindingSource”的对象转换为 键入“ System.Data.DataTable”
答案 0 :(得分:0)
我有同样的需要。下面是我修改和使用的DatagridView类。 无论数据源是什么,我都可以搜索所有字符串值单元格。 在我的表单中,我不得不将Datagridview的声明从System.Windows.Forms.DataGridView更改为'mDataGridView'
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Windows
Imports System.Windows.Forms
Imports System.ComponentModel.Design
Partial Public Class mDataGridView
Inherits System.Windows.Forms.DataGridView
Public Sub New()
End Sub
Private Sub mDataGridView_Load(sender As Object, e As EventArgs)
Me.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells ' Adjust as you need
Sort(Me.Columns(0), ListSortDirection.Ascending) ' Adjust as you need
End Sub
'''<summary>
''' Searches text in datagridview cells
''' </summary>
''' <param name="txt"></param>
''' <param name="direction"></param>
''' <param name="ColIndex"></param>
Public Sub SearchText(txt As String, direction As Direction)
'Keep track of selected line
Static Position As Integer = CurrentRow.Index
'Prevents searching when there is no row
If Me.Rows.Count < 1 Then
Return
End If
'keeps index of rows of matched cells
Select Case direction
Case direction.DOWN
Dim start As Integer = Position + 1 ' Next Row DOWN
For i As Integer = start To Me.Rows.Count - 1
Dim mRow As DataGridViewRow = Me.Rows(i)
For ColIndex As Integer = 0 To Me.Columns.Count - 1
While Not mRow.Cells(ColIndex).Value Is Nothing
If mRow.Cells(ColIndex).Value.ToString().ToUpper().Contains(txt.ToUpper()) Then
Me.SelectionMode = ataGridViewSelectionMode.FullRowSelect
'exclude non-string value cell (Checkbox,Image)
Dim val As Boolean = mRow.Cells(ColIndex).ValueType.Name = "Boolean"
val = mRow.Cells(ColIndex).ValueType.Name = "Image"
If val = False Then
mRow.Selected = True
Me.CurrentCell = mRow.Cells(ColIndex)
Position = Me.CurrentCell.RowIndex
Return
End If
End If
Exit While
End While
Next ColIndex
Position += 1
Next i
'no matched record found
System.Windows.Forms.MessageBox.Show("No more matching record found", "")
'///reset the position where search
' ///will start
'///so that next search will start from 0.
Position = -1
Case direction.UP
Dim start As Integer = Position - 1 ' Next Row UP
For i As Integer = start To 0 Step -1
If i < 0 Then Exit For
Dim mRow As DataGridViewRow = Me.Rows(i)
For ColIndex As Integer = 0 To Me.Columns.Count - 1
While Not mRow.Cells(ColIndex).Value Is Nothing
If mRow.Cells(ColIndex).Value.ToString().ToUpper().Contains(txt.ToUpper()) Then
Me.SelectionMode = DataGridViewSelectionMode.FullRowSelect
'exclude non-string value cell (Checkbox,Image)
Dim val As Boolean = mRow.Cells(ColIndex).ValueType.Name = "Boolean"
val = mRow.Cells(ColIndex).ValueType.Name = "Image"
If val = False Then
mRow.Selected = True
Me.CurrentCell = mRow.Cells(ColIndex)
Position = Me.CurrentCell.RowIndex
Return
End If
End If
Exit While
End While
Next cols
Next i
'Alert user ....
System.Windows.Forms.MessageBox.Show("No more matching record found", "")
'///reset the position where search
' ///will start
'///so that next search will start from Last Row.
Position = Me.RowCount - 1
End Select
End Sub
End Class
'''<summary>
'''Holds value for direction to navigate
''' rows.
'''</summary>
Public Enum Direction
DOWN
UP
End Enum