Solved

Help with partial string search

Posted on 2013-01-08
7
214 Views
Last Modified: 2013-01-08
I am using the code below to select rows in a Grid where values of SDeactivation is found agains SDéactivation_ID which is in Coumn(2). SDeactivation_ID contains single values.

When SDeactivation is a single value all is working of but when SDeactivation includes multiple values (i.e. 1,2,3) it does not work. How do i modify the code to select rows when using multiple values for SDeactivation?

MsgBox("keySDeactivation)" & keySDeactivation)
        '  keySDeactivation = "1,2" does not works
        '  keySDeactivation = "1" works
        If keySDeactivation <> "" Then
            Dim _tableA As DataView = dtMasterA.DefaultView
                  C1SDeactivation.DataSource = _tableA
                    For i As Integer = 0 To C1SDeactivation.RowCount - 1
                If C1SDeactivation.Columns(2).CellValue(i).ToString().Contains(keySDeactivation) = True Then
                    C1SDeactivation.SelectedRows.Add(i)
                End If
            Next
        End If


Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 38754030
So your cell value contains a single value such as 1 or 2...

in other words, you can have
               C1SDeactivation.Columns(2).CellValue(i) =1
or           C1SDeactivation.Columns(2).CellValue(i) = 2
etc...

And you are comparing that to keySDeactivation where you can have
             keySDeactivation = 1
             keySDeactivation = 2
             keySDeactivation = "1,2"
etc..

Am I understanding that correctly?

<< If C1SDeactivation.Columns(2).CellValue(i).ToString().Contains(keySDeactivation) = True Then>>

If so, I think your Contains statement is backwards.

Try rewriting it like this:

                If keySDeactivation.Contains(C1SDeactivation.Columns(2).CellValue(i).ToString()) = True Then

Open in new window

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38754221
To make sure that 1 does not match with 11 using Contains function, I would add commas at start and end of the keySDeactivation and do following comparison

Outside loop:
keySDeactivation = "," & keySDeactivation & ","


Inside Loop:
If keySDeactivation.Contains("," & C1SDeactivation.Columns(2).CellValue(i).ToString() & ",") = True Then
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38754249
You could use regular expressions:

Imports System.Text.RegularExpressions

...

If Regex.IsMatch(C1SDeactivation.Columns(2).CellValue(i).ToString(), "\b" + keySDeactivation + "\b") Then
    C1SDeactivation.SelectedRows.Add(i)
End If

...

Open in new window

0
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

Author Closing Comment

by:vcharles
ID: 38754529
Thank You for all the comments. This thread was the solution.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38754627
You are sure you wont have numbers greater than 9 in there?
0
 

Author Comment

by:vcharles
ID: 38754641
Yes, that is ok, those are just values of the ID fields I will use to select the rows.

Thanks.

Victor
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38754661
And you will never have an ID greater than 9(such as 10, 11 etc).  The reason I ask is that using the contains method as you are, 1 will match if there is 11 but no 1. So if you are trying to select just 11, both 1 and 11 will get selected.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question