Solved

Help with partial string search

Posted on 2013-01-08
7
212 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
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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

763 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