Error when using DataView.RowFilter to filter DatagridView

philiop

Member
Joined
May 6, 2010
Messages
22
Programming Experience
Beginner
Hi,

I'm getting the error system.indexoutofrangeexception when trying to filter a datagridview's datasource(dataview).

This is the code I use to fill the Datagridview

VB.NET:
FileDav = New DataView
        Try
            With FileDav
                .Table = ds.Tables("dboFile")
                .AllowDelete = False
                .AllowEdit = True
                .AllowNew = False
                .RowFilter = "IsActive = 1"
                .RowStateFilter = DataViewRowState.CurrentRows
                .Sort = "ShortFileName"
            End With
            DataGridView1.Columns.Clear()
            DataGridView1.DataBindings.Clear()
            With DataGridView1
                .AutoGenerateColumns = False
                .Columns.Insert(0, New DataGridViewTextBoxColumn)
                .Columns(0).Name = "ShortFileName"
                .Columns(0).HeaderText = "Short File Name"
                .Columns(0).DataPropertyName = "ShortFileName"
                .Columns(0).ReadOnly = True
                .Columns.Insert(1, New DataGridViewTextBoxColumn)
                .Columns(1).Name = "MatterIdString"
                .Columns(1).HeaderText = "Matter ID"
                .Columns(1).DataPropertyName = "MatterIDString"
                .Columns(1).ReadOnly = True
                .Columns.Insert(2, New DataGridViewCheckBoxColumn)
                .Columns(2).Name = "IsActive"
                .Columns(2).HeaderText = "Active / Closed"
                .Columns(2).DataPropertyName = "IsActive"
                .Columns(2).ReadOnly = True
                .Columns.Insert(3, New DataGridViewCheckBoxColumn)
                .Columns(3).Name = "IsSelected"
                .Columns(3).DataPropertyName = "IsSelected"
                .Columns(3).HeaderText = "Is Selected?"
                .Columns(3).ReadOnly = False
                .Columns.Insert(4, New DataGridViewTextBoxColumn)
                .Columns(4).Name = "ID"
                .Columns(4).HeaderText = "ID"
                .Columns(4).DataPropertyName = "Id"
                .Columns(4).ReadOnly = True
                .Columns(4).Visible = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .RowHeadersVisible = False
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AllowUserToOrderColumns = False
                .AllowUserToResizeColumns = False
                .AllowUserToResizeRows = False
                .DataSource = FileDav
            End With
        Catch ex As Exception
            SendErrorByMailMessage(ex.Message)
        End Try

I then use this to filter it

VB.NET:
        ' MatterIdString <> ''"""
        Dim Search As String
        Search = SearchTextBox.Text.Replace("'", "''")
        SearchString = "MatterIdString <> ''"
        If ComboBox1.SelectedValue = 0 Then
        Else
            SearchString = SearchString & " AND FileTypeId = " & ComboBox1.SelectedValue
        End If
        If ShowClosed = True Then
        Else
            SearchString = SearchString & " AND IsActive = 1"
        End If
        If SearchTextBox.Text = "" Then
        Else
            SearchString = SearchString & " AND (ShortFileName LIKE '%" & Search & "%' OR MatterIDString LIKE '%" & Search & "%')"
        End If
        If ComboBox2.SelectedValue = 0 Then
        Else
            SearchString = SearchString & " AND FileStatusID = " & ComboBox2.SelectedValue
        End If
        SearchString = SearchString
        Try
            FileDav.RowFilter = SearchString
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

The datagridview loads fine and the filter works until I try to search for something that isn't there.

I don't understand why it does this though as doing this on another dataview works fine and just brings up a blank datagrid.

Anyone have any idea?

Kind Regards

Philip Smith
 
I've got a fix for my problem unless there is a better way to do this.

I've created a datareader that checks to see if the search string would bring up any results. If it doesn't it stops the filter from being applied and brings up an error message.

VB.NET:
       Dim Search As String
        Search = SearchTextBox.Text.Replace("'", "''")
        SearchString = "MatterIdString <> ''"
        If ComboBox1.SelectedValue = 0 Then
        Else
            SearchString = SearchString & " AND FileTypeId = " & ComboBox1.SelectedValue
        End If
        If ShowClosed = True Then
        Else
            SearchString = SearchString & " AND IsActive = 1"
        End If
        If SearchTextBox.Text = "" Then
        Else
            SearchString = SearchString & " AND (ShortFileName LIKE '%" & Search & "%' OR MatterIDString LIKE '%" & Search & "%')"
        End If
        If ComboBox2.SelectedValue = 0 Then
        Else
            SearchString = SearchString & " AND FileStatusID = " & ComboBox2.SelectedValue
        End If
        If FilterSelected = True Then
            SearchString = SearchString & " AND IsSelected = 1"
        End If
        SearchString = SearchString
        Try
            Dim cmd As SqlCommand
            Dim cnn As SqlConnection
            Dim sql = "SELECT [File].MatterIDString, [File].ShortFileName FROM [File] WHERE (ShortFileName LIKE '%" & Search & "%' OR MatterIDString LIKE '%" & Search & "%')"
            If ShowClosed = True Then
            Else
                sql = sql & " AND IsActive = 1"
            End If
            If FilterSelected = True Then
                sql = sql & " AND IsSelected = 1"
            End If
            cnn = New SqlConnection(connetionString)
            cmd = New SqlCommand(sql, cnn)
            cnn.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader
            If dr.Read = False Then
                If ShowClosed = True Then
                    MessageBox.Show("Your search brought up no clients. Please try again")
                Else
                    MessageBox.Show("Your search brought up no clients. Please try again" & vbNewLine & vbNewLine & _
                                    "Closed Clients are currently filtered")
                    cnn.Close()
                    Exit Sub
                End If
            Else
                'MsgBox("Erm")
                FileDav.RowFilter = SearchString
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
 
Where EXACTLY does the stack trace say that the original exception is thrown? It can't be on that line specifically. It must be as a result of some event that gets raised as a result of that line.
 
Back
Top