private void DGV_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if ((e.Button == MouseButtons.Left) && DGV.IsColumnSortable(e.ColumnIndex)) { _LastSortInfo.SetSortOrderAndSaveCurrent(e.ColumnIndex); if (!_LastSortInfo.HasSorting) { _Model.Sort(SortHelper.CreateDefaultComparison()); } RestoreSortIfNeed(); _Settings.LastSortInfoJson = _LastSortInfo.ToJson(); _Settings.SaveNoThrow(); } else if (e.Button == MouseButtons.Right) { var displayIndex = DGV.Columns[e.ColumnIndex].DisplayIndex; var widthBefore = DGV.Columns.Cast <DataGridViewColumn>().Where(c => c.Visible && c.DisplayIndex < displayIndex).Sum(c => c.Width); var pt = new Point(widthBefore + e.X - DGV.HorizontalScrollingOffset, e.Y); _ColumnsContextMenu.Show(DGV, pt); } }