We help IT Professionals succeed at work.

how to delete multiple items from a list box and removing it from dataset datatable IN "WINDOWS APPLICATION"

kranthi4uonly
on
1,301 Views
Last Modified: 2013-12-17
how to delete multiple items from a list box and removing it from dataset datatable IN "WINDOWS APPLICATION"
this is my code it deletes only single item but how to delete mutiple items  and that should be removed from the dataset datable aslo here is my code
 public void RemoveItems()
        {
         
           string temp = listBox1.SelectedItem.ToString();
            for(int i =0;i<incidentTypeDataSet.IncidentTypeAlias.Rows.Count;i++)
            {
                if (incidentTypeDataSet.IncidentTypeAlias[i].AliasId == temp)
                {
                    parentManager.ProcessAction("AliasRemove");
                    incidentTypeDataSet.IncidentTypeAlias.Rows.RemoveAt(i);
                    break;
                }
            }
         
                   listBox1.Items.Remove(listBox1.SelectedItem);
             }
           
Comment
Watch Question

Commented:
Use "SelectedItems" properties of ListBox. Do for every item of this collection the same what you do for single selected item
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
But remember to loop through the list of selected items backwards if you're going to remove them, otherwise you will find yourself getting "Index Out Of Range" exceptions.

Commented:
foreach (ListItem i in ListBox1.Items)
        {
            if (i.Selected)
            {
               //delete i.Value from data
            }
        }
public void RemoveItems()
        {
         
           string temp = listBox1.SelectedItem.ToString();
            for(int i =incidentTypeDataSet.IncidentTypeAlias.Rows.Count  ;i>0  ;i-- )
            {
                if (incidentTypeDataSet.IncidentTypeAlias[i].AliasId == temp)
                {
                    parentManager.ProcessAction("AliasRemove");
                    incidentTypeDataSet.IncidentTypeAlias.Rows.RemoveAt(i);
                    listBox1.Items.RemoveAt(i);
                    //break;
                }
            }
         
                                }

Author

Commented:
hi  carl_tawn: s ia mgettingthis exception how to do that can u please help me thanks

Commented:
When you are deleting items, do not use "foreach", use "for" operator. this will prevent you from
using index which is bigger than number of items
for ( int i = listBox1.Items.Count-1; i > -1;i-- )
{
.......
}
Paste your code, so we can help you to correct it
//check this is a reverse loop, so you won't get exception.
for ( int i = listBox1.Items.Count-1; i > -1;i-- )
{
    if(listBox1.Items[i].Selected)
        listBox1.Items.RemoveAt(i);
}

Author

Commented:

here is the code ca nu tell em how to remove or loop thorugh  the dataset datatabel and remove the selected items
for (int i = 0; i < listBox1.SelectedItems.Count; i++)
              {
                  IncidentTypeDataSet.IncidentTypeAliasRow rowTemp = (IncidentTypeDataSet.IncidentTypeAliasRow)incidentTypeDataSet.IncidentTypeAlias.NewRow();

                  // Copy the keys from IncidentType table to IncidentTypeAlias table...
                  rowTemp.AgencyId = incidentTypeDataSet.IncidentType[0].AgencyId;
                  rowTemp.IncidentTypeId = incidentTypeDataSet.IncidentType[0].IncidentTypeId;
                  rowTemp.RecordTypeCode = incidentTypeDataSet.IncidentType[0].RecordTypeCode;
                  rowTemp.AliasId = listBox1.SelectedItems[i].ToString();
                  incidentTypeDataSet.IncidentTypeAlias.Rows.Add(rowTemp);          
              parentManager.ProcessAction("AliasRemove");                          
                  for (int i = 0; i < listBox1.SelectedItems.Count; i++)
                      listBox1.Items.Remove(listBox1.SelectedItems[i]);
}
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.