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

Grid Insert

4 Answers 103 Views
Grid
This is a migrated thread and some comments may be shown as answers.
william
Top achievements
Rank 1
william asked on 21 Oct 2010, 10:54 PM
Hi,
I have an issue, I want to insert a register in a rad grid. When I enable the property ReadOnly="true" I cannot edit the corresponding column which is right (you must not edit your primary key). However this property disables the same fields when I try to insert a new register which (for me) is wrong (I want to insert my primary key in the front-end). How can I set this property in order to maintain it as "ReadOnly"  these fields when I'm editing and besides to maintain enabled my pk fields when I insert new fields. 
I want to insert 3 primary keys. I don't want to edit it.

Thanks in advance,
William

<telerik:RadGrid  runat="server" ID="grid" AutoGenerateColumns="false"
    AllowPaging="true" OnNeedDataSource="grid_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
    OnInsertCommand="RadGrid1_InsertCommand" OnDeleteCommand="RadGrid1_DeleteCommand" >
    <MasterTableView DataKeyNames="CompanyID,BranchID,SupplierID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage"  EditMode="InPlace">
        <Columns>           
        <telerik:GridBoundColumn  DataField="CompanyID" HeaderText="Sociedad" 
             ForceExtractValue="InEditMode" ConvertEmptyStringToNull="true" ReadOnly="true" /> 
      <telerik:GridBoundColumn DataField="BranchID" HeaderText="Sucursal"  
            ForceExtractValue="Always" ConvertEmptyStringToNull="true" ReadOnly="true">
      </telerik:GridBoundColumn>
      <telerik:GridBoundColumn DataField="SupplierID" HeaderText="Tipo de identificación"  
            ForceExtractValue="Always" ConvertEmptyStringToNull="true" ReadOnly="true" >
       </telerik:GridBoundColumn>
       <telerik:GridBoundColumn DataField="Code" HeaderText="Código SAP" >
       </telerik:GridBoundColumn>
        <telerik:GridEditCommandColumn ButtonType="ImageButton" />
            <telerik:GridButtonColumn ConfirmText="XXXXXXXX" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings>
            <EditColumn ButtonType="ImageButton" />
        </EditFormSettings>
    </MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 22 Oct 2010, 08:08 AM
Hello William,

You can access the control in edit/insert mode from code behind and set its ReadOnly property accordingly like below.

C#:
protected void grid_ItemDataBound(object sender, GridItemEventArgs e)
    {
       if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridDataInsertItem) //item is about to be inserted  
        {
            GridDataInsertItem insertItem=(GridDataInsertItem)e.Item;
            TextBox txtid = (TextBox)insertItem["CompanyID"].Controls[0];
            txtid.ReadOnly = false;
         }
       if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)   //item is about to be edit
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            TextBox txtid = (TextBox)editItem["CompanyID"].Controls[0];
            txtid.ReadOnly = true;
        }  
    }

Thanks,
Princy.
0
william
Top achievements
Rank 1
answered on 22 Oct 2010, 02:27 PM
Dear Princy,

Thank you very much for your answer. It works!

William 
0
william
Top achievements
Rank 1
answered on 22 Oct 2010, 08:18 PM
Hi,
I have an additional issue. Inside the same grid I need to manage a checkbox control. I need to enable it both when I want to insert a register and when I want to edit it. I need to disable it when I want to populate the grid. When I'm editing and populating this grid it works. However whether I want to insert a register I get the following compilation error in my code aspx:Specified cast is not valid .


This is the corresponding aspx code:

<telerik:RadGrid  runat="server" ID="grid" AutoGenerateColumns="false"
    AllowPaging="true" OnNeedDataSource="grid_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
    OnInsertCommand="RadGrid1_InsertCommand" 
                    OnItemDataBound="RadGrid1_ItemDataBound" Culture="es-CO" >
    <MasterTableView DataKeyNames="CenterCostID" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage"  EditMode="InPlace">
        <Columns>           
        <telerik:GridBoundColumn  DataField="CenterCostID" HeaderText="Id Centro de Costo" 
             ForceExtractValue="InEditMode" ConvertEmptyStringToNull="true" > 
             </telerik:GridBoundColumn>
      <telerik:GridBoundColumn DataField="Description" HeaderText="Descripcion"  
            ForceExtractValue="InEditMode" ConvertEmptyStringToNull="true" >
      </telerik:GridBoundColumn>   
       
       <telerik:GridTemplateColumn UniqueName="Active" DataField="Active" HeaderText="Activo"  > 
                  <ItemTemplate> 
                      <asp:CheckBox  Checked='<%#Eval("Active") %>' ID="CheckBox1" runat="server"   /> 
                  </ItemTemplate> 
        </telerik:GridTemplateColumn> 
          
       <telerik:GridEditCommandColumn ButtonType="ImageButton" />           
        </Columns>    
    </MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" />
</telerik:RadGrid>


This is the code behind:

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
    {
       //to be inserted ->it  doesn't work
        if (e.Item.OwnerTableView.IsItemInserted && e.Item is GridDataInsertItem) 
        {
            GridDataInsertItem insertItem = (GridDataInsertItem)e.Item;
            TextBox txtid = (TextBox)insertItem["CenterCostID"].Controls[0];
            txtid.ReadOnly = false;
            GridDataItem item = (GridDataItem)e.Item;
            CheckBox chkbx = (CheckBox)item["Active"].FindControl("CheckBox1");
            chkbx.Enabled = true;

        }
//to be edited-> it works
        if (!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode)  
        {
            GridEditableItem editItem = (GridEditableItem)e.Item;
            TextBox txtid = (TextBox)editItem["CenterCostID"].Controls[0];
            txtid.ReadOnly = true;
            GridDataItem item = (GridDataItem)e.Item;
            CheckBox chkbx = (CheckBox)item["Active"].FindControl("CheckBox1");
            chkbx.Enabled = true;

        }
//to be populated-> it works
        if (e.Item is GridDataItem && !(!(e.Item is GridDataInsertItem) && e.Item.IsInEditMode) && !((e.Item.OwnerTableView.IsItemInserted && e.Item is GridDataInsertItem)))
        {
            GridDataItem item = (GridDataItem)e.Item;
            CheckBox chkbx = (CheckBox)item["Active"].FindControl("CheckBox1");
            chkbx.Enabled = false;
        }

    }


Thanks in advance.
William
0
william
Top achievements
Rank 1
answered on 22 Oct 2010, 08:42 PM
Hi,

I solved it adding the following:
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
   {
       if ((e.CommandName == RadGrid.InitInsertCommandName))
       {
           e.Canceled = true;
           //Prepare an IDictionary with the predefined values
           System.Collections.Specialized.ListDictionary newValues = new System.Collections.Specialized.ListDictionary();
 
           //set initial checked state for the checkbox on init insert
           newValues["Active"] = false;
 
           //Insert the item and rebind
           e.Item.OwnerTableView.InsertItem(newValues);
       }
   }

More information here: http://www.telerik.com/community/forums/aspnet/grid/checkbox-in-radgrid-editable-without-clicking-on-edit-button.aspx

The Best!
Tags
Grid
Asked by
william
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
william
Top achievements
Rank 1
Share this question
or