Hello,
I have a grid whose items I am always keeping in edit mode. But I've noticed that certain operations (such as column sorting, for example), causes the edit items to revert back to browse mode. To avoid this, I handle the grid commands explicitly, and set the grid back to edit mode once the command has executed.
Here is my declaration of the grid:
And here is the relevant C# code:
This works perfectly for all grid commands with the exception of the command to change the page size. When I call ExecuteCommand( ) for the GridPageSizeChangedEventArgs object (e) passed in, it throws a NullReferenceException, causing the action to fail.
How can I execute the page size event and keep my grid items always in edit mode? I realize that I can workaround this by removing the "default" case from the ItemCommand event handler and by removing the "if (!IsPostBack)" check from the grid's PreRender event handler, but I don't want to rebind the grid twice (first for the initial items, then again for setting each item to edit mode) every time the grid has to be rendered. I've noticed that this approach significantly slows down performance, especially when there are many grids being rebound several times each for each postback.
Is there a way I can get around this exception? Or alternatively, is there another way to ensure that the grid items are always in edit mode that doesn't require binding the grid twice?
Thanks!
Geoff
I have a grid whose items I am always keeping in edit mode. But I've noticed that certain operations (such as column sorting, for example), causes the edit items to revert back to browse mode. To avoid this, I handle the grid commands explicitly, and set the grid back to edit mode once the command has executed.
Here is my declaration of the grid:
<
telerik:RadGrid
ID
=
"dtgBuildingSizes"
runat
=
"server"
AllowPaging
=
"true"
AllowSorting
=
"true"
AllowFilteringByColumn
=
"true"
ShowFooter
=
"true"
AutoGenerateColumns
=
"false"
EnableHeaderContextMenu
=
"true"
EnableHeaderContextFilterMenu
=
"true"
PageSize
=
"5"
AllowMultiRowEdit
=
"true"
Width
=
"100%"
EnableLinqExpressions
=
"false"
OnNeedDataSource
=
"dtgBuildingSizes_NeedDataSource"
OnItemDataBound
=
"dtgBuildingSizes_ItemDataBound"
OnPreRender
=
"dtgBuildingSizes_PreRender"
OnItemCommand
=
"dtgBuildingSizes_ItemCommand"
>
<
MasterTableView
DataKeyNames
=
"Id"
EditMode
=
"InPlace"
IsFilterItemExpanded
=
"false"
>
<
Columns
>
<
telerik:GridBoundColumn
UniqueName
=
"SizeTypeDescr"
DataField
=
"SizeTypeDescr"
HeaderText="<%$ Resources: SizeType %>" ReadOnly="true" />
<
telerik:GridNumericColumn
UniqueName
=
"Size"
DataField
=
"Size"
DataType
=
"System.Decimal"
ItemStyle-HorizontalAlign
=
"Right"
HeaderStyle-HorizontalAlign
=
"Right"
HeaderText="<%$ Resources:RpaControlCaptions, lblSize %>" />
<
telerik:GridButtonColumn
UniqueName
=
"Delete"
CommandName
=
"Delete"
ButtonCssClass
=
"rgDel"
ButtonType
=
"LinkButton"
ItemStyle-HorizontalAlign
=
"Right"
ShowInEditForm
=
"true"
ConfirmText="<%$ Resources: AreYouSureYouWantToDeleteThisSize %>" />
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
And here is the relevant C# code:
protected
void
dtgBuildingSizes_PreRender(
object
sender, EventArgs e)
{
//initially set appropriate size grid rows to edit mode
if
(!IsPostBack)
dtgBuildingSizes.SetSizesGridToEditMode();
}
protected
void
dtgBuildingSizes_NeedDataSource(
object
sender, TelerikUI.GridNeedDataSourceEventArgs e)
{
var dataSource = ((BuildingEntity)CurrentBusinessEntity).Sizes;
dtgBuildingSizes.DataSource = dataSource;
dtgBuildingSizes.VirtualItemCount = dataSource.Count;
}
protected
void
dtgBuildingSizes_ItemDataBound(
object
sender, TelerikUI.GridItemEventArgs e)
{
if
(e.Item
is
TelerikUI.GridDataItem)
{
TelerikUI.GridDataItem dataItem = (TelerikUI.GridDataItem)e.Item;
//show/hide delete button based on availability for the size type
BuildingSizeEntity entity = (BuildingSizeEntity)dataItem.DataItem;
dataItem[
"Delete"
].Controls[0].Visible = entity.IsDeletable;
}
}
protected
void
dtgBuildingSizes_ItemCommand(
object
sender, TelerikUI.GridCommandEventArgs e)
{
switch
(e.CommandName)
{
case
RadGrid.DeleteCommandName:
if
(e.Item
is
TelerikUI.GridDataItem)
{
//remove the selected entity from the collection
TelerikUI.GridDataItem dataItem = (TelerikUI.GridDataItem)e.Item;
BuildingSizeEntity deletedEntity = GetSizeEntityForGridItem(dataItem);
((BuildingEntity)CurrentBusinessEntity).Sizes.Remove(deletedEntity);
}
break
;
default
:
if
(ControlsUtil.IsRebindNeededForRadGridCommand(e.CommandName))
{
//save previous edits before rebind
PostAllSizeData();
//execute command and place grid back into edit mode
e.ExecuteCommand(sender);
dtgBuildingSizes.SetSizesGridToEditMode();
e.Canceled =
true
;
//no further processing required
}
break
;
}
}
public
static
bool
IsRebindNeededForRadGridCommand(
string
commandName)
{
switch
(commandName)
{
// Editing
case
RadGrid.CancelAllCommandName:
case
RadGrid.CancelCommandName:
case
RadGrid.DeleteCommandName:
case
RadGrid.DeleteSelectedCommandName:
case
RadGrid.EditAllCommandName:
case
RadGrid.EditCommandName:
case
RadGrid.EditSelectedCommandName:
case
RadGrid.InitInsertCommandName:
case
RadGrid.PerformInsertCommandName:
case
RadGrid.UpdateCommandName:
case
RadGrid.UpdateEditedCommandName:
// Filtering
case
RadGrid.ClearFilterCommandName:
case
RadGrid.FilterCommandName:
case
RadGrid.HeaderContextMenuFilterCommandName:
// Paging
case
"ChangePageSize"
:
case
RadGrid.FirstPageCommandArgument:
case
RadGrid.LastPageCommandArgument:
case
RadGrid.NextPageCommandArgument:
case
RadGrid.PageCommandName:
case
RadGrid.PrevPageCommandArgument:
// Sorting
case
RadGrid.ClearSortCommandName:
case
RadGrid.SortCommandName:
// Misc.
case
RadGrid.ExpandCollapseCommandName:
case
RadGrid.RebindGridCommandName:
return
true
;
default
:
return
false
;
//all others do not implicitly call rebind
}
}
public
static
void
SetSizesGridToEditMode(
this
RadGrid grid)
{
if
(grid !=
null
)
{
SizeEntity size;
foreach
(GridDataItem dataItem
in
grid.Items)
{
size = dataItem.DataItem
as
SizeEntity;
if
(size !=
null
)
dataItem.Edit = !size.IsReadOnly;
//only set editable items to edit mode
}
grid.Rebind();
}
}
This works perfectly for all grid commands with the exception of the command to change the page size. When I call ExecuteCommand( ) for the GridPageSizeChangedEventArgs object (e) passed in, it throws a NullReferenceException, causing the action to fail.
How can I execute the page size event and keep my grid items always in edit mode? I realize that I can workaround this by removing the "default" case from the ItemCommand event handler and by removing the "if (!IsPostBack)" check from the grid's PreRender event handler, but I don't want to rebind the grid twice (first for the initial items, then again for setting each item to edit mode) every time the grid has to be rendered. I've noticed that this approach significantly slows down performance, especially when there are many grids being rebound several times each for each postback.
Is there a way I can get around this exception? Or alternatively, is there another way to ensure that the grid items are always in edit mode that doesn't require binding the grid twice?
Thanks!
Geoff