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

Edit/Insert mode on gridNumricColumn

1 Answer 41 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cecilie Nordbø
Top achievements
Rank 1
Cecilie Nordbø asked on 23 Jul 2010, 03:15 PM
Hi.
Is it possible to distinguish between edit and insert mode for a gridNumricColumn when not use a formTemplate?
I have read the documentation Distinguish edit/insert mode on ItemCreated/ItemDataBound but i still have problem to get it to work.
I want the gridNumricColumn (CardNumber) to be readonly=false when insertmode and readonly=true otherwise. The problem is that the field CardNumber is not visible when insertmode withe the following code:

protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{         
if (e.Item is GridDataInsertItem && e.Item.IsInEditMode)
    {
 
var column = (GridNumericColumn)e.Item.OwnerTableView.GetColumnSafe("CardNumberColumn");
 
  if (e.Item.OwnerTableView.IsItemInserted)
       column.ReadOnly = false;
               else
       column.ReadOnly = true;
 }
}

<telerik:RadGrid ID="RadGrid1" runat="server"AllowAutomaticInserts="True"   AllowAutomaticUpdates="True" DataSourceID="LinqDataSource1" ondatabound="RadGrid1_DataBound" onitemcommand="RadGrid1_ItemCommand"           onitemdatabound="RadGrid1_ItemDataBound" onitemcreated="RadGrid1_ItemCreated">
<MasterTableView AutoGenerateColumns="FalseDataKeyNames="CardNumber,ProductNumber,MediaType"
         DataSourceID="LinqDataSource1" EditMode="InPlace" >
<Columns>
       <telerik:GridNumericColumn DataField="CardNumber" DataType="System.Int32" HeaderText="Kortnr." ReadOnly="true" SortExpression="CardNumber" UniqueName="CardNumberColumn" >
       </telerik:GridNumericColumn>     
       <telerik:GridBoundColumn DataField="CardName" HeaderText="Kortnavn" SortExpression="CardName" UniqueName="CardName" >
       </telerik:GridBoundColumn>     
       <telerik:GridEditCommandColumn CancelText="Avbryt" EditText="Endre" InsertText="Sett inn" UpdateText="Oppdater"></telerik:GridEditCommandColumn>
       </Columns>
   </MasterTableView>   
   </telerik:RadGrid>


Have tried both with ItemCreated and ItemDataBound methode.

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 26 Jul 2010, 06:26 AM
Hello,

You can achieve this by accessing the RadNumericTextBox control and then set its ReadOnly property accordingly. Check out the following codes snippet.

ASPX:
<telerik:GridNumericColumn DataField="CardNumber" DataType="System.Int32" HeaderText="Kortnr." SortExpression="CardNumber" UniqueName="CardNumberColumn" >
</telerik:GridNumericColumn>

C#:
if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridDataInsertItem)
       //item is about to be inserted  
       {

           GridDataInsertItem insertItem = (GridDataInsertItem)e.Item;
           RadNumericTextBox txtBox = (RadNumericTextBox)insertItem["CardNumberColumn"].Controls[0];
           txtBox.ReadOnly = false;
       }
       if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)
       //item is about to be edit
       {
           GridEditableItem editItem = (GridEditableItem)e.Item;
           RadNumericTextBox txtBox = (RadNumericTextBox)editItem["CardNumberColumn"].Controls[0];
           txtBox.ReadOnly = true;
       }

Thanks,
Princy.
Tags
Grid
Asked by
Cecilie Nordbø
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or