Help with Concatenating search code for XML file

Hello,

I'm using 4 grids, Grid 1 is to hold values of a link file that contains the id fields of the and Donor XML files, which are loaded in Grids 3 and 4, Grid 2 is used to display the results.

My problem is the users will not always search by Donor or Receiver, and sometimes they will search by multiple fields using the "AND" or "OR" condition. How do I concatenate the code when user searches by multiple fields?

Below is my current code:

Dim dt As New DataTable()

        dt.Columns.Add("Link_ID")

        dt.Columns.Add("Receiver")

        dt.Columns.Add("Donor")  'Add (1)

 

        Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")

        Dim receiver As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")

        Dim Donor As XElement = XElement.Load(Application.StartupPath + "\Donor.xml") 'Add (2)

 

 

        For Each item As XElement In linker.Elements("Row")

            Dim linkID As String = item.Element("Link_ID").Value

            Dim receiverId As String = item.Element("Receiver_ID").Value

            Dim DonorID As String = item.Element("Donor_ID").Value 'Add (3)

 

            Dim receiverVal As String = String.Empty

            Dim xe As XElement = receiver.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Receiver_ID").Value = receiverId).FirstOrDefault()

            If xe IsNot Nothing Then

                receiverVal = xe.Element("Receiver").Value

            End If

 

            Dim DonorVal As String = String.Empty 'Add (4)

            xe = Donor.Elements("Row").Cast(Of XElement)().Where(Function(n) n.Element("Donor_ID").Value = DonorID).FirstOrDefault()

            If xe IsNot Nothing Then

                DonorVal = xe.Element("Donor").Value

            End If

 

            Dim dr As DataRow = dt.NewRow()

            dr("Link_ID") = linkID

            dr("Receiver") = receiverVal

            dr("Donor") = DonorVal 'Add (5)

            dt.Rows.Add(dr)

        Next

 

‘Bind Link File to Grid1

 

        Dim bs As New BindingSource()

        bs.DataSource = dt

        C1TrueDBGrid1.DataSource = bs

        Dim FilteredDT As DataTable

 

‘View data based on receiver and Donor data selected from grid

        Dim DV As New DataView(dt, "Receiver like " & "'" & C1TrueDBGrid3.Columns(1).Value & "'" & " and Donor like " & "'" & C1TrueDBGrid4.Columns(1).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)

 

‘Display results in Grid2

 

FilteredDT = DV.ToTable

C1TrueDBGrid2.DataSource = FilteredDT

 

 

        Me.C1TrueDBGrid2.Splits(0).DisplayColumns(0).Width = Me.C1TrueDBGrid2.Splits(0).DisplayColumns(0).Width - 100

        Me.C1TrueDBGrid2.Splits(0).DisplayColumns(1).Width = Me.C1TrueDBGrid2.Splits(0).DisplayColumns(1).Width + 5

        Me.C1TrueDBGrid2.Splits(0).DisplayColumns(2).Width = Me.C1TrueDBGrid2.Splits(0).DisplayColumns(2).Width - 40




When I select receiver I want to search with the following code:


 Dim DV As New DataView(dt, "Receiver like " & "'" & C1TrueDBGrid3.Columns(1).Value & "'" & "", Nothing, DataViewRowState.CurrentRows)

When I select receiver and donor I want to search with the following code:


Dim DV As New DataView(dt, "Receiver like " & "'" & C1TrueDBGrid3.Columns(1).Value & "'" & " and Donor like " & "'" & C1TrueDBGrid4.Columns(1).Value & "'" & "", Nothing
, DataViewRowState.CurrentRows)



When I decide to search by receiver and Donor How do I concatenate the code? I will have multiple search criterias, donor and receiver are only two of 6 serach criteries which will depend on which ones the users decide to search by. Therefore it's impossible to hardcode as if only two will be selected.

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
AmitGKCommented:
Assuming that column "Link_ID" is unique and contains values > 1.
Always include column Link_ID in your search criteria. Depending on whether the other columns are selected or not inculde them in the search criteria

Dim SearchCriteria as string = "Link_ID <> -1"

/*  if receiver is selected */
if receiverIsSelected = True then
   SearchCriteria &= " AND Receiver like " & "'" & C1TrueDBGrid3.Columns(1).Value & "'"
End If
/*  if donor is selected */
if donorIsSelected = True then
   SearchCriteria &= " AND Donor like " & "'" & C1TrueDBGrid4.Columns(1).Value & "'"
End If

/* Finally apply this SearchCriteria to your dataview*/
Dim DV As New DataView(dt, SearchCriteria, Nothing, DataViewRowState.CurrentRows)

Hope this helps
0
 
vcharlesAuthor Commented:
THANK YOU VERY MUCH.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.