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

Edit vs insert enabled column.

11 Answers 219 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 10 Nov 2011, 06:48 PM
I have tried several possibilities, none of which seem to work.  I need the "Variable" column to be editable on an Insert, but not editable on an edit.  The "Value" column has to be editable in both cases.

The first and third possibilities both leave the "Variable" column editable always.  The middle leaves the "Variable" column never editable.  What am I missing?

BTW the examples on how to do the first possibility leave out exactly how to set the readonly property.

        protected void SubstitutionVariables_OnItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (e.Item.OwnerTableView.IsItemInserted)
                {
                    Telerik.Web.UI.RadTextBox tb = (Telerik.Web.UI.RadTextBox)e.Item.FindControl("rtbSubstVarEdit");
                    tb.Visible = true;
                    tb.ReadOnly = false;
                    tb.Enabled = true;
                }
                else
                {
                    Telerik.Web.UI.RadTextBox tb = (Telerik.Web.UI.RadTextBox)e.Item.FindControl("rtbSubstVarEdit");
                    tb.Visible = true;
                    tb.ReadOnly = true;
                    tb.Enabled = false;
                }
            }
        }

        protected void SubstitutionVariables_OnColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e)
        {
            if (e.Column.UniqueName == "Variable")
            {
                GridBoundColumn Column = (GridBoundColumn)e.Column;
                Column.ReadOnly = !e.OwnerTableView.IsItemInserted;
            }
        }

            <telerik:RadGrid ID="RadGrid1" runat="server"
                AllowMultiRowSelection="true"
                AllowAutomaticDeletes="false"
                AllowAutomaticInserts="true"
                AllowAutomaticUpdates="true"
                AllowFilteringByColumn="false"
                AllowMultiRowEdit="false"
                AllowPaging="false"
                AllowSorting="false"
                OnNeedDataSource="SubstitutionVariables_OnNeedDataSource"
                OnColumnCreated="SubstitutionVariables_OnColumnCreated"
                OnItemCreated="SubstitutionVariables_OnItemCreated"
                OnUpdateCommand="SubstitutionVariables_OnUpdate"
                OnInsertCommand="SubstitutionVariables_OnInsert"
                OnDeleteCommand="SubstitutionVariables_OnDelete">

                <ClientSettings><Selecting AllowRowSelect="true" /></ClientSettings>
                <MasterTableView InsertItemPageIndexAction="ShowItemOnCurrentPage" EditMode="InPlace" >
                    <Columns>
                        <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
                        <telerik:GridTemplateColumn DataField="Variable" HeaderText="Variable" UniqueName="SubstitutionVariable"
                            Visible="false" >
                            <ItemTemplate>
                                <asp:Label ID="lblSubstVar" runat="server" ClientIDMode="Static"
                                    Width="150px" />
                            </ItemTemplate>
                            <InsertItemTemplate>
                                <telerik:RadTextBox ID="rtbSubstVarEdit" runat="server" ClientIDMode="Static"
                                    Width="150px" />
                            </InsertItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadTextBox ID="rtbSubstVarEdit" runat="server" ClientIDMode="Static" ReadOnly="true"
                                    Width="150px" />
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                        <telerik:GridTemplateColumn DataField="Value" HeaderText="Value" UniqueName="SubstitutionValue"
                            Visible="false">
                            <ItemTemplate>
                                <asp:Label ID="lblSubstValue" runat="server" ClientIDMode="Static"
                                    Width="150px" />
                            </ItemTemplate>
                            <InsertItemTemplate>
                                <telerik:RadTextBox ID="rtbSubstValueEdit" runat="server" ClientIDMode="Static"
                                    Width="150px" />
                            </InsertItemTemplate>
                            <EditItemTemplate>
                                <telerik:RadTextBox ID="rtbSubstValueEdit" runat="server" ClientIDMode="Static"
                                    Width="150px" />
                            </EditItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                    <CommandItemTemplate>
                    </CommandItemTemplate>
                </MasterTableView>
            </telerik:RadGrid>

11 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 11 Nov 2011, 06:17 AM
Hello John,

Try the following code snippet.
C#:
protected void grid_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["Variable"].Controls[0] as TextBox;
     txtbox.ReadOnly = true;
 }
}

-Shinu.
0
John
Top achievements
Rank 1
answered on 11 Nov 2011, 03:16 PM
Why am I seeing 2 complete sets of controls in Forms Mode?
0
Iana Tsolova
Telerik team
answered on 14 Nov 2011, 10:36 AM
Hello Johhn,

To achieve your goal, I suggest that you control the column ReadOnly property in the grid ItemCommand event. Try the below code and let me know if it helps:
protected void SubstitutionVariables_ItemCommand(object sender, GridCommandEventArgs e)
{
    if(e.CommandName == RadGrid.EditCommandName)
    {
        SubstitutionVariables.MasterTableView.IsItemInserted = false;
        (SubstitutionVariables.MasterTableView.GetColumnSafe("Variable") as GridBoundColumn).ReadOnly = true;
    }
    if(e.CommandName == RadGrid.InitInsertCommandName)
    {
        SubstitutionVariables.MasterTableView.EditIndexes.Clear();
        (SubstitutionVariables.MasterTableView.GetColumnSafe("Variable") as GridBoundColumn).ReadOnly = false;
    }
}


