.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Case Insensitive search using FindByValue method

Posted By:Faizal       Posted Date: January 17, 2010    Points: 10    Category: Collections    URL: http://www.dotnetspark.com  

Custom FindByValue Method which will search the value in the dropdownlist or ListBox in case insensitive form. Dropdownlist or Listbox FindByValue method does not provide the search value in case insensitive mode
 

Dropdownlist or Listbox FindByValue method does not provide the search value in case insensitive mode. Suppose if you have a value Text1 and you are trying to find text1 than  FindByValue of the dropdownlist will return false because FindByValue method accept case sensitive value.

So, below is the solution. Instead of using built in FindByValue method You can use below method if will work in both the cases case sensitive and insensitive.

C# Code
private bool FindByValue(DropDownList ddl, string value)
{
        bool isFound = false;
        foreach (ListItem item in ddl.Items)
        {
            if (string.Compare(value, item.Value, true) == 0)
            {
                isFound = true;
                break; 
            }
        }

        return isFound;
}

Vb.Net

Private Function FindByValue(ByVal ddl As DropDownList, ByVal value As String) As Boolean
    Dim isFound As Boolean = False
    For Each item As ListItem In ddl.Items
        If String.Compare(value, item.Value, True) = 0 Then
            isFound = True
            Exit For
        End If
    Next
    
    Return isFound
End Function

     

Further Readings:

Responses
Author: Scott Hooper         Company URL: http://www.dotnetspark.com
Posted Date: March 31, 2010

Great article! This was precisely the lateral thinking I was unable to muster at the time. I was however inspired to extend the functionality to return the list index of the item being searched for. I did this by overloading the function to provide both alternatives as shown below. I had to use a For...Next loop rather than For Each, so as to get the loop counter.

Public Shared Function FindByValue(ByVal ddl As DropDownList, ByVal Value As String) As Boolean

Dim isFound As Boolean = False

For Each item As ListItem In ddl.Items
If String.Compare(Value, item.Value, True) = 0 Then
isFound = True
Exit For
End If
Next

Return isFound

End Function

Public Shared Function FindByValue(ByVal ddl As DropDownList, ByVal Value As String, ByVal ReturnIndex As Boolean) As Integer

Dim itemIndex As Integer = -1 ''default - not in list

For i = 0 To ddl.Items.Count - 1
If String.Compare(Value, ddl.Items(i).Value, True) = 0 Then
itemIndex = i
Exit For
End If
Next

Return itemIndex

End Function

Author: Scott Hooper         Company URL: http://www.dotnetspark.com
Posted Date: March 31, 2010

hmmm, posted comments don't seem to preserve formatting.
Author: Faizal         Company URL: http://www.dotnetspark.com
Posted Date: March 31, 2010

Hi Scott,

Your solution from your code...Ya I agree comment of the resource does not format the code..I will raise this issue to webmaster of www.dotnetspark.com.

Thanks
Faizal

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend