解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”

解决WinForm下ListBox控件“设置DataSource属性后无法修改项集合”

最近更新: 2013-2-15    587  

很少写WinForm程序第一次使用ListBox控件就遇到了比较恶心的问题。因为我不想手 动绑定ListBox中的Item就使用了DataSource,但是当我进行一些添加、删除操作时就报 了这个错“设置DataSource属性后无法修改项集合”。实在太恶心了,不知道 设计ListBox的人是怎么想的给了DataSource属性却不能随便更改,而我要实现在一个 ListBox中选中几项然后放到另一个ListBox中的功能,不能用DataSource的话太麻烦了。 上博客园查了下没有找到解决办法,只能自己动手丰衣足食了。

因为有人说引起这个的原因是“在winForm程序中这样绑定之后是直接和数据源 DataTable相关,改动项会对DataTable造成影响”既然这样那解决方法就是如果要 对Items做更改就从新弄个DataSource重新绑定,试验后效果不错。我把操作两个ListBox 之间互相移动item的操作封装到一个类里,代码如下:

说明一下,这里必须用泛型来指明所绑定的对象类型,一开始没想用泛型的,可是转 移几次以后就不能按照DisplayerMember属性设置的字段来显示了比较奇怪。希望对遇到 相同问题的朋友有帮助。

经热心网友提醒,加入了对DataTable的支持。为了便于编码和统一调用使用 DataTable做数据源时泛型参数为DataRow,新代码如下:

view plaincopy to clipboardprint?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace Lucifer
{
  //用于统一处理两个List类型控件之间互相转移Items
  public static class LstCtrlMove_Mgr
  {
    //从一个ListBox中删除Items
    public static void RemoveItems(ListBox lstBox, IEnumerable items)
    {
      if (typeof(T) == typeof(DataRow))
      {
        DataTable dt = ((DataTable)lstBox.DataSource);
        DataTable newDt = dt.Clone();
        bool flag = false;
        //因为直接删除DataRow会保存,所以用这样丑陋的方式处理了
        foreach (DataRow dr in dt.Rows)
        {
          foreach (DataRowView item in items)
          {
            if (dr == item.Row)
            {
              flag = true;
              break;
            }
            else
              flag = false;
          }
          if (!flag)
            newDt.Rows.Add(dr.ItemArray);
          else
            continue;
        }
        lstBox.DataSource = newDt;
      }
      else
      {
        List lst = new List ();
        lst.AddRange((List)lstBox.DataSource);
        lst.RemoveAll(delegate(T item1)
        {
          foreach (T item2 in items)
          {
            if (item1.Equals(item2))
              return true;
          }
          return false;
        });
        lstBox.DataSource = lst;
      }
    }
    //向一个ListBox中添加Items
    public static void AddItems(ListBox lstBox, IEnumerable items)
    {
      if (typeof(T) == typeof(DataRow))
      {
        DataTable dt = null;
        foreach (object item in items)
        {
          if(item is DataRowView)
            dt = ((DataRowView)item).Row.Table.Clone();
          if (item is DataRow)
            dt = ((DataRow)item).Table.Clone();
          break;
        }
        if (lstBox.DataSource != null)
          dt = ((DataTable)lstBox.DataSource).Copy();
        foreach (object item in items)
        {
          if(item is DataRowView)
            dt.Rows.Add(((DataRowView)item).Row.ItemArray);
          if (item is DataRow)
            dt.Rows.Add(((DataRow)item).ItemArray);
        }
        lstBox.DataSource = dt;
      }
      else
      {
        List lst = new List ();
        if (lstBox.DataSource != null)
          lst.AddRange((List) lstBox.DataSource);
        foreach (T item in items)
        {
          lst.Add(item);
        }
        lstBox.DataSource = lst;
      }
    }
    //将ListBox1的选定项转移到ListBox2中,并从ListBox1中去除
    public static void Move(ListBox lstBox1, ListBox lstBox2)
    {
      if (lstBox1.SelectedItems.Count > 0)
      {
        AddItems(lstBox2, lstBox1.SelectedItems);
        RemoveItems(lstBox1, lstBox1.SelectedItems);
      }
    }
    //将整个lstBox1的项转移到ListBox2中,并清空ListBox1
    public static void MoveAll(ListBox lstBox1, ListBox lstBox2)
    {
      if (typeof(T) == typeof(DataRow))
      {
        DataTable dt = (DataTable)lstBox1.DataSource;
        AddItems(lstBox2, dt.Rows);
        lstBox1.DataSource = dt.Clone();
      }
      else
      {
        AddItems(lstBox2, (List) lstBox1.DataSource);
        lstBox1.DataSource = new List();
      }
    }
  }
}

原文地址:https://www.cnblogs.com/meimao5211/p/3346290.html