Skip to content

Commit

Permalink
feat: Usability improvements in the GUI application
Browse files Browse the repository at this point in the history
* 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 ! 🎉
  • Loading branch information
activescott authored Jan 8, 2021
2 parents a082afe + 81ab6d4 commit dc43d69
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ _Resharper*/
# Misc
src/TestResult.xml
src/.build/nuget.exe
src/msbuild.log
src/msbuild.log
src/.vs
9 changes: 8 additions & 1 deletion src/LessMsi.Gui/IMainFormView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using LessMsi.Gui.Model;

namespace LessMsi.Gui
Expand Down Expand Up @@ -88,7 +89,13 @@ internal interface IMainFormView
/// Adds a row to the MSI table grid.
/// </summary>
void SetTableViewGridDataSource(IEnumerable<object[]> values);
void SetPropertyGridDataSource(MsiPropertyInfo[] props);

/// <summary>
/// Sort the MSI Table grid by the specified column
/// </summary>
void TableViewSortBy(string columnName, ListSortDirection direction);

void SetPropertyGridDataSource(MsiPropertyInfo[] props);
void AddPropertyGridColumn(string boundPropertyName, string headerText);
/// <summary>
/// Specifies the data source for the names of the streams in the streams selector.
Expand Down
1 change: 1 addition & 0 deletions src/LessMsi.Gui/LessMsi.Gui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<DependentUpon>Settings.settings</DependentUpon>
</Compile>
<Compile Include="Windows.Forms\DataGridViewExtensionMethods.cs" />
<Compile Include="Windows.Forms\DisposableCursor.cs" />
<Compile Include="Windows.Forms\ElevationButton.cs">
<SubType>Component</SubType>
Expand Down
15 changes: 14 additions & 1 deletion src/LessMsi.Gui/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ public void AddTableViewGridColumn(string headerText)
{
DataGridViewColumn col = new DataGridViewTextBoxColumn
{
Name = headerText,
HeaderText = headerText,
Resizable = DataGridViewTriState.True,
SortMode = DataGridViewColumnSortMode.Automatic
Expand All @@ -184,13 +185,25 @@ public void SetTableViewGridDataSource(IEnumerable<object[]> values)
{
msiTableGrid.Rows.Add(row);
}
msiTableGrid.AutoResizeColumnsSafe();
}

public void TableViewSortBy(string columnName, ListSortDirection direction)
{
msiTableGrid.Sort(msiTableGrid.Columns[columnName], direction);
if (msiTableGrid.Rows.Count > 0)
{
// Prevent the view from scrolling down
msiTableGrid.CurrentCell = msiTableGrid.Rows[0].Cells[0];
}
}

#region Property Grid Stuff

public void SetPropertyGridDataSource(MsiPropertyInfo[] props)
{
msiPropertyGrid.DataSource = props;
msiPropertyGrid.AutoResizeColumnsSafe();
}

public void AddPropertyGridColumn(string boundPropertyName, string headerText)
Expand Down Expand Up @@ -493,12 +506,12 @@ private void InitializeComponent()
this.cboTable.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.cboTable.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cboTable.Enabled = false;
this.cboTable.Location = new System.Drawing.Point(50, 5);
this.cboTable.Name = "cboTable";
this.cboTable.Size = new System.Drawing.Size(323, 23);
this.cboTable.TabIndex = 8;
this.cboTable.Text = "table";
this.cboTable.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// msiTableGrid
Expand Down
13 changes: 12 additions & 1 deletion src/LessMsi.Gui/MainFormPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -433,14 +434,24 @@ private void UpdateMSiTableGrid(Database msidb, string tableName)
// NOTE: Deliberately not calling msidb.TableExists here as some System tables could not be read due to using it.
string query = string.Concat("SELECT * FROM `", tableName, "`");

string sequenceName = string.Empty;
using (var view = new ViewWrapper(msidb.OpenExecuteView(query)))
{
foreach (ColumnInfo col in view.Columns)
{
View.AddTableViewGridColumn(string.Concat(col.Name, " (", col.TypeID, ")"));
string displayName = string.Concat(col.Name, " (", col.TypeID, ")");
View.AddTableViewGridColumn(displayName);
if (col.Name == "Sequence")
{
sequenceName = displayName;
}
}
View.SetTableViewGridDataSource(view.Records);
}
if (!string.IsNullOrEmpty(sequenceName))
{
View.TableViewSortBy(sequenceName, ListSortDirection.Ascending);
}
Status();
}
catch (Exception eUnexpected)
Expand Down
39 changes: 39 additions & 0 deletions src/LessMsi.Gui/Windows.Forms/DataGridViewExtensionMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Windows.Forms;

namespace LessMsi.Gui.Windows.Forms
{
public static class DataGridViewExtensionMethods
{
/// <summary>
/// Adjusts the width of all columns to fit the contents of all cells.
/// This function takes care of the edgecase where the grid is not created yet.
/// (For example, when it is filled with data before the control is shown.)
/// </summary>
/// <param name="gridView">The target control.</param>
public static void AutoResizeColumnsSafe(this DataGridView gridView)
{
if (gridView.IsHandleCreated)
{
gridView.AutoResizeColumns();
}
else
{
// If the handle is not created yet we cannot resize columns,
// so delay the auto-resizing to when the control is created.
gridView.HandleCreated += Grid_HandleCreated;
}
}

private static void Grid_HandleCreated(object sender, EventArgs e)
{
DataGridView dgv = sender as DataGridView;
if (dgv != null)
{
dgv.HandleCreated -= Grid_HandleCreated;
dgv.AutoResizeColumns();
}
}

}
}

0 comments on commit dc43d69

Please sign in to comment.