私は持っていDataGridView dgv
ます。dgv のセルの 1 つに、グレーの陰影が付けられたセルがあります (残りのセルは空白で白です)。その影付きのセルを右にドラッグしてその列の別のセルにドラッグし、前のセルの位置、値、行番号、列番号など、新しいセルの位置、値、行/列から情報を取得したいと考えています。これは可能ですか?これはチェス盤と非常によく似ており、「駒」をドラッグして 1 つのセルから別のセルに移動します。
6921 次
1 に答える
5
以下のコードは機能しますが、少し整理することはほぼ確実です。これはDataGridViewFAQの例に基づいています。その例では、行をドラッグしているので、セルの値を移動し、マウスの右ボタンで操作するように少し変更しました。
私が機能しなかったことの 1 つは (あまり長くは費やしていませんでしたが)、DoDragDrop
パラメーターを使用してドラッグされたセルを渡すことでした。オブジェクトは null を通過していたので、代わりにフォーム レベル変数に配置しました。
private DataGridViewCell drag_cell;
private Rectangle dragBoxFromMouseDown;
public Form1()
{
InitializeComponent();
dataGridView1.AllowDrop = true;
dataGridView1.MouseDown += new MouseEventHandler(dataGridView1_MouseDown);
dataGridView1.MouseMove += new MouseEventHandler(dataGridView1_MouseMove);
dataGridView1.DragOver += new DragEventHandler(dataGridView1_DragOver);
dataGridView1.DragDrop += new DragEventHandler(dataGridView1_DragDrop);
}
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(
dataGridView1.Rows[rowIndexFromMouseDown],
DragDropEffects.Move);
}
}
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
// The mouse locations are relative to the screen, so they must be
// converted to client coordinates.
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
// Get the row index of the item the mouse is below.
DataGridView.HitTestInfo hti = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
DataGridViewCell targetCell = dataGridView1[hti.ColumnIndex, hti.RowIndex];
// If the drag operation was a move then remove and insert the row.
if (e.Effect == DragDropEffects.Move)
{
targetCell.Value = drag_cell.Value;
dataGridView1.Refresh();
}
}
void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
DataGridView.HitTestInfo hti = dataGridView1.HitTest(e.X, e.Y);
drag_cell = dataGridView1[hti.ColumnIndex, hti.RowIndex];
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(
drag_cell,
DragDropEffects.Move);
}
}
于 2012-07-16T18:40:04.640 に答える