You can create two HitTestInfo objects, one in the MouseDown
and one in the MouseUp
.
IMO, you also should use the DataGridView.HitTestInfo
class, not DataGrid.HitTestInfo
and try to not call or name DataGridViews
DataGrids
, which is a similar but different Control from WPF
!
DataGridView.HitTestInfo myHitTestDown, myHitTestUp;
int visibleColumnDown, visibleColumnUp;
private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
{
myHitTestUp = dataGrid1.HitTest(e.X, e.Y);
visibleColumnUp = getVisibleColumn(dataGrid1, e.X);
}
private void dataGrid1_MouseDown(object sender, MouseEventArgs e)
{
myHitTestDown = dataGrid1.HitTest(e.X, e.Y);
visibleColumnDown = getVisibleColumn(dataGrid1, e.X);
}
Update: To find the visible index of a column after the columns have been reordered simply use:
dataGrid1.Columns[myHitTestUp.ColumnIndex].DisplayIndex;
Before I found that, I wrote this little helper function, which does the same:
int getVisibleColumn(DataGridView dgv, int x)
{
int cx = dgv.RowHeadersWidth;
int c = 0;
foreach (DataGridViewColumn col in dgv.Columns)
{
cx += col.Width; if ( cx >= x) return c; c++;
}
return -1;
}
To find out which Column was shuffled seems to be a bit harder. There is an event, which gets called for each column that was affected and it always gets called first for the one that was dragged along. Here is one way to do it:
Create to variables at class level:
List<DataGridViewColumn> shuffled = new List<DataGridViewColumn>();
DataGridViewColumn shuffledColumn = null;
Remember the first column:
private void dgvLoadTable_ColumnDisplayIndexChanged(
object sender, DataGridViewColumnEventArgs e)
{
if (shuffledColumn == null) shuffledColumn = e.Column;
}
Forget what happend before:
private void dgvLoadTable_MouseDown(object sender, MouseEventArgs e)
{
shuffledColumn = null;
}
Now you can use it. Selecting Columns is, however, not going well with shuffling them! If you do
shuffledColumn.Selected = true;
it will only be selected if the SelectionMode
is either FullColumnSelect
or ColumnHeaderSelect
- In either mode the shuffling will not work, I'm afraid..