Telerik RadGrid set BoundColumn to ReadOnly in Edit Mode

一世执手 提交于 2019-12-10 19:57:01

问题


I have a Telerik RadGrid that has three bound columns and one button column. I would like to let the user edit the values in only one of the bound columns. The user can add a new record so i can't set the two bound column to read only. Is there anyway i can do this in the ASPX or do i have to do it in the code behind? I have some code that is working but it is not the best.

Here's my code:

Case "Edit"
    Dim aoeAnswerCode As GridBoundColumn = CType(e.Item.OwnerTableView.GetColumn("aoeAnswerCode"), GridBoundColumn)
    aoeAnswerCode.ReadOnly = True

Case "Update", "PerformInsert"
    For Each column As GridColumn In e.Item.OwnerTableView.RenderColumns
        If TypeOf column Is IGridEditableColumn Or column.UniqueName = "aoeAnswerCode" Then

回答1:


Simply set the ReadOnly property to true. See the example below:

<telerik:GridBoundColumn DataField="Datetime" HeaderText="Date" 
 DataFormatString="{0:MM/dd/yyyy}" ReadOnly="True">  
</telerik:GridBoundColumn>



回答2:


.aspx page code

 <telerik:GridBoundColumn DataField="ID" UniqueName="ID" HeaderText="ID">
                </telerik:GridBoundColumn>

aspx.cs page code

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode && e.Item is GridEditableItem)
    {
        if (e.Item.ItemIndex == -1)
        {
            // insert
            GridEditableItem item = e.Item as GridEditableItem;

        }
        else
        {
            // edit
            GridEditableItem item = e.Item as GridEditableItem;
            (item["ID"].Controls[0] as TextBox).ReadOnly = true;
        }

    }
}



回答3:


protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item.IsInEditMode && e.Item is GridEditFormItem)
    {
            // edit
            GridEditFormItem item = e.Item as GridEditFormItem;
            (item["column"].Controls[0] as TextBox).Enabled = false;
    }
}


来源:https://stackoverflow.com/questions/8774610/telerik-radgrid-set-boundcolumn-to-readonly-in-edit-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!