制作使用虚拟模式DGV的应用。我无法访问DataGridView的属性RowCount,IDE不会将其显示为有效属性。 MSDN说它是。使用Visual Studio 2017和.NET 4.5.2
这是我的代码。我只是按原样张贴,因为我需要每一行。
Imports System.Text.RegularExpressions
Imports HelperLib.SQLiteModule
Imports System.Data.SQLite
Public Class ProxyTesterView
Private connString As String = "Data Source=" & My.Application.Info.DirectoryPath & "/data.db3;" & "Version=3;"
Public Property ProxyDT As DataTable
Private Sub ProxyTesterView_Load(sender As Object, e As EventArgs) Handles Me.Load
'Only load proxies if we are not in Design mode!
If DesignMode = False Then
Try
LoadProxiesFromDB()
Catch ex As SQLite.SQLiteException
If ex.ErrorCode = 1 Then
'DB Doesn't exist, create it!
CreateNewDB()
End If
End Try
End If
End Sub
Private Sub CreateNewDB()
Dim sql As String = "CREATE TABLE proxies(ip VARCHAR(15), port VARCHAR(5), valid BOOLEAN, speed VARCHAR(5), countryName VARCHAR(36), PRIMARY KEY (ip, port))"
CreateDB(My.Application.Info.DirectoryPath & "/data.db3", sql)
End Sub
Private Sub LoadProxiesFromDB()
Dim sql As String = "SELECT ip, port, valid, speed, countryName FROM proxies"
ProxyDT = GetDataTableFromDB(connString, sql)
End Sub
Private Sub AddProxyToDB(proxy As Proxy)
Using cmd As New SQLiteCommand("INSERT INTO proxies (ip, port, valid, speed, countryName) VALUES (@ip, @port, @valid, @speed, @countryName)")
cmd.Parameters.AddWithValue("@ip", proxy.IP)
cmd.Parameters.AddWithValue("@port", proxy.Port)
cmd.Parameters.AddWithValue("@valid", proxy.IsValid)
cmd.Parameters.AddWithValue("@speed", proxy.Speed)
cmd.Parameters.AddWithValue("@countryName", proxy.Country)
ExecuteNonQuery(connString, cmd)
End Using
End Sub
Private Sub ProxyTesterView_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.V Then
For Each proxy As Proxy In GetProxiesFromClipboard()
ProxyDT.Rows.Add(proxy)
Next
End If
End Sub
Public Function GetProxiesFromClipboard() As List(Of Proxy)
Dim proxies As New List(Of Proxy)
For Each line As String In Clipboard.GetText().Split(vbCrLf)
'Regular Expression for proxy pattern found here: https://superuser.com/questions/582120/regex-pattern-for-proxy
If Regex.IsMatch(line, "((?:\d{1,3}\.){3}\d{1,3}):(\d+)") Then
proxies.Add(New Proxy(line))
End If
Next
Return proxies
End Function
Private Sub dgvProxies_CellValueNeeded(sender As Object, e As DataGridViewCellValueEventArgs) Handles dgvProxies.CellValueNeeded
Dim row As DataRow = ProxyDT.Rows(e.RowIndex)
Select Case dgvProxies.Columns(e.ColumnIndex).Name
Case "ip"
e.Value = row.Item("ip")
End Select
End Sub
End Class