Merge pull request #12 from jwestfall69/sort-fix

fix sorting on GameGrid
This commit is contained in:
gjefferyes
2015-01-26 09:44:28 -06:00
2 changed files with 98 additions and 3 deletions

View File

@@ -656,6 +656,7 @@
this.GameGrid.SelectionChanged += new System.EventHandler(this.GameGridSelectionChanged); this.GameGrid.SelectionChanged += new System.EventHandler(this.GameGridSelectionChanged);
this.GameGrid.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.GameGridMouseDoubleClick); this.GameGrid.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.GameGridMouseDoubleClick);
this.GameGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GameGrid_MouseUp); this.GameGrid.MouseUp += new System.Windows.Forms.MouseEventHandler(this.GameGrid_MouseUp);
this.GameGrid.ColumnHeaderMouseClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.GameGridColumnHeaderMouseClick);
// //
// Type // Type
// //
@@ -664,7 +665,7 @@
this.Type.Name = "Type"; this.Type.Name = "Type";
this.Type.ReadOnly = true; this.Type.ReadOnly = true;
this.Type.Resizable = System.Windows.Forms.DataGridViewTriState.True; this.Type.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.Type.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; this.Type.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.Type.Width = 40; this.Type.Width = 40;
// //
// CGame // CGame
@@ -672,6 +673,7 @@
this.CGame.HeaderText = "Game (Directory / Zip)"; this.CGame.HeaderText = "Game (Directory / Zip)";
this.CGame.Name = "CGame"; this.CGame.Name = "CGame";
this.CGame.ReadOnly = true; this.CGame.ReadOnly = true;
this.CGame.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Programmatic;
this.CGame.Width = 220; this.CGame.Width = 220;
// //
// CDescription // CDescription
@@ -679,6 +681,7 @@
this.CDescription.HeaderText = "Description"; this.CDescription.HeaderText = "Description";
this.CDescription.Name = "CDescription"; this.CDescription.Name = "CDescription";
this.CDescription.ReadOnly = true; this.CDescription.ReadOnly = true;
this.CDescription.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Programmatic;
this.CDescription.Width = 220; this.CDescription.Width = 220;
// //
// CCorrect // CCorrect
@@ -687,6 +690,7 @@
this.CCorrect.Name = "CCorrect"; this.CCorrect.Name = "CCorrect";
this.CCorrect.ReadOnly = true; this.CCorrect.ReadOnly = true;
this.CCorrect.Resizable = System.Windows.Forms.DataGridViewTriState.False; this.CCorrect.Resizable = System.Windows.Forms.DataGridViewTriState.False;
this.CCorrect.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.CCorrect.Width = 500; this.CCorrect.Width = 500;
// //
// RomGrid // RomGrid
@@ -756,7 +760,7 @@
this.CGot.Name = "CGot"; this.CGot.Name = "CGot";
this.CGot.ReadOnly = true; this.CGot.ReadOnly = true;
this.CGot.Resizable = System.Windows.Forms.DataGridViewTriState.True; this.CGot.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.CGot.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; this.CGot.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable;
this.CGot.Width = 65; this.CGot.Width = 65;
// //
// CRom // CRom

View File

@@ -38,6 +38,9 @@ namespace ROMVault2
private bool _updatingGameGrid; private bool _updatingGameGrid;
private int GameGridSortColumnIndex = 0;
private SortOrder GameGridSortOrder = SortOrder.Descending;
public static int[] GameGridColumnXPositions; public static int[] GameGridColumnXPositions;
private FrmKey _fk; private FrmKey _fk;
@@ -456,6 +459,11 @@ namespace ROMVault2
GameGrid.Rows.Clear(); GameGrid.Rows.Clear();
RomGrid.Rows.Clear(); RomGrid.Rows.Clear();
// clear sorting
GameGrid.Columns[GameGridSortColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.None;
GameGridSortColumnIndex = 0;
GameGridSortOrder = SortOrder.Descending;
if (cf == null) if (cf == null)
return; return;
@@ -600,6 +608,12 @@ namespace ROMVault2
GameGrid.Rows.Clear(); GameGrid.Rows.Clear();
RomGrid.Rows.Clear(); RomGrid.Rows.Clear();
// clear sorting
GameGrid.Columns[GameGridSortColumnIndex].HeaderCell.SortGlyphDirection = SortOrder.None;
GameGridSortColumnIndex = 0;
GameGridSortOrder = SortOrder.Descending;
ReportStatus tDirStat; ReportStatus tDirStat;
GameGridColumnXPositions = new int[(int)RepStatus.EndValue]; GameGridColumnXPositions = new int[(int)RepStatus.EndValue];
@@ -851,9 +865,86 @@ namespace ROMVault2
e.Value = bmp; e.Value = bmp;
} }
else else
Console.WriteLine("WARN: GameGrid_CellFormatting() unknown column: {0}", GameGrid.Columns[e.ColumnIndex].Name); Console.WriteLine("WARN: GameGrid_CellFormatting() unknown column: {0}", GameGrid.Columns[e.ColumnIndex].Name);
} }
private void GameGridColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
// only allow sort on CGame/CDescription
if (e.ColumnIndex != 1 && e.ColumnIndex != 2)
return;
DataGridViewColumn newColumn = GameGrid.Columns[e.ColumnIndex];
DataGridViewColumn oldColumn = GameGrid.Columns[GameGridSortColumnIndex];
if (newColumn == oldColumn)
{
if (GameGridSortOrder == SortOrder.Ascending)
GameGridSortOrder = SortOrder.Descending;
else
GameGridSortOrder = SortOrder.Ascending;
}
else
{
oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
GameGridSortOrder = SortOrder.Ascending;
}
GameGrid.Sort(new GameGridRowComparer(GameGridSortOrder, e.ColumnIndex));
newColumn.HeaderCell.SortGlyphDirection = GameGridSortOrder;
GameGridSortColumnIndex = e.ColumnIndex;
}
private class GameGridRowComparer : System.Collections.IComparer
{
private int sortMod = 1;
private int columnIndex;
public GameGridRowComparer(SortOrder sortOrder, int index)
{
columnIndex = index;
if (sortOrder == SortOrder.Descending)
sortMod = -1;
}
public int Compare(object a, object b)
{
DataGridViewRow aRow = (DataGridViewRow)a;
DataGridViewRow bRow = (DataGridViewRow)b;
RvDir aRvDir = (ROMVault2.RvDB.RvDir)aRow.Tag;
RvDir bRvDir = (ROMVault2.RvDB.RvDir)bRow.Tag;
int result = 0;
switch (columnIndex)
{
case 1: // CGame
result = System.String.Compare(aRvDir.Name, bRvDir.Name);
break;
case 2: // CDescription
String aDes = "";
String bDes = "";
if (aRvDir.Game != null)
aDes = aRvDir.Game.GetData(RvGame.GameData.Description);
if (bRvDir.Game != null)
bDes = bRvDir.Game.GetData(RvGame.GameData.Description);
result = System.String.Compare(aDes, bDes);
// if desciptions match, fall through to sorting by name
if (result == 0)
result = System.String.Compare(aRvDir.Name, bRvDir.Name);
break;
default:
Console.WriteLine("WARN: GameGridRowComparer::Compare() Invalid columnIndex: {0}", columnIndex);
break;
}
return sortMod * result;
}
}
#endregion #endregion
#region "Rom Grid Code" #region "Rom Grid Code"