This is a migrated thread and some comments may be shown as answers.

Disable update but keep insert in GridBoundColumn?

6 Answers 253 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Diego Pazos
Top achievements
Rank 1
Diego Pazos asked on 16 Jun 2011, 05:24 PM
Hi. I have a GridBoundColumn in a RadGrid, and I'd like to know how I can disable updating the value in the column, while keeping the ability to insert it, as the readonly attribute seems to affect both updating and insertion.
Thanks in advance.

6 Answers, 1 is accepted

Sort by
0
Elliott
Top achievements
Rank 2
answered on 16 Jun 2011, 06:40 PM
use a ItemTemplate with a Label control
and an EditItemTemplate with a RAD textbox (depending of the data type)

then in the ItemCreated event handler

check for e.EditMode to be true and TypeOf e.Item Is IGridInsertItem   --> this may depend whether you are editing InPlace or not
- if the first is true and the second not true, drill down to the textbox and set ReadOnly to be true
I think setting Enabled to be False might work, too
 or - in the EditItemTemplate have both a label and a textbox
if edit - hide the textbox, show the label
if insert - show the textbox, hide the label
0
Diego Pazos
Top achievements
Rank 1
answered on 16 Jun 2011, 08:19 PM
Thanks for the reply.
Actually, my RadGrid is binding dynamically from an ObjectDatasource.
And I need to disable update/enable insert applied to columns that are also datakeys in the RadGrid.

Is there an easier approach for this scenario, that doesn't imply dynamically creating ItemTemplate columns and the rest of columns to avoid getting duplicate autogeneratecolumns and itemtemplate columns?

Maybe an alternative could be with Javascript code, perhaps on the OnGridCreated event.
I'm currently tring geting input elements with JQuery
sender.get_element().getElementsByClassName("rgEditRow")[i].getElementsByTagName("input")[2];
But for the time being I haven't had any success.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Jun 2011, 08:09 AM
Hello Diego,

In order to achieve this, access the TextBox in GridBoundColumn in ItemCreated event and set its ReadOnly property accordingly. Hope this helps.

C#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridEditFormItem && e.Item.IsInEditMode && !(e.Item is GridEditFormInsertItem))
    {
            GridEditFormItem item = (GridEditFormItem)e.Item;
            TextBox txtbox = (TextBox)item["ColumnUniqueName"].Controls[0] as TextBox;
            txtbox.ReadOnly = true;
    }
}

Thanks,
Shinu.
0
Diego Pazos
Top achievements
Rank 1
answered on 20 Jun 2011, 02:45 PM
Thanks Shinu for the solution.
I only needed to change GridEditFormItem to GridDataItem in your code, but it seems to work just fine.
0
Elliott
Top achievements
Rank 2
answered on 20 Jun 2011, 02:50 PM
The type of e.Item varies depending on the EditMode (InPlace vs default vs user control)
0
Diego Pazos
Top achievements
Rank 1
answered on 20 Jun 2011, 03:05 PM
OK. Thanks!
Tags
Grid
Asked by
Diego Pazos
Top achievements
Rank 1
Answers by
Elliott
Top achievements
Rank 2
Diego Pazos
Top achievements
Rank 1
Shinu
Top achievements
Rank 2
Share this question
or