|
3 | 3 | [](https://supportcenter.devexpress.com/ticket/details/E3076)
|
4 | 4 | [](https://docs.devexpress.com/GeneralInformation/403183)
|
5 | 5 | <!-- default badges end -->
|
6 |
| -<!-- default file list --> |
7 |
| -*Files to look at*: |
8 |
| - |
9 |
| -* [Default.aspx](./CS/WebSite/Default.aspx) (VB: [Default.aspx](./VB/WebSite/Default.aspx)) |
10 |
| -* [Default.aspx.cs](./CS/WebSite/Default.aspx.cs) (VB: [Default.aspx.vb](./VB/WebSite/Default.aspx.vb)) |
11 |
| -<!-- default file list end --> |
12 |
| -# How to delete the ASPxGridView's selected rows |
| 6 | +# Grid View for ASP.NET Web Forms - How to Delete Selected Rows in a Grid |
13 | 7 | <!-- run online -->
|
14 | 8 | **[[Run Online]](https://codecentral.devexpress.com/e3076/)**
|
15 | 9 | <!-- run online end -->
|
16 | 10 |
|
17 | 11 |
|
18 |
| -<p>The example illustrates how to delete selected rows of the ASPxGridView bound with an in-memory DataSource.</p><p><strong>See Also:<br /> |
19 |
| -</strong><a href="https://www.devexpress.com/Support/Center/p/E2636">How to move selected rows from the ASPxGridView into another ASPxGridView</a><strong><br /> |
20 |
| -</strong><a href="https://www.devexpress.com/Support/Center/p/E257">Editing an in-memory dataset</a></p> |
| 12 | +The example shows how to delete the selected rows of an [ASPxGridView](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView) that is bound to an in-memory DataSource. |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +Use the client-side [PerformCallback](https://docs.devexpress.com/AspNet/js-ASPxClientGridView.PerformCallback(args)?p=netframework) method to send custom callbacks to the server when a user clicks the **Delete** button. |
| 17 | + |
| 18 | +```aspx |
| 19 | +function OnClickButtonDel(s, e) { |
| 20 | + grid.PerformCallback('Delete'); |
| 21 | +} |
| 22 | +... |
| 23 | +<dx:ASPxButton ID="buttonDel" AutoPostBack="false" |
| 24 | + runat="server" Text="Delete"> |
| 25 | + <ClientSideEvents Click="OnClickButtonDel"/> |
| 26 | +</dx:ASPxButton> |
| 27 | +``` |
| 28 | + |
| 29 | +On the server, the `PerformCallback` method raises the [CustomCallback](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView.CustomCallback?p=netframework) event. |
| 30 | + |
| 31 | +In the `CustomCallback` event handler, call the [GetSelectedFieldValues](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridBase.GetSelectedFieldValues(System.String--)?p=netframework) method to obtain the selected rows. Then, call the [Remove](https://docs.microsoft.com/en-us/dotnet/api/system.data.datarowcollection.remove?view=net-6.0) method for each selected row. |
| 32 | + |
| 33 | +```cs |
| 34 | +protected void gridView_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e) { |
| 35 | + if(e.Parameters == "Delete") { |
| 36 | + table = GetTable(); |
| 37 | + List<Object> selectItems = grid.GetSelectedFieldValues("ID"); |
| 38 | + foreach(object selectItemId in selectItems) { |
| 39 | + table.Rows.Remove(table.Rows.Find(selectItemId)); |
| 40 | + } |
| 41 | + grid.DataBind(); |
| 42 | + grid.Selection.UnselectAll(); |
| 43 | + } |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## Files to Look At |
| 48 | + |
| 49 | +* [Default.aspx](./CS/WebSite/Default.aspx) (VB: [Default.aspx](./VB/WebSite/Default.aspx)) |
| 50 | +* [Default.aspx.cs](./CS/WebSite/Default.aspx.cs#L45-L55) (VB: [Default.aspx.vb](./VB/WebSite/Default.aspx.vb#L60-L70)) |
| 51 | + |
| 52 | +## Documentation |
21 | 53 |
|
22 |
| -<br/> |
| 54 | +* [ASPxGridView](https://docs.devexpress.com/AspNet/DevExpress.Web.ASPxGridView) |
| 55 | +* [Grid View - Add and Edit Records](https://docs.devexpress.com/AspNet/401098/components/grid-view/concepts/edit-data/add-and-edit-records) |
| 56 | +* [Grid View - Delete Records](https://docs.devexpress.com/AspNet/401080/components/grid-view/concepts/edit-data/delete-records) |
| 57 | +* [Grid View - Examples](https://docs.devexpress.com/AspNet/3768/components/grid-view/examples) |
23 | 58 |
|
| 59 | +## More Examples |
24 | 60 |
|
| 61 | +* [How to Move Selected Rows From the ASPxGridView Into Another ASPxGridView](https://github.com/DevExpress-Examples/how-to-move-selected-rows-from-the-aspxgridview-into-another-aspxgridview-e2636) |
| 62 | +* [How to Edit an In-Memory Dataset](https://github.com/DevExpress-Examples/aspxgridview-editing-an-in-memory-dataset-e257) |
0 commit comments