Skip to content

Commit dc43d69

Browse files
authored
feat: Usability improvements in the GUI application
* Sorts the table view on the Sequence column (when it is present) * The table dropdown is a DropDownList * Columns in the property grid are automatically sized * Columns in the table grid are automatically sized Thanks and kudos to @learn-more ! 🎉
2 parents a082afe + 81ab6d4 commit dc43d69

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ _Resharper*/
2626
# Misc
2727
src/TestResult.xml
2828
src/.build/nuget.exe
29-
src/msbuild.log
29+
src/msbuild.log
30+
src/.vs

src/LessMsi.Gui/IMainFormView.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//
2525
using System;
2626
using System.Collections.Generic;
27+
using System.ComponentModel;
2728
using LessMsi.Gui.Model;
2829

2930
namespace LessMsi.Gui
@@ -88,7 +89,13 @@ internal interface IMainFormView
8889
/// Adds a row to the MSI table grid.
8990
/// </summary>
9091
void SetTableViewGridDataSource(IEnumerable<object[]> values);
91-
void SetPropertyGridDataSource(MsiPropertyInfo[] props);
92+
93+
/// <summary>
94+
/// Sort the MSI Table grid by the specified column
95+
/// </summary>
96+
void TableViewSortBy(string columnName, ListSortDirection direction);
97+
98+
void SetPropertyGridDataSource(MsiPropertyInfo[] props);
9299
void AddPropertyGridColumn(string boundPropertyName, string headerText);
93100
/// <summary>
94101
/// Specifies the data source for the names of the streams in the streams selector.

src/LessMsi.Gui/LessMsi.Gui.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<DesignTimeSharedInput>True</DesignTimeSharedInput>
9595
<DependentUpon>Settings.settings</DependentUpon>
9696
</Compile>
97+
<Compile Include="Windows.Forms\DataGridViewExtensionMethods.cs" />
9798
<Compile Include="Windows.Forms\DisposableCursor.cs" />
9899
<Compile Include="Windows.Forms\ElevationButton.cs">
99100
<SubType>Component</SubType>

src/LessMsi.Gui/MainForm.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public void AddTableViewGridColumn(string headerText)
165165
{
166166
DataGridViewColumn col = new DataGridViewTextBoxColumn
167167
{
168+
Name = headerText,
168169
HeaderText = headerText,
169170
Resizable = DataGridViewTriState.True,
170171
SortMode = DataGridViewColumnSortMode.Automatic
@@ -184,13 +185,25 @@ public void SetTableViewGridDataSource(IEnumerable<object[]> values)
184185
{
185186
msiTableGrid.Rows.Add(row);
186187
}
188+
msiTableGrid.AutoResizeColumnsSafe();
189+
}
190+
191+
public void TableViewSortBy(string columnName, ListSortDirection direction)
192+
{
193+
msiTableGrid.Sort(msiTableGrid.Columns[columnName], direction);
194+
if (msiTableGrid.Rows.Count > 0)
195+
{
196+
// Prevent the view from scrolling down
197+
msiTableGrid.CurrentCell = msiTableGrid.Rows[0].Cells[0];
198+
}
187199
}
188200

189201
#region Property Grid Stuff
190202

191203
public void SetPropertyGridDataSource(MsiPropertyInfo[] props)
192204
{
193205
msiPropertyGrid.DataSource = props;
206+
msiPropertyGrid.AutoResizeColumnsSafe();
194207
}
195208

196209
public void AddPropertyGridColumn(string boundPropertyName, string headerText)
@@ -493,12 +506,12 @@ private void InitializeComponent()
493506
this.cboTable.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
494507
| System.Windows.Forms.AnchorStyles.Left)
495508
| System.Windows.Forms.AnchorStyles.Right)));
509+
this.cboTable.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
496510
this.cboTable.Enabled = false;
497511
this.cboTable.Location = new System.Drawing.Point(50, 5);
498512
this.cboTable.Name = "cboTable";
499513
this.cboTable.Size = new System.Drawing.Size(323, 23);
500514
this.cboTable.TabIndex = 8;
501-
this.cboTable.Text = "table";
502515
this.cboTable.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
503516
//
504517
// msiTableGrid

src/LessMsi.Gui/MainFormPresenter.cs

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//
2525
using System;
2626
using System.Collections.Generic;
27+
using System.ComponentModel;
2728
using System.Diagnostics;
2829
using System.IO;
2930
using System.Linq;
@@ -433,14 +434,24 @@ private void UpdateMSiTableGrid(Database msidb, string tableName)
433434
// NOTE: Deliberately not calling msidb.TableExists here as some System tables could not be read due to using it.
434435
string query = string.Concat("SELECT * FROM `", tableName, "`");
435436

437+
string sequenceName = string.Empty;
436438
using (var view = new ViewWrapper(msidb.OpenExecuteView(query)))
437439
{
438440
foreach (ColumnInfo col in view.Columns)
439441
{
440-
View.AddTableViewGridColumn(string.Concat(col.Name, " (", col.TypeID, ")"));
442+
string displayName = string.Concat(col.Name, " (", col.TypeID, ")");
443+
View.AddTableViewGridColumn(displayName);
444+
if (col.Name == "Sequence")
445+
{
446+
sequenceName = displayName;
447+
}
441448
}
442449
View.SetTableViewGridDataSource(view.Records);
443450
}
451+
if (!string.IsNullOrEmpty(sequenceName))
452+
{
453+
View.TableViewSortBy(sequenceName, ListSortDirection.Ascending);
454+
}
444455
Status();
445456
}
446457
catch (Exception eUnexpected)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Windows.Forms;
3+
4+
namespace LessMsi.Gui.Windows.Forms
5+
{
6+
public static class DataGridViewExtensionMethods
7+
{
8+
/// <summary>
9+
/// Adjusts the width of all columns to fit the contents of all cells.
10+
/// This function takes care of the edgecase where the grid is not created yet.
11+
/// (For example, when it is filled with data before the control is shown.)
12+
/// </summary>
13+
/// <param name="gridView">The target control.</param>
14+
public static void AutoResizeColumnsSafe(this DataGridView gridView)
15+
{
16+
if (gridView.IsHandleCreated)
17+
{
18+
gridView.AutoResizeColumns();
19+
}
20+
else
21+
{
22+
// If the handle is not created yet we cannot resize columns,
23+
// so delay the auto-resizing to when the control is created.
24+
gridView.HandleCreated += Grid_HandleCreated;
25+
}
26+
}
27+
28+
private static void Grid_HandleCreated(object sender, EventArgs e)
29+
{
30+
DataGridView dgv = sender as DataGridView;
31+
if (dgv != null)
32+
{
33+
dgv.HandleCreated -= Grid_HandleCreated;
34+
dgv.AutoResizeColumns();
35+
}
36+
}
37+
38+
}
39+
}

0 commit comments

Comments
 (0)