2015-12-16 28 views
0

我是一個完整的新手。我從這裏學到了很多東西,但是我找不到答案。我正在使用Visual Studio Pro 2015.如何使用textbox vb.net過濾未綁定的datagridview?

我有一個窗體窗體應用程序,它具有單列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

編輯:

看你的解決方案,我會建議你執行Dim lines() As String = My.Computer.FileSystem.ReadAllText("c:\list_in.txt").Replace(vbLf, "").Split(vbCr)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 

我希望這可以幫助你開始!

+0

非常感謝。 – CreoGuy

+0

沒問題。隨意標記它是正確的答案,或者,你知道,贊成它;) – charliefox2

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 
相關問題