Kind regards,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
John
Top achievements
Rank 1
answered on 14 Nov 2011, 04:24 PM
After a meeting late on Friday, I have been told the desired result is a grid where all edits are inline rather than form or popup and:

All inserted items have both columns editable.
All old items have only the "Value" column editable.

Second best would be to have:

An insert popup with both items editable.
All items have only the "Value" column editable

I believe I can turn on multi edit, and make each item editable on creation.  However, I cannot figure out how to get inline edit to work and have only one item editable.  I have gotten form to work this way, but the method used to distinguish between insert and edit seems to fall apart when I switch to inline.
0
Iana Tsolova
Telerik team
answered on 15 Nov 2011, 01:52 PM
Hi Johhn,

The previously provided code is universal and does not depend on the EditMode of the MasterTableView. Check the attached sample and let me know if it helps.

Best wishes,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
John
Top achievements
Rank 1
answered on 15 Nov 2011, 10:16 PM
OK, now in InPlace mode, how do I get it to save rather than discard the changes.
0
Iana Tsolova
Telerik team
answered on 16 Nov 2011, 05:38 AM
Hello Johhn,

Have you implemented the update and insert operations in your case? Can you specify when the changes are disregarded?

Best wishes,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
John
Top achievements
Rank 1
answered on 16 Nov 2011, 02:44 PM
It I do Forms (either inline form or popup) there is an update button or an inset button.  In InPlace mode there are no buttons.  The only time I see a save is when that button is pushed.  I would like to save when switching from insert to edit or edit to insert, and during a "grand save" that includes several other edit fields.  It can be initiated either client side or server side.
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 Nov 2011, 03:11 PM
Hello ,

void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
  {
 
      if (e.Item.IsInEditMode && e.Item is GridEditableItem)
      {
          GridEditableItem item = e.Item as GridEditableItem;
          //Button btnDelete = item.FindControl("btnDelete") as Button;
          //btnDelete.Style.Add("display", "none");
          //btnDelete.Visible = false;
 
          item["EditColumn"].Visible = false;
      }
 
  }

void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.EditCommandName)
        {
            if (RadGrid1.MasterTableView.IsItemInserted)
            {
                GridEditableItem item = RadGrid1.MasterTableView.GetInsertItem();
                // perform  insert  oparatio here
                // (item["ID1"].Controls[0] as TextBox).Text // you can access insert item
                RadGrid1.MasterTableView.IsItemInserted = false;
            }
        }
        else if (e.CommandName == RadGrid.InitInsertCommandName)
        {
            foreach (GridEditableItem item in RadGrid1.EditItems)
            {
                // perform  Edit  operation here
                // (item["ID1"].Controls[0] as TextBox).Text // you can access Edit item
                item.Edit = false;
            }
        }
 
}

void Button1_Click(object sender, EventArgs e) // Grand Save Button Click
   {
 
  if (RadGrid1.MasterTableView.IsItemInserted)
        {
            GridEditableItem item = RadGrid1.MasterTableView.GetInsertItem();
            // perform  insert  oparatio here
            // (item["ID1"].Controls[0] as TextBox).Text // you can access insert item
            RadGrid1.MasterTableView.IsItemInserted = false;
        }
 
 
        foreach (GridEditableItem item in RadGrid1.EditItems)
        {
            // perform  Edit  operation here
            // (item["ID1"].Controls[0] as TextBox).Text // you can access Edit item
            item.Edit = false;
        }
 
        // if you are used Advance data Binding
        RadGrid1.Rebind();
        //else
        RadGrid1.DataSource = "Your Data Source";
        RadGrid1.DataBind();
}

<Columns>
                   <telerik:GridBoundColumn DataField="ID1" HeaderText="ID1" UniqueName="ID1">
                   </telerik:GridBoundColumn>
                 
                   <telerik:GridEditCommandColumn  UniqueName="EditColumn">
                   </telerik:GridEditCommandColumn>
               </Columns>


Thanks,
Jayesh Goyani
0
Karrie
Top achievements
Rank 1
answered on 31 Dec 2012, 05:58 PM
I've got a RadGrid with inline Edit and all rows load in edit mode. User may add a new blank empty row, into which he will then type
his data. After he is finished with all edits and any inserts, he will then click a "Save All" button, which should then save not only his
edited rows, but his newly inserted row as well.

When I follow your example, even though I have typed a value into the "FirstName" column of the newly added row, the text shows a value of "&nbsp;".

Please assist.

