📄 form1.vb
字号:
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form 设计工具产生的程序代码 "
Public Sub New()
MyBase.New()
'此呼叫为 Windows Form 设计工具的必要项。
InitializeComponent()
'在 InitializeComponent() 呼叫之后加入所有的初始设定
End Sub
'Form 覆写 Dispose 以清除组件清单。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'为 Windows Form 设计工具的必要项
Private components As System.ComponentModel.IContainer
'注意: 以下为 Windows Form 设计工具所需的程序
'您可以使用 Windows Form 设计工具进行修改。
'请勿使用程序代码编辑器来修改这些程序。
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Button2 = New System.Windows.Forms.Button
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Label2 = New System.Windows.Forms.Label
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(20, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "载入"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(236, 48)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(102, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = "a"
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(20, 56)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(159, 17)
Me.Label1.TabIndex = 2
Me.Label1.Text = "过滤ProductName字首的产品"
'
'Button2
'
Me.Button2.Enabled = False
Me.Button2.Location = New System.Drawing.Point(369, 48)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(96, 24)
Me.Button2.TabIndex = 3
Me.Button2.Text = "过滤"
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(20, 80)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(451, 208)
Me.DataGrid1.TabIndex = 4
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(133, 24)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(140, 17)
Me.Label2.TabIndex = 5
Me.Label2.Text = "按DataGrid的列可以排序"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(491, 302)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.DataGrid1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "DataGrid Filter"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BindDataGrid()
'按 DataGrid 的字段可做排序
Button2.Enabled = True
End Sub
Private ds As DataSet
Private Sub BindDataGrid()
'SQL Server
'Dim conn As New SqlConnection("Server=localhost;DataBase=northwind;Integrated Security=SSPI")
'MSDE
Dim conn As New SqlConnection("Server=(local);DataBase=northwind;Integrated Security=SSPI")
Dim da As SqlDataAdapter
Try
'da = New SqlDataAdapter("SELECT * FROM Products", conn)
Dim cmd1 As New SqlCommand("SELECT * FROM Products", conn)
da = New SqlDataAdapter(cmd1)
ds = New DataSet()
da.Fill(ds, "Products")
DataGrid1.DataSource = ds.Tables(0)
Catch e1 As Exception
MsgBox("无法读取. " & e1.ToString)
Finally
If (conn.State = ConnectionState.Open) Then
conn.Close()
End If
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'过滤 ProductName 前缀的产品
ds.Tables("Products").DefaultView.RowFilter = "ProductName like '" & TextBox1.Text & "%'"
'未找到
If ds.Tables("Products").DefaultView.Count = 0 Then
MessageBox.Show("未找到", "过滤", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
DataGrid1.DataSource = ds.Tables("Products").DefaultView
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -