this.listBoxControl2.AllowDrop = true;//获取或设置一个值,该值指示控件是否可以接受用户拖放到它上面的数据。
//源ListBoxControl
private void listBoxControl1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if ((p != Point.Empty) && ((Math.Abs(e.X - p.X) > SystemInformation.DragSize.Width) || (Math.Abs(e.Y - p.Y) > SystemInformation.DragSize.Height)))
{
DataRowView row = listBoxControl1.SelectedItem as DataRowView;
List<DataRow> rows = new List<DataRow>();
rows.Add(row.Row);
listBoxControl1.DoDragDrop(rows, DragDropEffects.Copy);
// 方法二
//listBoxControl1.DoDragDrop(sender, DragDropEffects.Copy);
}
}
}
//源ListBoxControl
private void listBoxControl1_MouseDown(object sender, MouseEventArgs e)
{
ListBoxControl c = sender as ListBoxControl;
p = new Point(e.X, e.Y);
int selectedIndex = c.IndexFromPoint(p);
if (selectedIndex == -1)
p = Point.Empty;
}
//目标ListBoxControl
private void listBoxControl2_DragDrop(object sender, DragEventArgs e)
{
ListBoxControl listBox = sender as ListBoxControl;
DataTable table = listBox.DataSource as DataTable;
List<DataRow> rows = e.Data.GetData(typeof(List<DataRow>)) as List<DataRow>;
if (rows != null && table != null)
{
if (rows.Count > 0)
{
for (int i = 0; i < rows.Count; i++)
{
//将 System.Data.DataRow 复制到 System.Data.DataTable 中,保留任何属性设置以及初始值和当前值。
table.ImportRow(rows[i]);
//把源ListBoxControl里面数据行删除
rows[i].Delete();
}
}
}
//方法二
//listBoxControl1.Refresh();
//ListBoxControl listBox = sender as ListBoxControl;
//Point newPoint = new Point(e.X, e.Y);
//newPoint = listBox.PointToClient(newPoint);
//int selectedIndex = listBox.IndexFromPoint(newPoint);
//object item = listBoxControl1.Items[listBoxControl1.IndexFromPoint(p)];
//if (selectedIndex == -1)
// listBox.Items.Add(item);
//else
// listBox.Items.Insert(selectedIndex, item);
}
//目标ListBoxControl
private void listBoxControl2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
拖拽数据为List<DataRow>,GridControl同样可以接收。
本文介绍了如何在C#中使用DevExpress的listBoxControl实现数据的拖放功能。通过设置AllowDrop属性允许拖放,然后在鼠标移动和按下事件中处理拖放源的操作。在目标listBoxControl的DragDrop和DragEnter事件中处理数据接收和效果显示。拖放的数据类型为List<DataRow>,适用于GridControl等组件。
1472

被折叠的 条评论
为什么被折叠?



