在我的C#应用程序中,我有两个ListBox
控件。 一个ListBox
,名为lstCategory
,填充了从数据库中检索的项目。其他ListBox
被命名为lstSelCategory
。将列表项从一个列表框移动到另一个列表
我想将所选项目从lstCategory
转换为lstSelCategory
,然后对lstSelCategory
中的项目进行排序。我该如何有效地做这件事?任何帮助,将不胜感激。
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;
}
请发布您的代码 - 我们不能帮助诊断出现问题,直到我们看到您的代码。 – 2009-10-30 13:08:55