2009-10-30 180 views
0

在我的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; 
} 
+0

请发布您的代码 - 我们不能帮助诊断出现问题,直到我们看到您的代码。 – 2009-10-30 13:08:55

回答

0

为什么不只是?

var r = (from p in lstCategory.SelectedItems.Cast<string>() 
    where p.Length > 0 
    orderby p 
    select p); 

lstSelcategory.Items.AddRange(r.ToArray()); 
+0

当然,这在3.5以前的.net版本中不起作用。 – 2009-10-30 13:23:06

0

看起来这里有很多不必要的工作要做。

是否有任何特别的原因,您将ListBox的项目转换为ArrayList?它的项目已经执行IEnumerable,并应该工作得很好,如DataSource

另外,为什么databind?您已经通过Add方法将项目从一个列表框移动到另一个列表框。除非我在这里没有看到什么,否则数据绑定操作很昂贵并且完全没有必要。

UPDATE 您要求粘贴代码,所以我要粘贴一些代码和讨论:

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]); 
     } 

到现在为止,你已经做了你需要的几乎一切去做。这些项目已被添加到ListBox控件中。当页面呈现时,框架将遍历ListItem集合并为这些项目呈现适当的HTML。如果为控件启用ViewState,则这些项目将为其缓存ViewState

但是,你移动到这样做:

  Target.DataSource = ConvertToArrayList(objTarget); 
     Target.DataBind(); 

    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 
    finally 
    { 
     licCollection = null; 
     objTarget = null; 
    } 
} 

现在,我的数据绑定的理解告诉我,这一系列语句的完全覆盖任何东西,你已经存储在ListItemCollection。因此,您在上述步骤中所做的所有工作都被抹杀了。

此外,您拨打一个电话给一个函数,ConvertToArrayList

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; 
} 

现在,这一切都是好的,很好,但问题是这样的:你可以设置ListItemCollection为您的数据源。或者,正如我们所显示的那样,您可以完全避免数据绑定,因为您已经自己构建了列表,并且放弃了此函数调用。

看起来您需要决定是要自己构建列表还是数据绑定。如果你要进行数据绑定,请清理你的工作方式。如果您不打算进行数据绑定,请删除构建列表并对其进行管理并非绝对必需的任何代码。

ALSO:

  • 你捕获异常(Exception型的,不会少),并没有做任何事的,但重新抛出它。如果您不打算对该例外进行任何操作,请删除 Catch条款。如果您计划重新抛出异常,把它像这样保存堆栈跟踪:

    try 
    { 
        // Blahdy blahdy 
    } catch (Exception e) 
    { 
        throw; // Do not pass e; this preserves the stack trace. 
    } 
    
  • 手动设置对象为null Finally子句中从旧的语言保持在不具有自动垃圾收集。我会消除这些代码,因为它只是使函数的主体混乱,并使其更难阅读。

  • 鉴于上述两点,你看起来像你可以删除整个Try...Catch...Finally,只是让例外气泡食物链。

+0

请粘贴该代码。 – Sakthivel 2009-10-30 14:50:13

相关问题