【问题标题】:Use Drag and Drop between list boxes DataSource在列表框数据源之间使用拖放
【发布时间】:2018-12-21 02:47:38
【问题描述】:

我正在尝试使用 tutorial 实现拖放功能

所以首先我先用DataSource 填充,比如:

   var techListQuery = $"exec getEmployeeListMailing";
   var techList = db.GetTableBySQL(techListQuery);

    lstTechUnnotified.DataSource = techList;
    lstTechUnnotified.DisplayMember = "Abbreviation";
    lstTechUnnotified.ValueMember = "UserName";

一旦我这样做了,我就有了Mouse_Down 第一个列表框的事件,但是当它尝试从第一个列表中删除项目时问题就开始了:

 if (dde1 == DragDropEffects.All)
            {
                lstTechUnnotified.Items.RemoveAt(lstTechUnnotified.IndexFromPoint(e.X, e.Y));
            }

我收到消息:

'当 DataSource 属性为 设置

我能做些什么来解决这个问题?问候

【问题讨论】:

  • 错误信息很清楚。当 ListBox 具有 DataSource 时,您不能修改它。而是从数据源中删除该项,然后重新设置 ListBox 的 DataSource 属性。

标签: winforms drag-and-drop listbox


【解决方案1】:

发生这种情况是因为您没有将项目添加到 Items 属性。如果您使用的是DataSource,则必须从DataSource 本身(即techList)中删除该项目。然后你必须通过重新分配数据源来更新列表框

techList.RemoveAt(lstTechUnnotified.IndexFromPoint(e.X, e.Y));

// Update the listbox
lstTechUnnotified.DataSource = null; // This is necessary, otherwise the LB donesn't notice
                                     // the change.
lstTechUnnotified.DataSource = techList;

如果您使用的是BindingList,则会自动更新列表框。


您还可以查看我在 Code Project 上的文章 Drag-and-Drop ListBox,其中描述了高级拖放技术。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2020-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-20
    相关资源
    最近更新 更多