2016-05-11 17 views
0

私はバインディングソースにバインドされたリストボックス "listBox_users"とバインドされたリストボックス "listBox_map"を持っています。 listBox_usersからlistBox_mapにユーザをドラッグアンドドロップしたいのですが、listBox_mapのバインディングソースを削除すると、これがうまくいきました。 私の問題は、データソースプロパティが定義されている場合listBox_mapに新しいアイテムを追加していないということです:DataSourceプロパティが設定されている場合バインドされたリストボックスに新しい項目を追加する

Itemsコレクションを変更することはできません。

private void listBox_map_DragDrop(object sender, DragEventArgs e) 
    { 

     if (e.Data.GetDataPresent(DataFormats.StringFormat)) 
     { 
      string str = listBox_users.Text; 

      listBox_map.Items.Add(str); // Error here! 
     } 

    } 

は、どのように私はバインドさリストボックスに新しいアイテムを追加することができますか? ありがとうございます。代わりに使用

listBox_map.Items.Add(str); // Error here! 

+0

代わりに使用listBox_map.Items.Addのデータソースに項目を追加します。 –

+0

このトピックを表示することができます:http://stackoverflow.com/questions/5035744/items-collection-cannot-be-modified-when-the-datasource-property-is-set –

+0

私はそれを見ましたが、彼は "削除データソース "と私はそれを維持する必要があります。 –

答えて

0

はそれを行う:それは自動的にあなたのlistBox_mapに反映されます

yourBindingSourceReference.Add(str); 

MSDN - BindingSource.Add

0

私は自分でそれを修正するので、私はDataSourceプロパティを削除し、私は、リストボックス内のすべての行を追加しました:

var q = 
      from user in SundicappEntities.Instance.users 
       join map in SundicappEntities.Instance.user_profile_map on user.id_user equals map.id_user 
       where map.id_profile==id_profile 
       select new 
       { 

          ListBoxItemDisplayText = user.name_user + " " + user.f_name_user  

       }; 
     foreach (var u in q) 
     { 
      listBox_map.Items.Add(u.ListBoxItemDisplayText); 
     } 
関連する問題