-
Notifications
You must be signed in to change notification settings - Fork 137
/
QueryParameters.cs
67 lines (59 loc) · 2.07 KB
/
QueryParameters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using SolarWinds.InformationService.Contract2;
using SwqlStudio.Utils;
using WeifenLuo.WinFormsUI.Docking;
namespace SwqlStudio
{
public partial class QueryParameters : DockContent
{
private bool allowSetParameters;
public bool AllowSetParameters
{
get => allowSetParameters;
set
{
allowSetParameters = value;
parametersGrid.AllowUserToAddRows = !allowSetParameters;
var keyColumn = parametersGrid.Columns[nameof(QueryVariable.Key)];
keyColumn.ReadOnly = allowSetParameters;
var cellColor = !allowSetParameters ? Color.White : Color.LightGray;
keyColumn.DefaultCellStyle.BackColor = cellColor;
}
}
public QueryParameters()
{
DpiHelper.FixFont(this);
InitializeComponent();
DpiHelper.FixRowHeight(parametersGrid);
parametersGrid.DataSource = new BindingList<QueryVariable>();
AllowSetParameters = true;
}
public PropertyBag Parameters
{
get
{
var bag = new PropertyBag();
foreach (QueryVariable pair in GetBoundQueryVariables())
bag[pair.Key] = pair.Value;
return bag;
}
set
{
if (!AllowSetParameters)
return;
var ordered = value.Select(pair => new QueryVariable(pair.Key, pair.Value?.ToString()))
.OrderBy(p => p.Key)
.ToList();
parametersGrid.DataSource = new BindingList<QueryVariable>(ordered) { AllowNew = true };
}
}
private IEnumerable<QueryVariable> GetBoundQueryVariables()
{
return ((BindingList<QueryVariable>)parametersGrid.DataSource)
.Where(v => v.Key != null);
}
}
}