Telerik RadGrid在编辑模式下将BoundColumn设置为ReadOnly

时间:2012-01-08 01:20:37

标签: c# vb.net telerik radgrid

我有一个Telerik RadGrid,它有三个绑定列和一个按钮列。我想让用户只在一个绑定列中编辑值。用户可以添加新记录,因此我无法将两个绑定列设置为只读。无论如何,我可以在ASPX中执行此操作,还是必须在后面的代码中执行此操作?我有一些有效的代码,但它不是最好的。

这是我的代码:

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

3 个答案:

答案 0 :(得分:7)

只需将ReadOnly属性设置为true即可。请参阅以下示例:

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

答案 1 :(得分:6)

.aspx页码

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

aspx.cs页面代码

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;
        }

    }
}

答案 2 :(得分:0)

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;
    }
}