Skip to content

Commit a05997b

Browse files
committed
[Kanban 30] Changed visual appearance of the grid and some implementation details
1 parent eb4e861 commit a05997b

File tree

4 files changed

+173
-28
lines changed

4 files changed

+173
-28
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace optimalDb.WinForms.GridExtras
8+
{
9+
public static class AutoSizeFix
10+
{
11+
public static void AutoSizeColumns(DataGridView dataGridView)
12+
{
13+
foreach (DataGridViewColumn column in dataGridView.Columns)
14+
{
15+
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
16+
}
17+
18+
var spaltenBreiten = new List<int>();
19+
20+
foreach (DataGridViewColumn column in dataGridView.Columns)
21+
{
22+
spaltenBreiten.Add(column.Width);
23+
}
24+
25+
foreach (DataGridViewColumn column in dataGridView.Columns)
26+
{
27+
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
28+
}
29+
30+
for (var i = 0; i < spaltenBreiten.Count; i++)
31+
{
32+
var spaltenbreite = spaltenBreiten[i];
33+
dataGridView.Columns[i].Width = spaltenbreite;
34+
}
35+
}
36+
}
37+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using System.ComponentModel;
2+
3+
namespace optimalDb.WinForms.GridExtras
4+
{
5+
public class SortableBindingList<T> : BindingList<T>
6+
{
7+
private bool _isSortedValue;
8+
ListSortDirection _sortDirectionValue;
9+
PropertyDescriptor _sortPropertyValue;
10+
11+
public SortableBindingList()
12+
{
13+
}
14+
15+
public SortableBindingList(IList<T> list)
16+
{
17+
foreach (object o in list)
18+
{
19+
this.Add((T)o);
20+
}
21+
}
22+
23+
protected override void ApplySortCore(PropertyDescriptor prop,
24+
ListSortDirection direction)
25+
{
26+
Type interfaceType = prop.PropertyType.GetInterface("IComparable");
27+
28+
if (interfaceType == null && prop.PropertyType.IsValueType)
29+
{
30+
Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
31+
32+
if (underlyingType != null)
33+
{
34+
interfaceType = underlyingType.GetInterface("IComparable");
35+
}
36+
}
37+
38+
if (interfaceType != null)
39+
{
40+
_sortPropertyValue = prop;
41+
_sortDirectionValue = direction;
42+
43+
IEnumerable<T> query = base.Items;
44+
45+
if (direction == ListSortDirection.Ascending)
46+
{
47+
query = query.OrderBy(i => prop.GetValue(i));
48+
}
49+
else
50+
{
51+
query = query.OrderByDescending(i => prop.GetValue(i));
52+
}
53+
54+
int newIndex = 0;
55+
foreach (object item in query)
56+
{
57+
this.Items[newIndex] = (T)item;
58+
newIndex++;
59+
}
60+
61+
_isSortedValue = true;
62+
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
63+
}
64+
}
65+
66+
protected override PropertyDescriptor SortPropertyCore
67+
{
68+
get { return _sortPropertyValue; }
69+
}
70+
71+
protected override ListSortDirection SortDirectionCore
72+
{
73+
get { return _sortDirectionValue; }
74+
}
75+
76+
protected override bool SupportsSortingCore
77+
{
78+
get { return true; }
79+
}
80+
81+
protected override bool IsSortedCore
82+
{
83+
get { return _isSortedValue; }
84+
}
85+
}
86+
}

Source/optimalDb/optimalDb.WinForms/MainForm.Designer.cs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Source/optimalDb/optimalDb.WinForms/MainForm.cs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Newtonsoft.Json.Linq;
33
using optimalDb.BL;
44
using optimalDb.Infrastructure;
5+
using optimalDb.WinForms.GridExtras;
56
using System.ComponentModel;
67
using System.Data;
78
using System.Data.SqlClient;
@@ -62,35 +63,14 @@ private void testButton_Click(object sender, EventArgs e)
6263
));
6364
}
6465

65-
dataGridView1.DataSource = result;
66+
result = result.OrderByDescending(x => x.DurationInSeconds).ToList();
67+
dataGridView1.DataSource = new SortableBindingList<ViewPerformanceTestResult>(result);
68+
AutoSizeFix.AutoSizeColumns(dataGridView1);
6669

67-
int columnCount = dataGridView1.Columns.Count;
68-
decimal warning = 5;
69-
decimal error = 25;
70-
71-
for (int i = 1; (i - 1) < dataGridView1.Rows.Count; i++)
72-
{
73-
for (int j = 0; j < columnCount; j++)
74-
{
75-
if (dataGridView1.Rows[i - 1].Cells[j].Value.GetType() == typeof(decimal))
76-
{
77-
decimal value;
78-
if (Decimal.TryParse(dataGridView1.Rows[i - 1].Cells[j].Value.ToString(), out value) && value >= warning)
79-
{
80-
81-
dataGridView1.Rows[i - 1].Cells[j].Style.BackColor = Color.Yellow;
82-
83-
}
84-
else if (Decimal.TryParse(dataGridView1.Rows[i - 1].Cells[j].Value.ToString(), out value) && value >= error)
85-
{
86-
87-
dataGridView1.Rows[i - 1].Cells[j].Style.BackColor = Color.Red;
88-
89-
}
90-
91-
}
92-
}
93-
}
70+
ColorTheGrid();
71+
EnableSortingInTheGrid();
72+
// right align content
73+
dataGridView1.Columns["DurationInSeconds"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopRight;
9474
}
9575
catch (Exception ex)
9676
{
@@ -99,6 +79,19 @@ private void testButton_Click(object sender, EventArgs e)
9979

10080
}
10181

82+
private void EnableSortingInTheGrid()
83+
{
84+
foreach(DataGridViewColumn column in dataGridView1.Columns)
85+
{
86+
column.SortMode = DataGridViewColumnSortMode.Automatic;
87+
}
88+
}
89+
90+
private void ColorTheGrid()
91+
{
92+
93+
}
94+
10295
protected decimal GetDurationOfViewExecution(string viewName, string connectionString)
10396
{
10497
DateTime start = DateTime.Now;
@@ -248,5 +241,32 @@ private void exportButton_Click(object sender, EventArgs e)
248241
MessageBox.Show("No Test Results To Export !!!", "Info");
249242
}
250243
}
244+
245+
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
246+
{
247+
decimal warning = 2;
248+
decimal error = 25;
249+
250+
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
251+
252+
var durationInSeconds = decimal.Parse(row.Cells["DurationInSeconds"].Value.ToString());
253+
var color = Color.White;
254+
var selectionColor = Color.Blue;
255+
256+
if (durationInSeconds > warning)
257+
{
258+
color = Color.Yellow;
259+
selectionColor = Color.Orange;
260+
}
261+
262+
if (durationInSeconds > error)
263+
{
264+
color = Color.Red;
265+
selectionColor = Color.Coral;
266+
}
267+
268+
row.DefaultCellStyle.BackColor = color;
269+
row.DefaultCellStyle.SelectionBackColor = selectionColor;
251270
}
271+
}
252272
}

0 commit comments

Comments
 (0)