Gulrej
Gulrej

Reputation: 999

get index of item in listbox c#

I am trying to get index of each items in a listbox Here is my code

           foreach (ListItem lstItem in lstCostCenter.Items)
           {
               int x = lstCostCenter.Items.IndexOf(lstItem);

           }

But each time its returning 0 only.

Please some one help me.

Thanks Gulrej

Upvotes: 2

Views: 4255

Answers (1)

YoryeNathan
YoryeNathan

Reputation: 14502

Why not use a for loop?

for (int i = 0; i < lstCostCenter.Items.Count; i++)
{
    ListItem lstItem = lstCostCenter.Items[i];
}

The index of the current item is i.

Upvotes: 6

Related Questions