There are cases when you may prefer to display the insertion form above/below the active grid page or visualize the form on the current/first grid page
instead on the last page (which is the default behavior on init insert). Furthermore, it would be convenient for the end user to keep the current page index when pressing the
[Cancel] button from the insertion form.
This is a pretty straight-forward task using the InsertItemDisplay and InsertItemPageIndexAction properties of the GridTableView
object. here are the possible values for these properties:
InsertItemDisplay: Top (default value) or Bottom
InsertItemPageIndexAction: ShowItemOnCurrentPage, ShowItemOnLastPage (default value) or ShowItemOnFirstPage
The usage of these properties is illustrated in this example.
If you prefer to retain the the active page on insert action with coding, subscribe to the ItemCommand event of RadGrid. The steps you have to undertake
are:
- Cancel the default action on init insert and store the current page index
- Process the insert invoking the InsertItem() method, after that recover the page index and rebind the grid
- Intercept the cancel command when the insert form is visible and keep the active page displayed
The code snippets below represent a sample implementation:
| ASPX/ASCX |
Copy Code |
|
<telerik:RadGrid ID="RadGrid1" DataSourceID="AccessDataSource1" AllowSorting="True"
Skin="Hay" runat="server" GridLines="None"
Width="95%" AllowPaging="true">
<MasterTableView Width="100%" CommandItemDisplay="TopAndBottom" />
<PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>
<br />
<asp:AccessDataSource ID="AccessDataSource1" DataFile="~/Grid/Data/Access/Nwind.mdb"
SelectCommand="SELECT CustomerID, CompanyName, ContactName,
ContactTitle, Address, PostalCode FROM Customers"
runat="server" />
|
| VB.NET |
Copy Code |
|
Protected Sub RadGrid1_ItemCommand(ByVal source As Object, ByVal e As
Telerik.Web.UI.GridCommandEventArgs)
Handles RadGrid1.ItemCommand
If (e.CommandName = RadGrid.InitInsertCommandName) Then
e.Canceled = True
Session( "curPageIndex") =
e.Item.OwnerTableView.CurrentPageIndex
e.Item.OwnerTableView.InsertItem()
e.Item.OwnerTableView.CurrentPageIndex = Integer.Parse(Session("curPageIndex"))
e.Item.OwnerTableView.Rebind()
ElseIf (e.CommandName = RadGrid.CancelCommandName AndAlso e.Item.OwnerTableView.IsItemInserted) Then
e.Item.OwnerTableView.CurrentPageIndex = Integer.Parse(Session("curPageIndex"))
End If
End Sub
|
| C# |
Copy Code |
|
protected void RadGrid1_ItemCommand(object
source, Telerik.Web.UI.GridCommandEventArgs e)
{ if (e.CommandName == RadGrid.InitInsertCommandName)
{
e.Canceled = true;
Session["curPageIndex"] = e.Item.OwnerTableView.CurrentPageIndex;
e.Item.OwnerTableView.InsertItem();
e.Item.OwnerTableView.CurrentPageIndex = int.Parse(Session["curPageIndex"]);
e.Item.OwnerTableView.Rebind();
}
else if (e.CommandName == RadGrid.CancelCommandName &&
e.Item.OwnerTableView.IsItemInserted)
{
e.Item.OwnerTableView.CurrentPageIndex = int.Parse(Session["curPageIndex"]);
}
}
|