This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
/
Form1.cs
210 lines (195 loc) · 6.86 KB
/
Form1.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Developer Express Code Central Example:
// How to edit GridView data in a popup form
//
// This example demonstrates how to switch a GridView to read-only mode and
// implement the Create, Update, Delete, and Insert operations in a popup form. In
// this example, you can also edit data after double-clicking a row.
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E4518
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
namespace GridSample
{
public partial class Form1 : XtraForm
{
public Form1()
{
InitializeComponent();
InitGrid();
gridView1.OptionsBehavior.Editable = false;
gridView1.DoubleClick += new EventHandler(gridView1_DoubleClick);
}
void gridView1_DoubleClick(object sender, EventArgs e)
{
personToEdit = GetCurrentPerson();
EditPerson(personToEdit, "EditPerson", CloseEditPersonHandler);
}
private EditForm f1;
private Person personToEdit;
BindingList<Person> personList = new BindingList<Person>();
void InitGrid()
{
personList.Add(new Person("John", "Smith"));
personList.Add(new Person("Gabriel", "Smith"));
personList.Add(new Person("Ashley", "Smith", "some comment"));
personList.Add(new Person("Adrian", "Smith", "some comment"));
personList.Add(new Person("Gabriella", "Smith", "some comment"));
gridControl.DataSource = personList;
}
private void button1_Click(object sender, EventArgs e)
{
personToEdit = new Person();
EditPerson(personToEdit, "NewPerson", CloseNewPersonHandler);
}
private void button2_Click(object sender, EventArgs e)
{
if (gridView1.FocusedRowHandle < 0)
{
MessageBox.Show("You didn' select a person to edit");
return;
}
personToEdit = GetCurrentPerson();
EditPerson(personToEdit, "EditPerson", CloseEditPersonHandler);
}
private Person GetCurrentPerson()
{
return gridView1.GetRow(gridView1.FocusedRowHandle) as Person;
}
private void EditPerson(Person person, string windowTitle, FormClosingEventHandler closedDelegate)
{
f1 = new EditForm(person,false) { Text = windowTitle };
f1.FormClosing += closedDelegate;
f1.ShowDialog();
}
private void CloseEditPersonHandler(object sender, EventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
try
{
personList.ResetBindings();
}
catch (Exception ex)
{
HandleExcepton(ex);
}
}
personToEdit = null;
}
private void CloseNewPersonHandler(object sender, FormClosingEventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
try
{
personList.Add(personToEdit);
}
catch (Exception ex)
{
HandleExcepton(ex);
}
SetNewFocus();
}
personToEdit = null;
}
private void SetNewFocus()
{
for (int i = 0; i < gridView1.DataRowCount; i++)
{
if (personToEdit.FirstName == gridView1.GetRowCellValue(i, gridView1.Columns["FirstName"]).ToString())
{
gridView1.FocusedRowHandle = i;
break;
}
}
}
private void HandleExcepton(Exception ex)
{
MessageBox.Show(ex.Message);
}
private void button3_Click(object sender, EventArgs e)
{
DeletePerson(gridView1.FocusedRowHandle);
}
private void DeletePerson(int focusedRowHandle)
{
if (focusedRowHandle < 0)
{
MessageBox.Show("You didn't select a person to delete");
return;
}
if (MessageBox.Show("Do you really want to delete the selected person?", "Delete Person", MessageBoxButtons.OKCancel) != DialogResult.OK)
{
return;
}
try
{
personToEdit = GetCurrentPerson();
personList.Remove(personToEdit);
}
catch (Exception ex)
{
HandleExcepton(ex);
}
gridView1.FocusedRowHandle = focusedRowHandle;
personToEdit = null;
}
private void simpleButton4_Click(object sender, EventArgs e)
{
personToEdit = new Person();
InsertPerson(personToEdit, "InsertForm", InsertPersonHandler);
}
private void InsertPerson(Person person, string windowTitle, FormClosingEventHandler closedDelegate)
{
f1 = new EditForm(person, true) { Text = windowTitle };
f1.FormClosing += closedDelegate;
f1.ShowDialog();
}
private void InsertPersonHandler(object sender, FormClosingEventArgs e)
{
EditForm form = sender as EditForm;
if (form != null)
{
if (form.DialogResult == DialogResult.OK)
{
try
{
if (!String.IsNullOrWhiteSpace(form.NewPosition))
{
int pos = Convert.ToInt32(form.NewPosition);
if (pos > personList.Count - 1)
{
personList.Add(personToEdit);
}
else
{
personList.Insert(pos, personToEdit);
}
}
else
{
MessageBox.Show("Incorrect position value");
}
}
catch (Exception ex)
{
HandleExcepton(ex);
}
}
SetNewFocus();
}
personToEdit = null;
}
}
}