Link to home
Start Free TrialLog in
Avatar of kranthi4uonly
kranthi4uonly

asked on

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

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);
             }
           
Avatar of gelbert
gelbert

Use "SelectedItems" properties of ListBox. Do for every item of this collection the same what you do for single selected item
Avatar of Carl Tawn
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.
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;
                }
            }
         
                                }
Avatar of kranthi4uonly

ASKER

hi  carl_tawn: s ia mgettingthis exception how to do that can u please help me thanks
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);
}

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]);
}
ASKER CERTIFIED SOLUTION
Avatar of gelbert
gelbert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial