Home All Groups Group Topic Archive Search About

can't remove listitem from ddl in loop

Author
12 Dec 2006 7:02 PM
mharness
Hello,

Any ideas why this code will not remove listitems for the dropdownlist?

    Protected Sub CullProjects()
    Dim ProjectName As String
        If Me.gvRequestProjects.Rows.Count > 0 Then
            For Each row As GridViewRow In Me.gvRequestProjects.Rows
                ProjectName =
Trim(Me.gvRequestProjects.DataKeys(row.RowIndex).Values(1).ToString)
                Me.ddlProjects.Items.Remove(ProjectName)
            Next
        End If
    End Sub

ProjectName is a valid name of an item in the ddl.  Using something very
similar to this on a one-by-one basis and it works correctly but not in this
code.

Thanks,

Mike

Author
15 Dec 2006 7:49 AM
Philipp Landolt
Hi Mike,

might be because the indexes of the datagrid will be corrupt when you
deleting items in a loop after which is joined by same collection.
I've had nearly the same problem with a listbox and solved it by an opposite
way.
Means writing all items which are not selected in a list and bind them with
the listbox AFTER the ForEach - Loop.

Public Sub btn_removejob_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim lst_joblist As WebControls.ListBox
Dim lic_joblist As New WebControls.ListItemCollection()
Dim lst_tmpitem As WebControls.ListItem
For Each lst_tmpitem In lst_joblist.Items
  If lst_tmpitem.Selected = False Then
   lic_joblist.Add(lst_tmpitem.Text)
  End If
Next
lst_joblist.DataSource = lic_joblist
lst_joblist.DataBind()
end sub

Hope this will solve your problem.

regards
phil

Show quoteHide quote
"mharness" <bo***@hotmail.com> schrieb im Newsbeitrag
news:da2dnQMoPITCluLYnZ2dnUVZ_qemnZ2d@comcast.com...
> Hello,
>
> Any ideas why this code will not remove listitems for the dropdownlist?
>
>    Protected Sub CullProjects()
>    Dim ProjectName As String
>        If Me.gvRequestProjects.Rows.Count > 0 Then
>            For Each row As GridViewRow In Me.gvRequestProjects.Rows
>                ProjectName =
> Trim(Me.gvRequestProjects.DataKeys(row.RowIndex).Values(1).ToString)
>                Me.ddlProjects.Items.Remove(ProjectName)
>            Next
>        End If
>    End Sub
>
> ProjectName is a valid name of an item in the ddl.  Using something very
> similar to this on a one-by-one basis and it works correctly but not in
> this code.
>
> Thanks,
>
> Mike
>