none
filter datagridview RRS feed

  • Question

  • I export a text file to the Data Grid View
    I want to filter DatagridView data by deleting unwanted rows
    The following code has been used and is not working

      If Not TextBox1.Text = "" Then
                Dim i As Integer
                For i = 0 To DataGridView1.Rows.Count - 1
                    If DataGridView1.Rows(i).Cells(1).Value.Contains(TextBox1.Text.ToLower) Then
                        DataGridView1.Rows(i).Cells(1).Value.ToString.Remove(DataGridView1.Rows(i).Cells(1).Value)
                    End If
                Next
            Else
            End If

    Tuesday, February 20, 2018 3:10 PM

Answers

  • I export a text file to the Data Grid View
    I want to filter DatagridView data by deleting unwanted rows
    The following code has been used and is not working

      If Not TextBox1.Text = "" Then
                Dim i As Integer
                For i = 0 To DataGridView1.Rows.Count - 1
                    If DataGridView1.Rows(i).Cells(1).Value.Contains(TextBox1.Text.ToLower) Then
                        DataGridView1.Rows(i).Cells(1).Value.ToString.Remove(DataGridView1.Rows(i).Cells(1).Value)
                    End If
                Next
            Else
            End If

    Hi

    As far as I can understand your question, here is an amended version based on your code. Try it out.

    	If Not Trim(TextBox1.Text) = Nothing Then
    	  For i As Integer = 0 To DataGridView1.Rows.Count - 1
    		If i < DataGridView1.NewRowIndex Then
    		  If DataGridView1.Rows(i).Cells(1).Value.ToString.ToLower.Contains(TextBox1.Text.ToLower) Then
    			DataGridView1.Rows.RemoveAt(i)
    		  End If
    		End If
    	  Next
    	End If
    


    Regards Les, Livingston, Scotland

    • Marked as answer by monemas Tuesday, February 20, 2018 5:18 PM
    Tuesday, February 20, 2018 4:55 PM

  • error

    Error    1    'AsEnumberable' is not a member of 'System.Data.DataTable'.  

    That is very strange is the reference needs is System.Data.DataSetExtensions which is a standard reference for a windows project


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by monemas Tuesday, February 20, 2018 5:19 PM
    Tuesday, February 20, 2018 4:14 PM
    Moderator
  • I know this code looks to simple to you. 

    However, it does everything you want. 

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim txt = IO.File.ReadAllLines("D:\Test\DGH.Txt")
            Dim dt As New DataTable
            For i = 1 To 3
                dt.Columns.Add(New DataColumn("Z" & CStr(i)))
            Next
            For i = 0 To 2
                TextBox1.Text &= txt(i) & vbCrLf
                dt.Rows.Add(txt(i).Split(","c))
            Next
            DataGridView1.DataSource = dt.DefaultView
            dt.DefaultView.RowFilter = "Z1 <> 'B'"
        End Sub
    End Class


    Success
    Cor

    • Marked as answer by monemas Tuesday, February 20, 2018 5:18 PM
    Tuesday, February 20, 2018 4:51 PM

All replies

  • You would be better off importing the text file into say a DataTable or List(Of T) where T represents the data. Either one can be set to the DataSource of the DataGridView.

    From here you can use LINQ or Lambda to query the data.

    Hand written code, no Visual Studio

    Dim dt As DataTable = CType(DataGridView1.DataSource, Datatable)

    Dim results = dt.AsEnumberable().Where(Function(row) row.Field(Of String)("SomeField").ToLower = TextBox1.Text.ToLower)

    That is a basic starter and we can go deeper by using a Select preceding the Where to even get the row number if needed.

    You can then use For/Next to iterate the results and delete them.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 20, 2018 3:21 PM
    Moderator
  • You would be better off importing the text file into say a DataTable or List(Of T) where T represents the data. Either one can be set to the DataSource of the DataGridView.

    From here you can use LINQ or Lambda to query the data.

    Hand written code, no Visual Studio

    Dim dt As DataTable = CType(DataGridView1.DataSource, Datatable)

    Dim results = dt.AsEnumberable().Where(Function(row) row.Field(Of String)("SomeField").ToLower = TextBox1.Text.ToLower)

    That is a basic starter and we can go deeper by using a Select preceding the Where to even get the row number if needed.

    You can then use For/Next to iterate the results and delete them.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Do not use a database

    from textfile to datagridview direct

    • Edited by monemas Tuesday, February 20, 2018 3:24 PM
    Tuesday, February 20, 2018 3:23 PM
  • You should know that a DataTable is not just for databases. I've loaded DataTables with just about everything from text files, xml files to database tables.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Tuesday, February 20, 2018 3:43 PM
    Moderator
  • You should know that a DataTable is not just for databases. I've loaded DataTables with just about everything from text files, xml files to database tables.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    error

    Error    1    'AsEnumberable' is not a member of 'System.Data.DataTable'.  

    Tuesday, February 20, 2018 4:02 PM

  • error

    Error    1    'AsEnumberable' is not a member of 'System.Data.DataTable'.  

    That is very strange is the reference needs is System.Data.DataSetExtensions which is a standard reference for a windows project


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by monemas Tuesday, February 20, 2018 5:19 PM
    Tuesday, February 20, 2018 4:14 PM
    Moderator
  • I know this code looks to simple to you. 

    However, it does everything you want. 

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim txt = IO.File.ReadAllLines("D:\Test\DGH.Txt")
            Dim dt As New DataTable
            For i = 1 To 3
                dt.Columns.Add(New DataColumn("Z" & CStr(i)))
            Next
            For i = 0 To 2
                TextBox1.Text &= txt(i) & vbCrLf
                dt.Rows.Add(txt(i).Split(","c))
            Next
            DataGridView1.DataSource = dt.DefaultView
            dt.DefaultView.RowFilter = "Z1 <> 'B'"
        End Sub
    End Class


    Success
    Cor

    • Marked as answer by monemas Tuesday, February 20, 2018 5:18 PM
    Tuesday, February 20, 2018 4:51 PM
  • I export a text file to the Data Grid View
    I want to filter DatagridView data by deleting unwanted rows
    The following code has been used and is not working

      If Not TextBox1.Text = "" Then
                Dim i As Integer
                For i = 0 To DataGridView1.Rows.Count - 1
                    If DataGridView1.Rows(i).Cells(1).Value.Contains(TextBox1.Text.ToLower) Then
                        DataGridView1.Rows(i).Cells(1).Value.ToString.Remove(DataGridView1.Rows(i).Cells(1).Value)
                    End If
                Next
            Else
            End If

    Hi

    As far as I can understand your question, here is an amended version based on your code. Try it out.

    	If Not Trim(TextBox1.Text) = Nothing Then
    	  For i As Integer = 0 To DataGridView1.Rows.Count - 1
    		If i < DataGridView1.NewRowIndex Then
    		  If DataGridView1.Rows(i).Cells(1).Value.ToString.ToLower.Contains(TextBox1.Text.ToLower) Then
    			DataGridView1.Rows.RemoveAt(i)
    		  End If
    		End If
    	  Next
    	End If
    


    Regards Les, Livingston, Scotland

    • Marked as answer by monemas Tuesday, February 20, 2018 5:18 PM
    Tuesday, February 20, 2018 4:55 PM