DevExpress 17.2.7
我尝试在网格内移动一行
元素“用户控件”放置在“gridControl1”中。
代码包含以下事件:
-“gridView1_MouseDown”;
-“gridControl1_DragOver”;
事件“gridView1\u MouseDown”、“gridView1\u MouseMove”起作用。
事件“gridControl1\u DragOver”、“gridControl1\u DragDrop”不起作用。
如何使“gridControl1\u DragOver”、“gridControl1\u DragDrop”事件工作?
public partial class Frm10UC : UserControl
{
#region *** ÐеÑеменнÑе
// *** ÐеÑеменнÑе
ConectDB conectDB;
#region ÐеÑеÑаÑкивание
#region *** СоÑÑиÑовка
const string OrderFieldName = "sorting";
#endregion **
#endregion
#endregion *** ÐеÑеменнÑе
public Frm10UC()
{
InitializeComponent();
}
private void Frm10UC_Load(object sender, EventArgs e)
{
// ÐодклÑÑение к ÐÐ
// conectDB = new ConectDB(Convert.ToInt32(aIncement));
conectDB = new ConectDB();
conectDB.connect();
// gridControl. Ðаполнение даннÑми
gridControl1.DataSource = conectDB.dt;
gridView1.BestFitColumns();
// СÑÑока. ÐобавиÑÑ "ÐовÑÑ Ð·Ð°Ð¿Ð¸ÑÑ"
// ÐÑобÑажение ÑÑÑоки нового ÑлеменÑа Ð´Ð»Ñ Ð´Ð¾Ð±Ð°Ð²Ð»ÐµÐ½Ð¸Ñ ÑÑÑок в пÑедÑÑавление.
gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Top; // Available modes: Top, Bottom, None (// ÐоÑÑÑпнÑе ÑежимÑ: ÑвеÑÑ
Ñ, ÑнизÑ, неÑ)
// СоÑÑиÑовка
// SortData();
#region *** СоÑÑиÑовка
gridView1.PopulateColumns(); // Ð¡Ð¾Ð·Ð´Ð°ÐµÑ ÑÑолбÑÑ ÑеÑки/Ð¿Ð¾Ð»Ñ ÐºÐ°ÑÑÑ Ð´Ð»Ñ Ð¿Ð¾Ð»ÐµÐ¹ в ÑвÑзанном иÑÑоÑнике даннÑÑ
View.
gridView1.Columns[OrderFieldName].SortOrder = DevExpress.Data.ColumnSortOrder.Ascending; //СоÑÑиÑовка. "Ascending" - по возÑаÑÑаниÑ
gridView1.OptionsCustomization.AllowSort = false; // ÐолÑÑÐ°ÐµÑ Ð¸Ð»Ð¸ Ð·Ð°Ð´Ð°ÐµÑ Ð·Ð½Ð°Ñение, опÑеделÑÑÑее, могÑÑ Ð»Ð¸ конеÑнÑе полÑзоваÑели пÑименÑÑÑ ÑоÑÑиÑÐ¾Ð²ÐºÑ Ð´Ð°Ð½Ð½ÑÑ
.
gridView1.OptionsView.ShowGroupPanel = false; // ÐозвÑаÑÐ°ÐµÑ Ð¸Ð»Ð¸ Ð·Ð°Ð´Ð°ÐµÑ Ð·Ð½Ð°Ñение, опÑеделÑÑÑее, ÑвлÑеÑÑÑ Ð»Ð¸ Ð¿Ð°Ð½ÐµÐ»Ñ Ð³ÑÑÐ¿Ð¿Ñ Ð²Ð¸Ð´Ð¸Ð¼Ð¾Ð¹.
#endregion *** СоÑÑиÑовка
}
#region *** СобÑÑиÑ
GridHitInfo downHitInfo = null; // СодеÑÐ¶Ð¸Ñ Ð¸Ð½ÑоÑмаÑÐ¸Ñ Ð¾ конкÑеÑной ÑоÑке в виде ÑеÑки .
// https://documentation.devexpress.com/WindowsForms/DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo.members
private void gridView1_MouseDown(object sender, MouseEventArgs e)
{
GridView view = sender as GridView;
downHitInfo = null;
GridHitInfo hitInfo = view.CalcHitInfo(new Point(e.X, e.Y));
if (Control.ModifierKeys != Keys.None)
return;
if (e.Button == MouseButtons.Left && hitInfo.InRow && hitInfo.RowHandle != GridControl.NewItemRowHandle)
downHitInfo = hitInfo;
}
private void gridView1_MouseMove(object sender, MouseEventArgs e)
{
GridView view = sender as GridView;
if (e.Button == MouseButtons.Left && downHitInfo != null)
{
Size dragSize = SystemInformation.DragSize;
Rectangle dragRect = new Rectangle(new Point(downHitInfo.HitPoint.X - dragSize.Width / 2,
downHitInfo.HitPoint.Y - dragSize.Height / 2), dragSize);
if (!dragRect.Contains(new Point(e.X, e.Y)))
{
view.GridControl.DoDragDrop(downHitInfo, DragDropEffects.All);
downHitInfo = null;
}
}
}
private void gridControl1_DragOver(object sender, DragEventArgs e) // когда обÑÐµÐºÑ Ð¿ÐµÑеÑаÑкиваеÑÑÑ Ð¿Ð¾Â Ð³ÑаниÑам ÑлеменÑа ÑпÑавлениÑ;
{
if (e.Data.GetDataPresent(typeof(GridHitInfo)))
{
GridHitInfo downHitInfo = e.Data.GetData(typeof(GridHitInfo)) as GridHitInfo;
if (downHitInfo == null)
return;
GridControl grid = sender as GridControl;
GridView view = grid.MainView as GridView;
GridHitInfo hitInfo = view.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y)));
if (hitInfo.InRow && hitInfo.RowHandle != downHitInfo.RowHandle && hitInfo.RowHandle != GridControl.NewItemRowHandle)
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
}
private void gridControl1_DragDrop(object sender, DragEventArgs e) //когда опеÑаÑÐ¸Ñ Ð¿ÐµÑеÑаÑÐºÐ¸Ð²Ð°Ð½Ð¸Ñ Ð·Ð°Ð²ÐµÑÑена;
{
GridControl grid = sender as GridControl;
GridView view = grid.MainView as GridView;
GridHitInfo srcHitInfo = e.Data.GetData(typeof(GridHitInfo)) as GridHitInfo;
GridHitInfo hitInfo = view.CalcHitInfo(grid.PointToClient(new Point(e.X, e.Y)));
int sourceRow = srcHitInfo.RowHandle;
int targetRow = hitInfo.RowHandle;
MoveRow(sourceRow, targetRow);
}
private void MoveRow(int sourceRow, int targetRow)
{
if (sourceRow == targetRow)
return;
GridView view = gridView1;
DataRow row0 = null;
DataRow row1 = null;
DataRow row2 = null;
decimal val1 = 0;
decimal val2 = 0;
if (targetRow == sourceRow + 1)
{
row1 = view.GetDataRow(sourceRow);
row2 = view.GetDataRow(targetRow);
val1 = (decimal)row1[OrderFieldName];
val2 = (decimal)row2[OrderFieldName];
row1[OrderFieldName] = val2;
row2[OrderFieldName] = val1;
view.FocusedRowHandle = sourceRow + 1;
return;
}
if (sourceRow == targetRow + 1)
{
row1 = view.GetDataRow(sourceRow);
row2 = view.GetDataRow(targetRow);
val1 = (decimal)row1[OrderFieldName];
val2 = (decimal)row2[OrderFieldName];
row1[OrderFieldName] = val2;
row2[OrderFieldName] = val1;
view.FocusedRowHandle = sourceRow - 1;
return;
}
row0 = view.GetDataRow(targetRow - 1);
row1 = view.GetDataRow(targetRow);
row2 = view.GetDataRow(targetRow + 1);
DataRow dragRow = view.GetDataRow(sourceRow);
val1 = (decimal)row1[OrderFieldName];
if (row2 == null)
dragRow[OrderFieldName] = val1 + 1;
else
{
val2 = (decimal)row2[OrderFieldName];
if (row0 == null)
dragRow[OrderFieldName] = val1 - 1;
else
dragRow[OrderFieldName] = (val1 + val2) / 2;
}
}
#endregion *** СобÑÑиÑ
}