I got a Datagridview that is empty. The user is able to drag and drop values from a list in to the Datagridview copying the text.. The user is also able to drag and drop values within the Datagrid view moving the text.
But what i also want is for the Rows to drag and drop able (changing the order they appear in).
I manged to do both individually by using the code provided from these 2 answers:
https://stackoverflow.com/a/21133200/10086705 (for cell to cell) How could I Drag and Drop DataGridView Rows under each other? (For dragging rows).
The issue is they both use the same events, my current solution is using a check box to see which one to use. (Checked for row, unchecked for cells) Al tough this works i don't believe this is the most efficient/user friendly way.
This is the code for the dragging of the cells. (Don't mind the try catches they are a temp solution.)
private Rectangle dragBoxFromMouseDown;
private object valueFromMouseDown;
private DataGridViewCell origin;
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
try
{
DragDropEffects dropEffect = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy);
}
catch{}
}
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
origin = sender as DataGridViewCell;
var hittestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
{
valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex].Value;
if (valueFromMouseDown != null)
{
origin = dataGridView1.Rows[hittestInfo.RowIndex].Cells[hittestInfo.ColumnIndex] as DataGridViewCell;
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
}
else
{
dragBoxFromMouseDown = Rectangle.Empty;
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
if (e.Effect == DragDropEffects.Copy)
{
string cellvalue = e.Data.GetData(typeof(string)) as string;
var hittest = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
if (hittest.ColumnIndex != -1 && hittest.RowIndex != -1)
{
try
{
if (dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value.ToString() != "")
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to replace this value?", "!", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
try{origin.Value = "";}catch{}
}
else if (dialogResult == DialogResult.No){}
}
}
catch
{
dataGridView1[hittest.ColumnIndex, hittest.RowIndex].Value = cellvalue;
try{origin.Value = "";}catch{}
}
}
}
}
What i was hoping for is the possibility of an IF statement that checks if a row cell has been selected or if the RowHeader has been selected. In case of the cell it should only move the text from one cell to a other while if the RowHeader is selected it should move the row to a new location (without overwriting any existing row)