我的Form上有2个listbox,我现在想做的是,用鼠标拖动listbox1中的一项将它添加到listbox2中。
代码如下:
private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (listBox1.Items.Count == 0)
            {
                return;
            }
            else
            {
                int index = listBox1.IndexFromPoint(e.X, e.Y);
                string s = listBox1.Items[index].ToString();
                DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);                //if (dde1 == DragDropEffects.All)
                //{
                //    listBox1.Items.RemoveAt(listBox1.IndexFromPoint(e.X, e.Y));
                //    //删除listbox1中移走的项
                //}
            }
        }        private void listBox2_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }
        
        private void listBox2_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.StringFormat))
            {
                string addString = (string)e.Data.GetData(DataFormats.StringFormat);
                listBox2.Items.Add(addString);
            }
        }
其中,listbox1 中的数据是从数据库中读取的。
现在的问题是:当不连接数据库时,拖动正常,但是,连接上数据库时,拖过去的就是System.Data.DataRowView.
这是什么原因?那位大侠知道。

解决方案 »

  1.   

     string s = listBox1.Items[index].ToString();
    你确定 s 里面试你要拖拽的内容么?还有在拖拽进入目标空间的时候 要判断拖入的类型
    不合法类型 让鼠标变成禁止的样子
      

  2.   

    这句有问题么?当不连接数据库时,显示正确。
    当连接数据库时,我刚用断点查看了下,即时窗口显示的是"System.Data.DataRowView"。
    这是怎么回事?
      

  3.   

    问题出在
    DragDropEffects dde1 = DoDragDrop(s, DragDropEffects.All);怎么改?哪位知道?说说原因。