Click here to Skip to main content
11,575,921 members (56,914 online)
Rate this: bad
good
Please Sign up or sign in to vote.
SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["MyList"];
    if (list != null)
    {
        for (int i = list.ItemCount - 1; i >= 0; i--)
        {
            list.Items[i].Delete();
        }
        list.Update();
    }

I have more Items in an sharepoint list then when i in an listbox select only one item and delete if with the following code , then it deletes al the items in my list , not just the only i have selected , can somebody help me ?
Posted 2-Dec-12 21:14pm
Kurac1866
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Because you delete all the items in your code!
You should try to match your criteria before delete it.

SPWeb web = SPContext.Current.Web;
    SPList list = web.Lists["MyList"];
    if (list != null)
    {
        for (int i = list.ItemCount - 1; i >= 0; i--)
        {
            if( list.Items[i].equals("your criteia ")){
               list.Items[i].Delete();
            }
        }
        list.Update();
    }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As you have a loop and in a loop you have put a code, which delete itmes so all items will deleted, if you want to delete only the selected element then don't take a loop and use this code,
 
 if (list != null)
{
     list.Items.Remove(list.SelectedItem);
        list.Update();
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 330
1 F-ES Sitecore 145
2 OriginalGriff 125
3 Dave Kreskowiak 120
4 Abhinav S 98
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 600
2 Abhinav S 438
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100