Question Display Datagrivdiew rows based on another datagridview filter (Urgent)

Status
Not open for further replies.

wstevens

Member
Joined
May 12, 2008
Messages
16
Location
South Africa PTA
Programming Experience
1-3
Hi All
I have a form with 2 datagridviews each having there own bindingsource using strongly typed datasets.

Both datagridviews do not have all the same columns however there are 3 columns in each datagridview that have a common relationship. For example both have a column "Employee No"

The Filter I created uses the bindingsource.filter on datagridview 1. I can filter by or exclude by on any column in the datagridview. This is where the problems start

I set the the 2nd datagridview bindingsource.filter to equal the first bindingsource.filter This works great for filtering the 3 common columns however a column that does not exist in both datagridviews will cause an error

Both datagridviews have the same amount of records "110" For every row in the first datagridview there is one row in the other datagridview with the same Employee No.

I need to get the first datagridview bindingsource's filtered items employee no and then tell the second datagridview binding source to show those records only

Can anybody help me.
 
This is untested but it should work:
VB.NET:
Dim filterBuilder As New System.Text.StringBuilder("EmployeeNo IN (")

For index As Integer = 0 To Me.BindingSource1.Count - 1
    If index > 0 Then
        filterBuilder.Append(", ")
    End If

    filterBuilder.Append(DirectCast(Me.BindingSource1(index), DataRowView)("EmployeeNo"))
Next

filterBuilder.Append(")")

Me.BindingSource2.Filter = filterBuilder.ToString()
That assumes that EmployeeNo contains Integer values. If it contains Strings then replace this:
VB.NET:
filterBuilder.Append(DirectCast(Me.BindingSource1(index), DataRowView)("EmployeeNo"))
with this:
VB.NET:
filterBuilder.AppendFormat("'{0}'", DirectCast(Me.BindingSource1(index), DataRowView)("EmployeeNo"))
 
Status
Not open for further replies.

Latest posts

Back
Top