共用方式為


SPListItemCollection.Delete method

刪除集合中的指定索引處的項目。

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Sub Delete ( _
    iIndex As Integer _
)
'用途
Dim instance As SPListItemCollection
Dim iIndex As Integer

instance.Delete(iIndex)
public void Delete(
    int iIndex
)

參數

  • iIndex
    Type: System.Int32

    指定的索引為 32 位元整數。

備註

Delete方法會刪除它在集合中的索引為基礎的項目。若要刪除其識別碼為基礎的項目,請使用DeleteItemById方法。

如果指定的索引超出索引集合的有效範圍,這個方法會傳回ArgumentOutOfRangeException例外狀況。

Examples

下列程式碼範例會刪除從指定的清單在其中一個整數欄位值是小於 70 或文字欄位值等於無任何項目。

For迴圈的範例計數向下 (intindex-- ) 而不是向上 (intindex++ ),因為正在刪除的項目和項目數目就會減少,每個增量。

Dim site As SPWeb = SPControl.GetContextWeb(Context)
Dim srcList As SPList = site.Lists("List_Name")
Dim listItems As SPListItemCollection = srcList.Items
Dim intIndex As Integer

For intIndex = listItems.Count - 1 To 0 Step -1

    If Convert.ToInt32(listItems(intIndex)("Field1_Name")) < 70 _ 
        OrElse listItems(intIndex)("Field2_Name").ToString() = "None" Then

        listItems.Delete(intIndex)

    End If

Next i
SPWeb oWebsite = SPContext.Current.Web;
SPList oList = oWebsite.Lists["List_Name"];
SPListItemCollection collListItems = oList.Items;

for (int intIndex = collListItems.Count - 1; intIndex > -1; intIndex--)
{
    if (Convert.ToInt32(collListItems[intIndex]["Field1_Name"]) < 70 ||
        collListItems[intIndex]["Field2_Name"].ToString() == "None")
    {
        collListItems.Delete(intIndex);
    }
}

請參閱

參照

SPListItemCollection class

SPListItemCollection members

Microsoft.SharePoint namespace