none
filter datagridview RRS feed

  • Question

  • hi

    Use the following code to filter the DataView
    Problem If the column you want to search is empty, the filtering process does not appear

       For i As Integer = 0 To DATAGRID_NAME.Rows.Count - 1
                If Not IsDBNull(DATAGRID_NAME.Rows(i).Cells(2).Value) AndAlso Not DATAGRID_NAME.Rows(i).Cells(2).Value.ToString.Contains(TXT_SEARCHDATGRID.Text.ToLower) Then
                    DATAGRID_NAME.Rows.RemoveAt(i)
                End If
            Next

    Sunday, September 23, 2018 8:06 PM

Answers

  • Hi

    I believe your problem may be due to your loop counting from zero toupper bound. Whatb happens is that when an item is deleted, then the loop fails because the item count has been changed - in other words, you can't modify the items in such a way.

    To do something like this, then you would need to count from upper bound down to 0 where such issues are not a problem.

    So, try this:

    For i As Integer = DATAGRID_NAME.Rows.Count - 1 To 0 Step - 1
    also, be aware of a row being the NewRow.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, September 23, 2018 8:17 PM
    • Marked as answer by ahmeddc Sunday, September 23, 2018 8:20 PM
    Sunday, September 23, 2018 8:15 PM

All replies

  • Hi

    I believe your problem may be due to your loop counting from zero toupper bound. Whatb happens is that when an item is deleted, then the loop fails because the item count has been changed - in other words, you can't modify the items in such a way.

    To do something like this, then you would need to count from upper bound down to 0 where such issues are not a problem.

    So, try this:

    For i As Integer = DATAGRID_NAME.Rows.Count - 1 To 0 Step - 1
    also, be aware of a row being the NewRow.


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, September 23, 2018 8:17 PM
    • Marked as answer by ahmeddc Sunday, September 23, 2018 8:20 PM
    Sunday, September 23, 2018 8:15 PM
  • hi  leshay

    All thanks and respect

    Sunday, September 23, 2018 8:20 PM