-
Notifications
You must be signed in to change notification settings - Fork 1
/
BindingSourceExtensions.cs
142 lines (104 loc) · 3.67 KB
/
BindingSourceExtensions.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System.Data;
using System.Text.Json;
using DapperLibrary1.Models;
namespace DapperLibrary1.Extensions;
public static class BindingSourceExtensions
{
/// <summary>
/// Move row up by one, tied to a specific class
/// </summary>
/// <param name="sender">BindingSource</param>
public static void MoveRowUp(this BindingSource sender)
{
if (!string.IsNullOrWhiteSpace(sender.Sort))
{
sender.Sort = "";
}
var newIndex = Convert.ToInt32((sender.Position == 0) ?
0 :
sender.Position - 1);
var source = (List<Person>)sender.DataSource;
Person? rowToMove = ((Person)sender.Current);
Person? newRow = ((Person)sender.Current);
source.RemoveAt(sender.Position);
source.Insert(newIndex, newRow);
sender.Position = newIndex;
sender.ResetBindings(false);
}
/// <summary>
/// Move row up by one
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="sender">BindingSource</param>
public static void MoveRowUp<T>(this BindingSource sender)
{
if (!string.IsNullOrWhiteSpace(sender.Sort))
{
sender.Sort = "";
}
var newIndex = Convert.ToInt32((sender.Position == 0) ?
0 :
sender.Position - 1);
var source = (List<T>)sender.DataSource;
T? rowToMove = ((T)sender.Current);
T? newRow = ((T)sender.Current);
source.RemoveAt(sender.Position);
source.Insert(newIndex, newRow);
sender.Position = newIndex;
sender.ResetBindings(false);
}
/// <summary>
/// Move row down by one, tied to a specific type
/// </summary>
/// <param name="sender"></param>
public static void MoveRowDown(this BindingSource sender)
{
if (!string.IsNullOrWhiteSpace(sender.Sort))
{
sender.Sort = "";
}
var upperLimit = sender.Count - 1;
var newIndex = Convert.ToInt32((sender.Position + 1 >= upperLimit) ?
upperLimit :
sender.Position + 1);
var source = (List<Person>)sender.DataSource;
Person? rowToMove = ((Person)sender.Current);
var newRow = (Person)rowToMove.Clone();
source.RemoveAt(sender.Position);
source.Insert(newIndex, newRow);
sender.Position = newIndex;
sender.ResetBindings(false);
}
public static void MoveRowDown<T>(this BindingSource sender)
{
if (!string.IsNullOrWhiteSpace(sender.Sort))
{
sender.Sort = "";
}
var upperLimit = sender.Count - 1;
var newIndex = Convert.ToInt32((sender.Position + 1 >= upperLimit) ?
upperLimit :
sender.Position + 1);
var source = (List<T>)sender.DataSource;
T? rowToMove = ((T)sender.Current);
T? newRow = ((T)sender.Current);
source.RemoveAt(sender.Position);
source.Insert(newIndex, newRow);
sender.Position = newIndex;
sender.ResetBindings(false);
}
public static void ToJsonFile(this BindingSource sender, string fileName = "people.json")
{
var serializerOptions = new JsonSerializerOptions { WriteIndented = true };
var list = (List<Person>)sender.DataSource;
File.WriteAllText(fileName, JsonSerializer.Serialize(list, serializerOptions));
}
public static DataTable DataTable(this BindingSource pBindingSource)
{
return (DataTable)pBindingSource.DataSource;
}
public static DataRow CurrentRow(this BindingSource pBindingSource)
{
return ((DataRowView)pBindingSource.Current).Row;
}
}