Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with partial string search

Posted on 2013-01-08
7
Medium Priority
?
216 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 2000 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

718 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