This repository has been archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
198 lines (185 loc) · 7 KB
/
Main.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
// Developer Express Code Central Example:
// How to implement simple copy/paste operations in the GridView from a toolbar menu
//
// This example illustrates how to implement simple copy/paste/delete operations in
// the GridView.
// You can find more in the article
// http://www.devexpress.com/scid=A1266
//
// You can find sample updates and versions for different programming languages here:
// http://www.devexpress.com/example=E4528
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
namespace DXGridCRUDoperations
{
public partial class Main : XtraForm
{
public Main()
{
InitializeComponent();
InitGrid();
}
BindingList<Person> gridDataList = new BindingList<Person>();
void InitGrid()
{
gridDataList.Add(new Person("John", "Smith"));
gridDataList.Add(new Person("Gabriel", "Smith"));
gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
gridControl.DataSource = gridDataList;
gridView1.OptionsClipboard.PasteMode = DevExpress.Export.PasteMode.Update;
gridView1.InitNewRow += new DevExpress.XtraGrid.Views.Grid.InitNewRowEventHandler(gridView1_InitNewRow);
}
void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
{
GridView view = sender as GridView;
if (view !=null && personToEdit != null)
{
view.SetRowCellValue(e.RowHandle, view.Columns["FirstName"],personToEdit.FirstName);
view.SetRowCellValue(e.RowHandle, view.Columns["SecondName"], personToEdit.SecondName);
view.SetRowCellValue(e.RowHandle, view.Columns["Info"], personToEdit.Info);
}
}
private EditForm f1;
private Person personToEdit;
BindingList<Person> personList = new BindingList<Person>();
private void iAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
personToEdit = new Person();
EditPerson(personToEdit, "NewPerson", CloseNewPersonHandler);
}
private void EditPerson(Person person, string windowTitle, FormClosingEventHandler closedDelegate)
{
f1 = new EditForm(person, false) { Text = windowTitle };
f1.FormClosing += closedDelegate;
f1.ShowDialog();
}
private void CloseNewPersonHandler(object sender, FormClosingEventArgs e)
{
if (((EditForm)sender).DialogResult == DialogResult.OK)
{
try
{
gridView1.AddNewRow();
}
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 iCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
gridView1.CopyToClipboard();
}
private void iPaste_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
gridView1.PasteFromClipboard();
}
private void iInsert_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs 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;
}
private void iDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
DeletePerson(gridView1.FocusedRowHandle);
}
public Person GetCurrentPerson()
{
return gridView1.GetRow(gridView1.FocusedRowHandle) as Person;
}
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
{
gridView1.DeleteRow(gridView1.FocusedRowHandle);
}
catch (Exception ex)
{
HandleExcepton(ex);
}
gridView1.FocusedRowHandle = focusedRowHandle;
personToEdit = null;
}
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (gridView1.IsMultiSelect && gridView1.SelectedRowsCount > 0)
{
gridView1.DeleteSelectedRows();
}
}
}
}