My purpose is to remove a specific Row..

patrick

Well-known member
Joined
Dec 5, 2021
Messages
238
Programming Experience
1-3
My purpose is to remove a specific Row..
The source below runs the entire For statement from for (int i = 0; i < listNoAssign.Items.Count; i++) to listNoAssign.Items.Count .

In the meantime, it has been removed,
for (int i = 0; i < listNoAssign.Items.Count; i++) Here's the problem.


C#:
                for (int i = 0; i < listNoAssign.Items.Count; i++)
                {
                    CheckBox chk = this.listNoAssign.Items[i].FindControl("chkSELECT") as CheckBox;

                    if (chk.Checked == true)
                    {
                        listNoAssign.Items.RemoveAt(i);
                    }
                }

                this.listNoAssign.DataSource = NoAssignInfoList;
                this.listNoAssign.DataBind();
                ViewState["NoAssignInfoList"] = NoAssignInfoList;
 
You have to be clearer about the problem you are encountering. Are you getting a compilation error? Are getting an exception?
 
My purpose is to remove a specific Row..
The source below runs the entire For statement from for (int i = 0; i < listNoAssign.Items.Count; i++) to listNoAssign.Items.Count .

In the meantime, it has been removed,
for (int i = 0; i < listNoAssign.Items.Count; i++) Here's the problem.


C#:
                for (int i = 0; i < listNoAssign.Items.Count; i++)
                {
                    CheckBox chk = this.listNoAssign.Items[i].FindControl("chkSELECT") as CheckBox;

                    if (chk.Checked == true)
                    {
                        listNoAssign.Items.RemoveAt(i);
                    }
                }

                this.listNoAssign.DataSource = NoAssignInfoList;
                this.listNoAssign.DataBind();
                ViewState["NoAssignInfoList"] = NoAssignInfoList;


In the source,
after doing this ===> listNoAssign.Items.RemoveAt(i);
From here ====> for (int i = 0; i < listNoAssign.Items.Count; i++)
listNoAssign.Items.Count <==== This becomes a minus.
 
If you remove items from a collection, wouldn't you expect the count of items to decrease?
 
Loop from highest index to lowest when removing items from that list.
 
You have to be clearer about the problem you are encountering. Are you getting a compilation error? Are getting an exception?
C#:
foreach (ListViewItem listNoView in listNoAssign.SelectedItems)
                {
                    listNoAssign.Items.Remove(listNoView);

                    lvOriginal.Items[iIdx].SubItems[2].Text = MCMN.trim(listNoView.SubItems[2].Text);

                    iIdx++;
                }


Comparing with the above source, now the source is normally removed.
because ====> foreach (ListViewItem listNoView in listNoAssign.SelectedItems) <===== foreach ( SelectedItems )
 
Last edited by a moderator:
Loop from highest index to lowest when removing items from that list.

C#:
[CODE=csharp]foreach (ListViewItem listNoView in listNoAssign.SelectedItems)
                {
                    listNoAssign.Items.Remove(listNoView);

                    lvOriginal.Items[iIdx].SubItems[2].Text = MCMN.trim(listNoView.SubItems[2].Text);
                    iIdx++;
                }
[/CODE]

Comparing with the above source, now the source is normally removed.
because ====> foreach (ListViewItem listNoView in listNoAssign.SelectedItems) <===== foreach ( SelectedItems )
 
Last edited by a moderator:
So what? You are doing an apples to oranges comparison. Your code in post #1 is iterating by index over the entire list. The code you are comparing against in post #7 is iterating by object reference over the selected items in the list.

@JohnH was trying to relieve your pain by shortcutting your learning to easiest way to modify your existing code to get the behavior that you wanted: deleting the selected items. It looks like you really want to learn why you are seeing the behavior you are currently getting, and why the solution presented is the easiest. My kudos to you.
 
Back
Top Bottom