반응형
바인딩이 되어있는 DataGridView에서 마우스 드래그 이벤트를 통해서 컬럼 순서(위치)를 변경할 수 있는 방법
Row에 해당하는 내용은 구글링을 해도 많이 나오지만, Column에 해당하는 내용은 거의 없었다
응용해서 코드를 수정해본 내용이다 .
private Rectangle dragBoxFromMouseDown;
private int columnIndexFromMouseDown;
private int columnIndexOfItemUnderMouseToDrop;
private void metroGridWaferReview_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
DragDropEffects dropEffect = metroGridWaferReview.DoDragDrop(metroGridWaferReview.Columns[columnIndexFromMouseDown], DragDropEffects.Move);
}
}
}
private void metroGridWaferReview_MouseDown(object sender, MouseEventArgs e)
{
columnIndexFromMouseDown = metroGridWaferReview.HitTest(e.X, e.Y).ColumnIndex;
if (columnIndexFromMouseDown != -1)
{
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 metroGridWaferReview_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = metroGridWaferReview.PointToClient(new Point(e.X, e.Y));
columnIndexOfItemUnderMouseToDrop = metroGridWaferReview.HitTest(clientPoint.X, clientPoint.Y).ColumnIndex;
string down = metroGridWaferReview.Columns[columnIndexFromMouseDown].Name.ToString();
string drop = metroGridWaferReview.Columns[columnIndexOfItemUnderMouseToDrop].Name.ToString();
int down_displayindex = metroGridWaferReview.Columns[down].DisplayIndex;
int drop_displayindex = metroGridWaferReview.Columns[drop].DisplayIndex;
List<string> list_columnname = new List<string>();
if (e.Effect == DragDropEffects.Move)
{
for(int i=0; i < metroGridWaferReview.Columns.Count; i++)
{
if (down_displayindex < metroGridWaferReview.Columns[i].DisplayIndex && metroGridWaferReview.Columns[i].DisplayIndex <= drop_displayindex)
{
list_columnname.Add(metroGridWaferReview.Columns[i].Name);
}
}
for (int i = 0; i < list_columnname.Count; i++)
metroGridWaferReview.Columns[list_columnname[i]].DisplayIndex -= 1;
metroGridWaferReview.Columns[down].DisplayIndex = drop_displayindex;
}
}
private void metroGridWaferReview_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
https://icodebroker.tistory.com/3516
반응형
'Language > C#' 카테고리의 다른 글
[Error] System.Windows.Forms.DataGridViewCell.Value.get returned null (0) | 2022.07.05 |
---|---|
[리스트로 바인딩 된 DataGridview] (0) | 2022.07.05 |
[C#] control invalidate, refresh, update 차이 (0) | 2022.06.30 |
[int / int] 캐스팅 (0) | 2022.06.28 |
[Chart 마우스 드래그 이벤트] (0) | 2022.06.13 |