Skip to content

Commit 3f7711b

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article grid-cancel-filtering-when-in-edit-mode (#597)
Co-authored-by: KB Bot <[email protected]>
1 parent 6a21e41 commit 3f7711b

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: Cancel filtering when in Edit Mode
3+
description: Learn how to automatically exit the edit mode of a row in RadGrid for ASP.NET AJAX when the data source is changed, such as after filtering.
4+
type: how-to
5+
page_title: Cancel filtering when in Edit Mode
6+
slug: grid-cancel-filtering-when-in-edit-mode
7+
tags: radgrid, asp.net ajax, edit mode, datasource, filtering, dropdownlist, combobox
8+
res_type: kb
9+
ticketid: 1664283
10+
---
11+
12+
## Environment
13+
14+
| Product | Version |
15+
| --- | --- |
16+
| RadGrid for ASP.NET AJAX | All |
17+
18+
## Description
19+
20+
When using RadGrid to display and edit data, it is possible to enter an edit mode for a specific row. If the data source of the grid changes while a row is in edit mode, the same row position might enter edit mode with different data. This behavior could lead to inconsistencies, especially when filtering data through controls like RadDropDownList or RadComboBox. This article demonstrates how to ensure the grid exits edit mode when the data source changes due to filtering.
21+
22+
This KB article also answers the following questions:
23+
24+
- How do I disable RadGrid row editing on data source refresh?
25+
- How can I ensure a row exits edit mode when changing the filter?
26+
- What approach should I take to prevent editing a different row after data refresh in RadGrid?
27+
28+
## Solution
29+
30+
To manage the edit mode of RadGrid rows effectively when the data source changes, such as after applying a filter, follow these steps:
31+
32+
1. Utilize the `OnClientItemSelected` event of the DropDownList or ComboBox used for filtering.
33+
2. In the event handler, check if the grid is currently in edit mode by examining the length of the `editItems` collection.
34+
3. If the grid is in edit mode, prevent the filter operation or, alternatively, exit edit mode before proceeding with the filter.
35+
36+
### Example Implementation
37+
38+
Here's an example of implementing the above solution in a RadGrid with a RadDropDownList for filtering:
39+
40+
````ASP.NET
41+
<telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" Width="200px" AllowFilteringByColumn="true" OnNeedDataSource="RadGrid1_NeedDataSource">
42+
<MasterTableView AutoGenerateColumns="False">
43+
<Columns>
44+
<telerik:GridEditCommandColumn />
45+
<telerik:GridBoundColumn DataField="ShipName" AutoPostBackOnFilter="false"
46+
FilterControlAltText="Filter ShipName column" HeaderText="ShipName"
47+
SortExpression="ShipName" UniqueName="ShipName">
48+
<FilterTemplate>
49+
<telerik:RadDropDownList ID="RadDropDownList1" runat="server" RenderMode="Lightweight" OnClientItemSelected="onClientItemSelected">
50+
<Items>
51+
<telerik:DropDownListItem Text="ShipName1" />
52+
<telerik:DropDownListItem Text="ShipName2" />
53+
</Items>
54+
</telerik:RadDropDownList>
55+
</FilterTemplate>
56+
</telerik:GridBoundColumn>
57+
</Columns>
58+
</MasterTableView>
59+
</telerik:RadGrid>
60+
````
61+
62+
````JavaScirpt
63+
function onClientItemSelected(sender, args) {
64+
var masterTable = $find("<%= RadGrid1.ClientID %>").get_masterTableView();
65+
var isGridInEditMode = masterTable.get_editItems().length > 0;
66+
67+
if (!isGridInEditMode) {
68+
var selectedText = args.get_item().get_text();
69+
var filterFunction = Telerik.Web.UI.GridFilterFunction.EqualTo;
70+
71+
masterTable.filter("ShipName", selectedText, filterFunction, true);
72+
} else {
73+
alert("Grid is in edit mode, you can't filter!");
74+
}
75+
}
76+
````
77+
78+
### Server-Side Data Source Method
79+
80+
````C#
81+
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
82+
{
83+
(sender as RadGrid).DataSource = OrdersTable();
84+
}
85+
86+
private DataTable OrdersTable()
87+
{
88+
DataTable dt = new DataTable();
89+
dt.Columns.Add(new DataColumn("ShipName", typeof(string)));
90+
91+
for (int i = 0; i < 6; i++)
92+
{
93+
int index = i + 1;
94+
DataRow row = dt.NewRow();
95+
row["ShipName"] = index % 2 == 0 ? "ShipName" + 1 : "ShipName" + 2;
96+
dt.Rows.Add(row);
97+
}
98+
99+
return dt;
100+
}
101+
````
102+
103+
This approach ensures that the RadGrid exits edit mode when the data source changes, preventing the editing of a different row with potentially different data.
104+
105+
## See Also
106+
107+
- [RadGrid Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/grid/overview)
108+
- [RadDropDownList Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/overview)
109+
- [RadComboBox Overview](https://docs.telerik.com/devtools/aspnet-ajax/controls/combobox/overview)
110+
- [OnClientItemSelected](https://docs.telerik.com/devtools/aspnet-ajax/controls/dropdownlist/client-side-programming/events/onclientitemselected#onclientitemselected)

0 commit comments

Comments
 (0)