VB.net DataGridview 筛选 2013
VB.net DataGridview Filtering 2013
我创建了一个 Datagridview
来显示所有数据。我现在希望能够过滤我的数据。我正在使用 DataSet
、BindingSource
和 TableAdapter
。我尝试了几件事,但似乎没有任何效果。目前我有一个 TextBox
应该在写入时过滤。当我执行并在框中键入时,它不会过滤或出错。下面是我的代码。我错过了什么吗?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToAddRows = True
DataGridView1.AllowUserToDeleteRows = True
Dim cn As SqlConnection = New SqlConnection("")
adap = New SqlDataAdapter("SELECT res_snbr, First_Name, Last_Name, Item FROM Inventory_Details", cn)
Dim builder As New SqlCommandBuilder(adap)
adap.InsertCommand = builder.GetInsertCommand()
'adap.UpdateCommand = builder.GetUpdateCommand()
'adap.DeleteCommand = builder.GetDeleteCommand()
dt = New DataTable()
adap.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength > 0 Then
InventoryDetailsBindingSource.Filter = _
String.Format("res_snbr Like '%" & TextBox1.Text) & "%'"
Else
InventoryDetailsBindingSource.Filter = String.Empty
End If
End Sub
如果您的数据源是数据表,您可以试试这个。
TryCast(InventoryDetailsBindingSource, DataTable).DefaultView.RowFilter = String.Format("Field = '{0}'", textBoxFilter.Text)
您可以在数据表上使用行过滤器 属性,如此处所示。
http://www.csharp-examples.net/dataview-rowfilter/
Imports System.Data.SqlClient
Public Class Form1
Dim cn As New SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=")
Dim adap As New SqlDataAdapter("SELECT res_snbr, First_Name, Last_Name, Item FROM Inventory_Details", cn)
Dim builder As New SqlCommandBuilder(adap)
Dim dt As New DataTable
Dim InventoryDetailsBindingSource As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
adap.InsertCommand = builder.GetInsertCommand()
adap.Fill(dt)
InventoryDetailsBindingSource.DataSource = dt
DataGridView1.DataSource = InventoryDetailsBindingSource
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength > 0 Then
InventoryDetailsBindingSource.Filter = String.Format("res_snbr Like '%{0}%'", TextBox1.Text)
Else
InventoryDetailsBindingSource.Filter = String.Empty
End If
End Sub
End Class
这里有很多方法可以完成你想做的事情,上面的代码就是我的方法。除其他差异外,请注意 BindingSource
被用作 DataGridView
的 DataSource
,并且在设置 Filter
时正确使用 String.Format
BindingSource
.
的 属性
干杯!
我创建了一个 Datagridview
来显示所有数据。我现在希望能够过滤我的数据。我正在使用 DataSet
、BindingSource
和 TableAdapter
。我尝试了几件事,但似乎没有任何效果。目前我有一个 TextBox
应该在写入时过滤。当我执行并在框中键入时,它不会过滤或出错。下面是我的代码。我错过了什么吗?
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToAddRows = True
DataGridView1.AllowUserToDeleteRows = True
Dim cn As SqlConnection = New SqlConnection("")
adap = New SqlDataAdapter("SELECT res_snbr, First_Name, Last_Name, Item FROM Inventory_Details", cn)
Dim builder As New SqlCommandBuilder(adap)
adap.InsertCommand = builder.GetInsertCommand()
'adap.UpdateCommand = builder.GetUpdateCommand()
'adap.DeleteCommand = builder.GetDeleteCommand()
dt = New DataTable()
adap.Fill(dt)
DataGridView1.DataSource = dt
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength > 0 Then
InventoryDetailsBindingSource.Filter = _
String.Format("res_snbr Like '%" & TextBox1.Text) & "%'"
Else
InventoryDetailsBindingSource.Filter = String.Empty
End If
End Sub
如果您的数据源是数据表,您可以试试这个。
TryCast(InventoryDetailsBindingSource, DataTable).DefaultView.RowFilter = String.Format("Field = '{0}'", textBoxFilter.Text)
您可以在数据表上使用行过滤器 属性,如此处所示。 http://www.csharp-examples.net/dataview-rowfilter/
Imports System.Data.SqlClient
Public Class Form1
Dim cn As New SqlConnection("Data Source=;Initial Catalog=;Persist Security Info=True;User ID=;Password=")
Dim adap As New SqlDataAdapter("SELECT res_snbr, First_Name, Last_Name, Item FROM Inventory_Details", cn)
Dim builder As New SqlCommandBuilder(adap)
Dim dt As New DataTable
Dim InventoryDetailsBindingSource As New BindingSource
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
adap.InsertCommand = builder.GetInsertCommand()
adap.Fill(dt)
InventoryDetailsBindingSource.DataSource = dt
DataGridView1.DataSource = InventoryDetailsBindingSource
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.TextLength > 0 Then
InventoryDetailsBindingSource.Filter = String.Format("res_snbr Like '%{0}%'", TextBox1.Text)
Else
InventoryDetailsBindingSource.Filter = String.Empty
End If
End Sub
End Class
这里有很多方法可以完成你想做的事情,上面的代码就是我的方法。除其他差异外,请注意 BindingSource
被用作 DataGridView
的 DataSource
,并且在设置 Filter
时正确使用 String.Format
BindingSource
.
干杯!