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.ToSt ring();
for(int i =0;i<incidentTypeDataSet.I ncidentTyp eAlias.Row s.Count;i+ +)
{
if (incidentTypeDataSet.Incid entTypeAli as[i].Alia sId == temp)
{
parentManager.ProcessActio n("AliasRe move");
incidentTypeDataSet.Incide ntTypeAlia s.Rows.Rem oveAt(i);
break;
}
}
listBox1.Items.Remove(list Box1.Selec tedItem);
}
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.ToSt
for(int i =0;i<incidentTypeDataSet.I
{
if (incidentTypeDataSet.Incid
{
parentManager.ProcessActio
incidentTypeDataSet.Incide
break;
}
}
listBox1.Items.Remove(list
}
Use "SelectedItems" properties of ListBox. Do for every item of this collection the same what you do for single selected item
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
}
}
{
if (i.Selected)
{
//delete i.Value from data
}
}
public void RemoveItems()
{
string temp = listBox1.SelectedItem.ToSt ring();
for(int i =incidentTypeDataSet.Incid entTypeAli as.Rows.Co unt ;i>0 ;i-- )
{
if (incidentTypeDataSet.Incid entTypeAli as[i].Alia sId == temp)
{
parentManager.ProcessActio n("AliasRe move");
incidentTypeDataSet.Incide ntTypeAlia s.Rows.Rem oveAt(i);
listBox1.Items.RemoveAt(i) ;
//break;
}
}
}
{
string temp = listBox1.SelectedItem.ToSt
for(int i =incidentTypeDataSet.Incid
{
if (incidentTypeDataSet.Incid
{
parentManager.ProcessActio
incidentTypeDataSet.Incide
listBox1.Items.RemoveAt(i)
//break;
}
}
}
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
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].Selec ted)
listBox1.Items.RemoveAt(i) ;
}
for ( int i = listBox1.Items.Count-1; i > -1;i-- )
{
if(listBox1.Items[i].Selec
listBox1.Items.RemoveAt(i)
}
ASKER
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.Cou
{
IncidentTypeDataSet.Incide
// Copy the keys from IncidentType table to IncidentTypeAlias table...
rowTemp.AgencyId = incidentTypeDataSet.Incide
rowTemp.IncidentTypeId = incidentTypeDataSet.Incide
rowTemp.RecordTypeCode = incidentTypeDataSet.Incide
rowTemp.AliasId = listBox1.SelectedItems[i].
incidentTypeDataSet.Incide
parentManager.ProcessActio
for (int i = 0; i < listBox1.SelectedItems.Cou
listBox1.Items.Remove(list
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.