Hi
A lot of repetitive code to look through there. The only thing I kept thinking while trying to undestand what you were doing was 'why not use a filter' = you hav datatables, bindingsources all eager to assist in what I believe you are doing.
Anyway, I may way off the mark, but here is a stand alone text project that filters the rows with any matches from user input into a textbox in real time. It generates some pecular and random data just to try it out, and as someting is entered into the textbox, all rows containing the search term in any column are displayed. No data is 'lost', and just clearing the search input textbox will display the original whole data. As I say, just ignor this if it is off track.
Option Strict Off
Option Explicit On
' DGV DT DYNAMIC ROW FILTER
' This project attempts to allow User
' to Filter all columns in real time
' This example needs a Form1 with
' a blank DataGridView1 and
' TextBox1
Public Class Form1
Dim Label1 As New Label
Dim rand As New Random
Dim myTable As New DataTable("Freddy")
Dim BS As New BindingSource
Dim Path As String = Application.StartupPath & "\Data\Data.xml"
'"C:\Users\lesha\Documents\Projects\!!!STORE\DataTable DGV and filters\WindowsApplication2\bin\Debug\Data\Data.xml"
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim parent As String = My.Computer.FileSystem.GetParentPath(Path)
' if the Folder doesn't exist, create it
If Not IO.Directory.Exists(parent) Then
IO.Directory.CreateDirectory(parent)
End If
' save data file when exit the application
myTable.WriteXml(Path)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Size = New Size(940, 250)
' no Data file, so create dummy data
' this will be saved on app exit
With myTable
' add some random columns
.Columns.Add("Integer", GetType(Integer))
.Columns.Add("Boolean", GetType(String))
.Columns.Add("Double", GetType(Double))
.Columns.Add("Firstname", GetType(String))
.Columns.Add("Lastname", GetType(String))
.Columns.Add("Gender", GetType(String))
.Columns.Add("DateTime", GetType(String))
.Columns.Add("Decimal", GetType(Decimal))
.Columns.Add("Sum 0 2 7", GetType(Decimal), "Integer + Double + Decimal")
' just some test data
Dim r1() As String = {"Freddy", "Mary", "Brian", "Led", "Smith", "Brown", "MacHine", "Elizabedh", "Jones", "Tom"}
Dim r2() As String = {"Male", "Female", "Undeclared", "T-Shirt Male", "T-Shirt Female", "T-Shirt Child", "T-Shirt Formal", "T-Shirt XXXL"}
Dim r3() As Boolean = {True, False}
Dim md As Double = Double.MaxValue
.Rows.Add(11, True, 12, "Freddy", "MacHine", "Male", Now.AddDays(11).Date.ToString("ddd, dd MMM"), 13)
.Rows.Add(3334, False, 13.122345678901271, "Mary", "Smith", "Female", Now.AddDays(12).Date.ToString("ddd, dd MMM"), 1.25)
.Rows.Add(3335, True, 129.12234567890124, "George", "Mathews", "undeclared", Now.AddDays(13).Date.ToString("ddd, dd MMM"), 1.255)
For i As Integer = 0 To 99
.Rows.Add(i, If(rand.Next(0, 2) = 1, "False", "True"), rand.NextDouble() * 10000, r1(rand.Next(r1.Length)), r1(rand.Next(r1.Length)), r2(rand.Next(r2.Length)), Now.Date.ToString("ddd, dd MMM"), rand.NextDouble() * 100)
Next
End With
BS.DataSource = myTable
' just some formatting of DGV
With DataGridView1
.DataSource = BS
.MultiSelect = False
.RowHeadersWidth = 24
.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False
.SelectionMode = DataGridViewSelectionMode.CellSelect
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.Columns("Sum 0 2 7").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
For Each c As DataGridViewColumn In .Columns
c.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
Next
.DefaultCellStyle.BackColor = Color.PaleGoldenrod
.DefaultCellStyle.Padding = New Padding(8, 2, 2, 8)
.Columns("DateTime").DefaultCellStyle.Format = "dd MMM yyyy"
With .Columns("Sum 0 2 7").DefaultCellStyle
.BackColor = Color.PaleGreen
.Format = "0.000"
.Font = New Font("Arial", 14, FontStyle.Bold)
End With
.Columns("Sum 0 2 7").ReadOnly = True
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
End With
With Label1
.Location = New Point(TextBox1.Right + 8, TextBox1.Top)
.Font = New Font(.Font.FontFamily, 14, FontStyle.Bold)
.BorderStyle = BorderStyle.FixedSingle
.BackColor = Color.LightGreen
.ForeColor = Color.Black
.Text = " Found = 0 "
.AutoSize = True
.Anchor = AnchorStyles.Left Or AnchorStyles.Bottom
End With
Controls.Add(Label1)
TextBox1.Select()
End Sub
'Private Sub DataGridView1_CellValidated(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellValidated
' Label3.Text = myTable.Compute("Sum(Integer)", Nothing)
' Label4.Text = myTable.Compute("Sum(Double)", Nothing)
' Label5.Text = myTable.Compute("Sum(Decimal)", Nothing)
'End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
DoFilter(Trim(TextBox1.Text))
End Sub
Sub DoFilter(t As String)
myTable.DefaultView.RowFilter = String.Empty
If t.Length < 1 Then Exit Sub
Dim fstring As String = Nothing
If IsNumeric(t) Then
For Each c As DataColumn In myTable.Columns
If c.DataType Is GetType(Integer) Or c.DataType Is GetType(Double) Or c.DataType Is GetType(Decimal) Then
fstring &= "[" & c.ColumnName & "] = " & t & " Or "
End If
Next
Else
For Each c As DataColumn In myTable.Columns
If Not (c.DataType Is GetType(Integer) Or c.DataType Is GetType(Double) Or c.DataType Is GetType(Decimal)) Then
fstring &= "[" & c.ColumnName & "] Like '" & t & "*' or "
End If
Next
End If
fstring = fstring.Substring(0, fstring.Length - 3)
myTable.DefaultView.RowFilter = fstring
If myTable.DefaultView.Count < 1 Then
With Label1
.BackColor = Color.Pink
.Text = " No Matches "
End With
Exit Sub
End If
With Label1
.BackColor = Color.PaleGreen
.Text = " Found = " & myTable.DefaultView.Count.ToString & " "
End With
End Sub
End Class