4 Answers, 1 is accepted
Try the following code snippet to prevent editing in Grid.
CS:
protected void RadGrid1_PreRender(object sender, EventArgs e) |
{ |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
item.Edit = false; |
} |
} |
If you want to remove the auto generated edit column set AutoGenerateEditColumn to false.
Thanks
Shinu.
I tried your code snippet but I still can click on the Edit icon of a row to get row edit. When I click away, the page gave this error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
Thank you,
d-cpt
Here is the grid:
<telerik:RadGrid ID="radgridAdmin" runat="server" AllowPaging="True"
Skin="WebBlue" ShowStatusBar="True" AllowSorting="True"
AutoGenerateColumns="False" GridLines="None"
ShowFooter="True" DataSourceID="SqlDataSource1" AllowAutomaticInserts=True AllowAutomaticDeletes=True
OnPreRender="radgridAdmin_PreRender"
OnItemInserted="radgridAdmin_ItemInserted"
OnDataBound="radgridAdmin_DataBound"
OnItemCreated="radgridAdmin_ItemCreated">
<PagerStyle Mode="NextPrevNumericAndAdvanced" />
<MasterTableView DataSourceID="SqlDataSource1" DataKeyNames="PersonId" EditMode=InPlace CommandItemDisplay="TopAndBottom" HorizontalAlign="NotSet" AutoGenerateColumns="False">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
<Columns>
<telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn" >
<ItemStyle CssClass="MyImageButton" />
</telerik:GridEditCommandColumn>
<telerik:GridDropDownColumn DataField="PersonID" DataSourceID="SqlDataSource2"
HeaderText="Administrator Name" ListTextField="UserName" ListValueField="PersonID"
UniqueName="PersonID" HeaderStyle-Width=50%>
<HeaderStyle Width="50%"></HeaderStyle>
</telerik:GridDropDownColumn>
<telerik:GridButtonColumn ConfirmText="Delete this admin?" ConfirmDialogType="RadWindow" ConfirmTitle="Delete" ButtonType="ImageButton"
CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
<ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
</telerik:GridButtonColumn>
</Columns>
<EditFormSettings>
<EditColumn UniqueName="EditCommandColumn1"></EditColumn>
</EditFormSettings>
</MasterTableView>
<FilterMenu EnableTheming="True">
<CollapseAnimation Type="OutQuint" Duration="200"></CollapseAnimation>
</FilterMenu>
</telerik:RadGrid>
You can check out the following code snippet to disable the Edit buttons in the EditCommandColumn.
aspx:
<telerik:GridEditCommandColumn ButtonType="ImageButton" EditImageUrl="images/Edit.gif" UniqueName="EditCommandColumn"> |
</telerik:GridEditCommandColumn> |
cs:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridDataItem) |
{ |
GridDataItem item = (GridDataItem)e.Item; |
ImageButton imageButton = (ImageButton)item["EditCommandColumn"].Controls[0]; |
imageButton.Enabled = false; |
} |
} |
Princy.
I actually went a bit further to reset default CssClass to a new one in which sets cursor:default insteads of cursor:hand When user has the mouse over the column or the image, the cursor doesn't change. this would improve the user experience a bit better.
if
(imageButton.ImageUrl == "myEditImagePath")
{
item["EditCommandColumn"].CssClass = "MyImageButtonDisabled";
imageButton.Enabled = false;
imageButton.CssClass = "MyImageButtonDisabled";
imageButton.ToolTip = "";
}
Regards,
d-cpt