I have made a sample here Download sample project from here. When I reshuffle same column then reshuffled columns are not getting selected correctly. Here columns are getting selected randomly. please correct this code.
Asked
Active
Viewed 65 times
1 Answers
0
You could store the column on which the MouseDown
is raised and then select it in the MouseUp
event:
private DataGridViewColumn columnToMove;
public Form1()
{
InitializeComponent();
dataGridView1.Columns.AddRange(new DataGridViewColumn[]
{
new DataGridViewTextBoxColumn { Name = "AAA", SortMode = DataGridViewColumnSortMode.NotSortable },
new DataGridViewTextBoxColumn { Name = "BBB", SortMode = DataGridViewColumnSortMode.NotSortable },
new DataGridViewTextBoxColumn { Name = "CCC", SortMode = DataGridViewColumnSortMode.NotSortable }
});
dataGridView1.Rows.Add(2);
dataGridView1.AllowUserToOrderColumns = true;
dataGridView1.MouseDown += dataGridView1_MouseDown;
dataGridView1.MouseUp += dataGridView1_MouseUp;
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
var hti = dataGridView1.HitTest(e.X, e.Y);
if (hti.Type == DataGridViewHitTestType.ColumnHeader)
{
columnToMove = dataGridView1.Columns[hti.ColumnIndex];
dataGridView1.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
}
}
private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
{
if (columnToMove != null)
{
dataGridView1.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
columnToMove.Selected = true;
}
}
EDIT
Sample project: download

Dmitry
- 13,797
- 6
- 32
- 48
-
@AnkitSingh Yes, and there is no problem, because it will be set in the `MouseDown` event handler. Does this code work as you expected? – Dmitry Oct 19 '14 at 18:34
-
absolutely its working for me only one thing is remaining that when select a column without reshuffling it doesn't select the column. while reshuffle it is working. please suggest that one also. – Ankit Singh Oct 19 '14 at 18:50
-
@AnkitSingh I've updated the answer. Download link was also updated. If my answer was helpful, you should mark it as accepted. And do the same for my previous answer [here](http://stackoverflow.com/a/26449464/2878550). – Dmitry Oct 19 '14 at 19:04