Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Filter Datagridview On UNBOUND Column

$
0
0

I have a datagridview "datagridview1" which has a bindingsource of "PayorContractedBindingSource". It has a number of checkboxcolumns that need to be updated. My business requirement is such that I need to add 2 unbound columns "BIN and PCN" to that datagridview1 so the rows can be identified visually. An additional business requirement is that I have 2 txtboxes "txtBIN and txtPCN" which, when changed, will perform a filter on their respective columns and display only what is being typed into the txtboxes in the datagridview1.

Originally I was populating datagridview1 from a bindingsource that used a sql join query to show all the columns that were needed and my filter worked fine as can be seen in the example code below for the PCN column:

Private Sub txtPCN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPCN.TextChanged
    If txtPCN.TextLength > 0 Then
        PayorContractedBindingSource.Filter = String.Format("PCN Like '" & txtPCN.Text) & "*'"
    Else
        PayorContractedBindingSource.Filter = String.Empty
    End If
End Sub

However, since the initial query was based on a join, I could not perform an update on changes to datagridview1. So I changed it to a sql query of just a single table and added the unbound columns to provide the additional data.

Now, of course, the PayorContractedBindingSource only knows what to do with bound columns for the filtering. I tried adding a DataPropertyName for those unbound columns but it makes no difference - it still won't recognize them when trying to filter.

I tried the following code for the BIN field to see what happens and I get a NULLREFERENCEEXCEPTION as soon as I type a second character into the txtBIN textbox.

Private Sub txtBIN_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBIN.TextChanged
    If txtBIN.TextLength > 0 Then
        For y As Integer = 0 To DataGridView1.Rows.Count - 1
            Me.DataGridView1.DataSource.Filter = String.Format("'" & Me.DataGridView1.Rows(y).Cells(1).Value.ToString & "'" & " Like '" & txtBIN.Text) & "*'"
        Next
    Else
        Me.DataGridView1.DataSource.Filter = String.Empty

    End If
End Sub


Obviously I'm missing something here (my code above is probably completely wrong). So I need see an example of how to execute a filter on these unbound columns.


Please help! Lost way too much hair already... 


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>