@@ -27,99 +27,123 @@ To enable Inline editing in the grid, set its `EditMode` property to `inline`, t
27
27
<strong>Editing is cancelled for the first two records.</strong>
28
28
29
29
<TelerikGrid Data=@MyData EditMode="inline" Pageable="true">
30
- <TelerikGridEvents>
31
- <EventsManager OnUpdate="@UpdateHandler" OnEdit="@EditHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler" OnCancel="@CancelHandler"></EventsManager>
32
- </TelerikGridEvents>
33
- <TelerikGridToolBar>
34
- <TelerikGridCommandButton Command="Add" Icon="add">Add Employee</TelerikGridCommandButton>
35
- </TelerikGridToolBar>
36
- <TelerikGridColumns>
37
- <TelerikGridColumn Field=@nameof(SampleData.ID) Title="ID" Editable="false" />
38
- <TelerikGridColumn Field=@nameof(SampleData.Name) Title="Name" />
39
- <TelerikGridCommandColumn>
40
- <TelerikGridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</TelerikGridCommandButton>
41
- <TelerikGridCommandButton Command="Edit" Icon="edit">Edit</TelerikGridCommandButton>
42
- <TelerikGridCommandButton Command="Delete" Icon="delete">Delete</TelerikGridCommandButton>
43
- <TelerikGridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</TelerikGridCommandButton>
44
- </TelerikGridCommandColumn>
45
- </TelerikGridColumns>
30
+ <TelerikGridEvents>
31
+ <EventsManager OnUpdate="@UpdateHandler" OnEdit="@EditHandler" OnDelete="@DeleteHandler" OnCreate="@CreateHandler" OnCancel="@CancelHandler"></EventsManager>
32
+ </TelerikGridEvents>
33
+ <TelerikGridToolBar>
34
+ <TelerikGridCommandButton Command="Add" Icon="add">Add Employee</TelerikGridCommandButton>
35
+ </TelerikGridToolBar>
36
+ <TelerikGridColumns>
37
+ <TelerikGridColumn Field=@nameof(SampleData.ID) Title="ID" Editable="false" />
38
+ <TelerikGridColumn Field=@nameof(SampleData.Name) Title="Name" />
39
+ <TelerikGridCommandColumn>
40
+ <TelerikGridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</TelerikGridCommandButton>
41
+ <TelerikGridCommandButton Command="Edit" Icon="edit">Edit</TelerikGridCommandButton>
42
+ <TelerikGridCommandButton Command="Delete" Icon="delete">Delete</TelerikGridCommandButton>
43
+ <TelerikGridCommandButton Command="Cancel" Icon="cancel" ShowInEdit="true">Cancel</TelerikGridCommandButton>
44
+ </TelerikGridCommandColumn>
45
+ </TelerikGridColumns>
46
46
</TelerikGrid>
47
47
48
48
@functions {
49
- public void EditHandler(GridCommandEventArgs args)
50
- {
51
- SampleData item = (SampleData)args.Item;
52
-
53
- //prevent opening for edit based on condition
54
- if (item.ID < 3)
55
- {
56
- args.IsCancelled = true;//the general approach for cancelling an event
57
- }
58
-
59
- Console.WriteLine("Edit event is fired.");
60
- }
61
-
62
- public void UpdateHandler(GridCommandEventArgs args)
63
- {
64
- SampleData item = (SampleData)args.Item;
65
-
66
- //perform actual data source operations here
67
-
68
- //if you have a context added through an @inject statement, you could call its SaveChanges() method
69
- //myContext.SaveChanges();
70
-
71
- Console.WriteLine("Update event is fired.");
72
- }
73
-
74
- public void DeleteHandler(GridCommandEventArgs args)
75
- {
76
- SampleData item = (SampleData)args.Item;
77
-
78
- //perform actual data source operation here
79
-
80
- //if you have a context added through an @inject statement, you could call its SaveChanges() method
81
- //myContext.SaveChanges();
82
-
83
- Console.WriteLine("Delete event is fired.");
84
- }
85
-
86
-
87
- public void CreateHandler(GridCommandEventArgs args)
88
- {
89
- SampleData item = (SampleData)args.Item;
90
-
91
- //perform actual data source operation here
92
-
93
- Console.WriteLine("Add event is fired.");
94
- }
95
-
96
- public void CancelHandler(GridCommandEventArgs args)
97
- {
98
- Console.WriteLine("Cancel event is fired.");
99
-
100
- SampleData item = (SampleData)args.Item;
101
-
102
- //if necessary, perform actual data source operation here (like cancel changes on a context)
103
- //if you have a context added through an @inject statement, you could use something like this to abort changes
104
- //foreach (var entry in myContext.ChangeTracker.Entries().Where(entry => entry.State == EntityState.Modified))
105
- //{
106
- // entry.State = EntityState.Unchanged;
107
- //}
108
- }
109
-
110
- //in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
111
- public class SampleData
112
- {
113
- public int ID { get; set; }
114
- public string Name { get; set; }
115
- }
116
-
117
-
118
- public IEnumerable<SampleData> MyData = Enumerable.Range(1, 50).Select(x => new SampleData
119
- {
120
- ID = x,
121
- Name = "name " + x
122
- });
49
+ public void EditHandler(GridCommandEventArgs args)
50
+ {
51
+ SampleData item = (SampleData)args.Item;
52
+
53
+ //prevent opening for edit based on condition
54
+ if (item.ID < 3)
55
+ {
56
+ args.IsCancelled = true;//the general approach for cancelling an event
57
+ }
58
+
59
+ Console.WriteLine("Edit event is fired.");
60
+ }
61
+
62
+ public void UpdateHandler(GridCommandEventArgs args)
63
+ {
64
+ SampleData item = (SampleData)args.Item;
65
+
66
+ //perform actual data source operations here
67
+
68
+ //if you have a context added through an @inject statement, you could call its SaveChanges() method
69
+ //myContext.SaveChanges();
70
+
71
+ var matchingItem = MyData.FirstOrDefault(c => c.ID == item.ID);
72
+ if (matchingItem != null)
73
+ {
74
+ matchingItem.Name = item.Name;
75
+ }
76
+
77
+ Console.WriteLine("Update event is fired.");
78
+ }
79
+
80
+ public void DeleteHandler(GridCommandEventArgs args)
81
+ {
82
+ SampleData item = (SampleData)args.Item;
83
+
84
+ //perform actual data source operation here
85
+
86
+ //if you have a context added through an @inject statement, you could call its SaveChanges() method
87
+ //myContext.SaveChanges();
88
+
89
+ MyData.Remove(item);
90
+
91
+ Console.WriteLine("Delete event is fired.");
92
+ }
93
+
94
+ public void CreateHandler(GridCommandEventArgs args)
95
+ {
96
+ SampleData item = (SampleData)args.Item;
97
+
98
+ //perform actual data source operation here
99
+
100
+ //if you have a context added through an @inject statement, you could call its SaveChanges() method
101
+ //myContext.SaveChanges();
102
+
103
+ item.ID = MyData.Count;
104
+ MyData.Add(item);
105
+
106
+ Console.WriteLine("Create event is fired.");
107
+ }
108
+
109
+ public void CancelHandler(GridCommandEventArgs args)
110
+ {
111
+ SampleData item = (SampleData)args.Item;
112
+
113
+ //if necessary, perform actual data source operation here (like cancel changes on a context)
114
+
115
+ //if you have a context added through an @inject statement, you could use something like this to abort changes
116
+ //foreach (var entry in myContext.ChangeTracker.Entries().Where(entry => entry.State == EntityState.Modified))
117
+ //{
118
+ // entry.State = EntityState.Unchanged;
119
+ //}
120
+
121
+ Console.WriteLine("Cancel event is fired.");
122
+ }
123
+
124
+
125
+ //in a real case, keep the models in dedicated locations, this is just an easy to copy and see example
126
+ public class SampleData
127
+ {
128
+ public int ID { get; set; }
129
+ public string Name { get; set; }
130
+ }
131
+
132
+ public List<SampleData> MyData { get; set; }
133
+
134
+ protected override void OnInit()
135
+ {
136
+ MyData = new List<SampleData>();
137
+
138
+ for (int i = 0; i < 50; i++)
139
+ {
140
+ MyData.Add(new SampleData()
141
+ {
142
+ ID = i,
143
+ Name = "Name " + i.ToString()
144
+ });
145
+ }
146
+ }
123
147
}
124
148
````
125
149
0 commit comments