我是一个完整的新秀。我从这里学到了很多,但是这个我找不到答案。我正在使用Visual Studio Pro 2015。
我有一个Windows窗体应用程序,它有一个列datagridview,通过在运行时逐行读取文本文件来填充。每次文本文件的内容都会不同。
我希望用户能够通过在文本框中输入字符来过滤datagridview中的列表。数据没有“绑定”到datagridview,因为此时我不知道是否有必要,我不完全理解它。
这是我加载datagridview的代码,文本框名为txtFilter。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'read all lines from the file into a string array (one line per string)
Dim lines() As String = My.Computer.FileSystem.ReadAllText("c:\list_in.txt").Replace(vbLf, "").Split(vbCr)
Dim dgrow As DataGridViewRow
Dim dgcell As DataGridViewCell
'insert each line of input into a row in the datagrid
For Each line As String In lines
dgrow = New DataGridViewRow
dgcell = New DataGridViewTextBoxCell
If line <> "" Then
dgcell.Value = line
dgrow.Cells.Add(dgcell)
DataGridView1.Rows.Add(dgrow)
End If
Next
DataGridView1.Columns("ObjectName").ReadOnly = True
DataGridView1.ClearSelection()
End Sub
答案 0 :(得分:0)
修改强>
查看您的解决方案,我建议您在Dim lines() As String = My.Computer.FileSystem.ReadAllText("c:\list_in.txt").Replace(vbLf, "").Split(vbCr)
sub之外执行txtFilter_TextChanged
,否则每次用户输入密钥时都会导入整个列表,这是不必要的。如果在用户使用该程序时列表可能会发生变化,我建议添加一个“刷新”按钮,特别是如果您的文本文件可能很长。
当用户按下Enter
键时,您还添加了几行代码来删除声音。如果用户需要按回车键进行搜索,则每次用户在文本框中输入新字符时都不需要更新DataGridView
。这在内存上会更容易,如果你有一个大的文本文件,这也会非常有用。
我确信这是一种更简单的方法,但这是我的方法。
Private Sub txtFilter_TextChanged(sender As Object, e As EventArgs) Handles txtFilter.TextChanged
Dim searchedlines(-1) As String 'create an array to fill with terms that match our search
If txtFilter.Text = Nothing Then
datapopulate(lines) 'just populate the datagridview with our text file array
Else
For Each line In lines
If line Like "*" & txtFilter.Text & "*" Then 'check if anything in our search matches any of our terms
ReDim Preserve searchedlines(UBound(searchedlines) + 1) 'resize the array to fit our needs
searchedlines(UBound(searchedlines)) = line 'add the matched line to our array
End If
Next
datapopulate(searchedlines) 'populate the datagrid with our matched terms array
End If
End Sub
Private Sub datapopulate(ByVal mylist)
Dim dgrow As DataGridViewRow
Dim dgcell As DataGridViewCell
DataGridView1.Rows.Clear() 'clear the grid
For Each line As String In mylist 'do the same thing here that we did on form load
dgrow = New DataGridViewRow
dgcell = New DataGridViewTextBoxCell
dgcell.Value = line
dgrow.Cells.Add(dgcell)
DataGridView1.Rows.Add(dgrow)
Next
End Sub
我所做的是创建了一个子,可以在txtFilter
中的文本发生变化时进行处理。或者,您可以在处理按钮单击的子中运行该代码。鉴于此,据我所知,ReDim
在内存使用方面可能是一个代价高昂的项目,如果您的文本文档长达数百行,您可能需要在按钮上单击。你可以使用一个列表,但我还没有玩过足够的知识如何去做。
一个重要的注意事项:为了使子txtFilter_TextChanged
能够看到lines()
,我将其定义在子类之外但在主类内部,以便所有潜艇都可以访问它,像这样:
Public Class Form1
Dim lines() As String = My.Computer.FileSystem.ReadAllText("c:\list_in.txt").Replace(vbLf, "").Split(vbCr)
'...subs here...
End Class
我希望这有助于您开始使用!
答案 1 :(得分:0)
我有一个解决方案。非常感谢你。
Private Sub txtFilter_TextChanged(sender As Object, e As EventArgs) Handles txtFilter.TextChanged
'read all lines from the file into a string array (one line per string)
Dim lines() As String = My.Computer.FileSystem.ReadAllText("c:\list_in.txt").Replace(vbLf, "").Split(vbCr)
Dim dgrow As DataGridViewRow
Dim dgcell As DataGridViewCell
DataGridView1.Rows.Clear()
'insert each line of input into a row in the datagrid
For Each line As String In lines
dgrow = New DataGridViewRow
dgcell = New DataGridViewTextBoxCell
If line.Contains(txtFilter.Text) Then
dgcell.Value = line
dgrow.Cells.Add(dgcell)
DataGridView1.Rows.Add(dgrow)
End If
Next
DataGridView1.Columns("ObjectName").ReadOnly = True
DataGridView1.ClearSelection()
End Sub
我还发现这可以消除用户在输入过滤器文本时按下回车键时响铃。
Private Sub txtFilter_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtFilter.KeyPress
' this keeps the bell from ringing when the user presses the 'enter' key
If Asc(e.KeyChar) = 13 Then
e.Handled = True
End If
End Sub