Posted by: R Manimaran | October 11, 2007

ListView Search in Visual Studio 2003

In Visual Studio .net 2003’s (.net V.1.1) ListView control does not contains the search feature. In Visual Studio 2005, they have added this functionality. Here I have try a Search procedure which will search the ListView. There is a bool parameter to this function to include the subitems text in the search. We can also start our search from a specified index value of the listview. 2 Constructors

v      FindItemByText(ListView lstView,string SearchText)

v      FindItemByText(ListView lstView,string SearchText,bool includeSubItemsInSearch,int StartIndex)

 And here the Procedure 

private void FindItemByText(ListView lstView,string SearchText)

{  

 int index=0;

for(int i=0;i<lstView.Items.Count;i++)

 {

if(lstView.Items[i].SubItems[0].Text.ToUpper()==SearchText.ToUpper())    {    

  index=i;   

   break;  

  }

 }

 lstView.FullRowSelect=true; 

lstView.Items[index].Selected=true;

 lstView.Select();

} 

private void FindItemByText(ListView lstView,string SearchText,bool includeSubItemsInSearch,int StartIndex)

{int colcount=0;int index=0;

  if(includeSubItemsInSearch==true)            colcount=lstView.Columns.Count; 

     else    

        colcount=1; 

      if(StartIndex>lstView.Items.Count)  

          StartIndex=lstView.Items.Count;

for(int j=0;j<colcount;j++)

{for(int i=StartIndex;i<lstView.Items.Count;i++)

 { if(lstView.Items[i].SubItems[j].Text.ToUpper()==SearchText.ToUpper()) 

    {        index=i;        break; 

     } 

 }

if(index!=0)break;

}

lstView.FullRowSelect=true;

lstView.Items[index].Selected=true;

lstView.Select();

}

Happy Coding………..

Advertisements

Responses

  1. Thanks for the great code! 🙂

    • i tried FindItemByText ,it works but there is an extra text concated with my serarch text

      it looks like this: ListViewSubItem: {my search text}

      how to remove the label ‘ListViewSubItem’ from my search text.

      thanks for your help.

  2. sory ,
    i forgot to add .text after subitem[index]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: