Resolve WinForm under ListBox control "Set DataSource property Cannot modify item collection"

Source: Internet
Author: User

Rarely write WinForm the first time you use the ListBox control, you are experiencing a more disgusting problem. Because I didn't want to manually bind the item in the listbox to use DataSource, but when I did some additions and deletions, I reported the error "The item collection cannot be modified after setting the DataSource property." Really disgusting, do not know how the design of the listbox to give the DataSource attribute can not casually change, and I want to implement in a ListBox to select a few items and then put into another listbox in the function, can not use datasource words too troublesome. On the blog Park checked the next did not find a solution, can only own hands and clothing.

Because some people say that the reason for this is that "after this binding in the WinForm program is directly related to the data source DataTable, the changes will affect the DataTable." So the solution is if you want to Make changes to the items to get a new DataSource rebind, after the test effect is good. I encapsulate the action of moving the item between the two ListBox into a class with the following code:

To illustrate, there must be a generic to indicate the object type of the binding, at first did not want to use generics, but the transfer several times after the Displayermember property can not be set by the field to show the more strange. Hope to be helpful to friends who have the same problem.

After the enthusiastic netizens to remind, added to the DataTable support. To facilitate encoding and unified invocation The generic parameter is DataRow when using the DataTable as the data source, and the new code is as follows:

view Plaincopy to Clipboardprint?


using System;


using System.Collections;


using System.Collections.Generic;


using System.Data;


using System.Windows.Forms;





namespace Lucifer


{


//For the unified processing of two list type controls transfer items


public static class Lstctrlmove_mgr<t></t>


  {


//delete items from a ListBox


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;


//Because the direct deletion of the DataRow will be saved, so the ugly way to deal with the


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<t></t> lst = new list<t></t> ();


lst. AddRange ((list<t></t>) lstbox.datasource);


lst. RemoveAll (Delegate (T item1)


        {


foreach (T item2 in items)


          {


if (item1. Equals (item2))


return true;


          }


return false;


        });


Lstbox.datasource = LST;


      }


    }


//Add items to a ListBox


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<t></t> lst = new list<t></t> ();


if (lstbox.datasource!= null)


lst. AddRange ((list<t></t>) lstbox.datasource);


foreach (T item in items)


        {


lst. ADD (item);


        }


Lstbox.datasource = LST;


      }


    }


//Transfers ListBox1 selected items to ListBox2 and removes
from ListBox1

public static void Move (ListBox LstBox1, ListBox LstBox2)


    {


if (LstBox1.SelectedItems.Count > 0)


      {


Additems (LstBox2, Lstbox1.selecteditems);


Removeitems (LstBox1, Lstbox1.selecteditems);


      }


    }


//Transfer the entire LstBox1 item to the ListBox2 and empty 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<t></t>) lstbox1.datasource);


Lstbox1.datasource = new list<t></t> ();


      }


    }


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.