Dans mon application C#, j'ai deux contrôles ListBox
. Un ListBox
, nommé lstCategory
, est rempli d'éléments récupérés à partir d'une base de données. L'autre ListBox
est nommé lstSelCategory
. Je souhaite déplacer les éléments sélectionnés de lstCategory
vers lstSelCategory
, puis trier les éléments dans lstSelCategory
. Comment pourrais-je faire cela efficacement? Toute aide serait appréciée.Déplacer des éléments de liste d'une liste à une autre
protected void BtnCopyNext_Click(object sender, EventArgs e)
{
try
{
MakeDecision(lstCategory,lstSelCategory);
}
catch (Exception ex)
{
}
}
private void MakeDecision(ListBox Source, ListBox Target)
{
int[] selectedIndices;
try
{
selectedIndices = Source.GetSelectedIndices();
if (!(selectedIndices.Length == 0))
{
Copy(Source, Target);
}
}
catch (Exception ex)
{
throw ex;
}
}
private void Copy(ListBox Source, ListBox Target)
{
int[] selectedIndices;
ListItemCollection licCollection;
ListBox objTarget;
try
{
selectedIndices = Source.GetSelectedIndices();
licCollection = new ListItemCollection();
objTarget = new ListBox();
if (Target != null && Target.Items.Count > 0)
{
foreach (ListItem item in Target.Items)
{
objTarget.Items.Add(item);
}
Target.Items.Clear();
}
int selectedIndexLength = selectedIndices.Length;
for (int intCount = 0; intCount < selectedIndexLength; intCount++)
{
licCollection.Add(Source.Items[selectedIndices[intCount]]);
}
int collectionCount = licCollection.Count;
for (int intCount = 0; intCount < collectionCount; intCount++)
{
Source.Items.Remove(licCollection[intCount]);
if (!objTarget.Items.Contains(licCollection[intCount]))
objTarget.Items.Add(licCollection[intCount]);
}
Target.DataSource = ConvertToArrayList(objTarget);
Target.DataBind();
}
catch (Exception ex)
{
throw ex;
}
finally
{
licCollection = null;
objTarget = null;
}
}
private ArrayList ConvertToArrayList(ListBox Source)
{
ArrayList arrayList;
try
{
arrayList = new ArrayList();
foreach (ListItem item in Source.Items)
arrayList.Add(item.Text);
arrayList.Sort();
}
catch (Exception ex)
{
throw ex;
}
return arrayList;
}
S'il vous plaît poster votre code - nous ne pouvons pas aider à diagnostiquer ce qui ne va pas jusqu'à ce que nous voyons votre code. –