Private Sub rgCrewList_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles rgCrewList.ItemCommand
 
        If rgCrewList.MasterTableView.IsItemInserted Then
             Dim insertedItem As GridEditableItem = rgCrewList.MasterTableView.GetInsertItem()
            Dim firstName As String = insertedItem("FirstName").Text
         End If
  
        If (e.CommandName = "SaveAll") Then
            For Each editedItem As GridEditableItem In rgCrewList.EditItems
                Dim newValues As Hashtable = New Hashtable
                'The GridTableView will fill the values from all editable columns in the hash
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem)
                sdsCrewList.UpdateCommand = String.Format("Update marina.dbo.tbl_lead_contact_master SET FirstName='{0}', LastName='{1}' WHERE ukid='{2}'", newValues("FirstName"), newValues("LastName"), editedItem.GetDataKeyValue("ukid").ToString())
                sdsCrewList.Update()
                'editedItem.Edit = False
            Next
        End If
     End Sub

<telerik:RadGrid ID="rgCrewList" runat="server" DataSourceID="sdsCrewList" Width="1090px" AllowMultiRowEdit="true"
                    GridLines="None" Skin="WebBlue" CellSpacing="0" AllowAutomaticUpdates="True" AllowAutomaticInserts="True">
                    <ClientSettings>
                        <Scrolling AllowScroll="True" UseStaticHeaders="True" />
                        <Selecting AllowRowSelect="true" />
                    </ClientSettings>
                    <MasterTableView DataKeyNames="ukid" DataSourceID="sdsCrewList" EditMode="InPlace"
                        CommandItemDisplay="Top" AutoGenerateColumns="False" AllowAutomaticUpdates="True"
                        AllowAutomaticInserts="True">
                         
                        <Columns>
                            <telerik:GridBoundColumn DataField="ukid" DataType="System.Int32" HeaderText="ukid"
                                ReadOnly="True" SortExpression="ukid" UniqueName="ukid" Visible="False">
                                <HeaderStyle Width="0px" />
                                <ItemStyle Width="0px" />
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="FirstName" HeaderText="Contact First Name" SortExpression="FirstName"
                                UniqueName="FirstName">
                                <HeaderStyle Width="200px" />
                                <ItemStyle Width="200px" />
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="LastName" HeaderText="Contact Last Name" SortExpression="LastName"
                                UniqueName="LastName">
                                <HeaderStyle Width="200px" />
                                <ItemStyle Width="200px" />
                            </telerik:GridBoundColumn>
                            <telerik:GridDropDownColumn DataField="ContactTypeUkid" DataSourceID="sdsContactTypes"
                                DropDownControlType="RadComboBox" HeaderText="Contact Type" ListTextField="ContactType"
                                ListValueField="ContactTypeUkid" UniqueName="ContactTypeUkid">
                                <HeaderStyle Width="180px" />
                                <ItemStyle Width="180px" />
                            </telerik:GridDropDownColumn>
                            <telerik:GridBoundColumn DataField="CellPhone" HeaderText="Cell Phone" UniqueName="CellPhone">
                                <HeaderStyle Width="140px" />
                                <ItemStyle Width="140px" />
                            </telerik:GridBoundColumn>
                            <telerik:GridBoundColumn DataField="Email" HeaderText="Email" UniqueName="Email">
                                <HeaderStyle Width="200px" />
                                <ItemStyle Width="200px" />
                            </telerik:GridBoundColumn>
                            <telerik:GridCheckBoxColumn DataField="AuthCrewPurch" HeaderText="Auth Crew Purch"
                                UniqueName="AuthCrewPurch">
                                <HeaderStyle Width="100px" />
                                <ItemStyle Width="100px" />
                            </telerik:GridCheckBoxColumn>
                            <telerik:GridTemplateColumn UniqueName="PrimaryContact" DataField="PrimaryContact"
                                HeaderText="Primary Contact">
                                <ItemTemplate>
                                 <asp:CheckBox ID="chkPrimaryContact" runat="server" OnClick="deselectAllOtherRows(this);" checked='<%# IF(Eval("PrimaryContact") is DBNull.Value, False, Eval("PrimaryContact")) %>'/> 
                                 </ItemTemplate>
                            </telerik:GridTemplateColumn>
                        </Columns>
                        <HeaderStyle Font-Bold="True" />
                        <CommandItemTemplate>
                            <asp:Button runat="server" ID="UpdateAll" Text="Save All" CommandName="SaveAll" />
                            <asp:Button ID="btnInsertRecord" Text="Insert Record" SkinID="WebBlue" Runat="server" CommandName="InitInsert" ></asp:Button>
                        </CommandItemTemplate>
                    </MasterTableView>
                    <FilterMenu EnableImageSprites="False">
                    </FilterMenu>
                </telerik:RadGrid>

Please see attached files for additional information.
Thank you so much for your assistance.


0
Shinu
Top achievements
Rank 2
answered on 02 Jan 2013, 07:10 AM
Hi,

Try accessing the column as shown below.
VB:
Protected Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If RadGrid1.MasterTableView.IsItemInserted Then
        Dim insertedItem As GridEditableItem = RadGrid1.MasterTableView.GetInsertItem()
        Dim txt As TextBox = DirectCast(insertedItem("FirstName").Controls(0), TextBox)
        Dim firstName As String = txt.Text
    End If
End Sub

Thanks,
Shinu.
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
John
Top achievements
Rank 1
Iana Tsolova
Telerik team
Jayesh Goyani
Top achievements
Rank 2
Karrie
Top achievements
Rank 1
Share this question
or