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

editableItem.Edit = true but IsInEditMode = false

2 Answers 371 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DEWISME Vincent
Top achievements
Rank 1
DEWISME Vincent asked on 19 Oct 2010, 10:51 AM
Hello,

I'm using a RadGrid bound to an ASP.Net ObjectDataSource.

I want it to put all its rows in Edit Mode by clicking on a command button.

When the user has entered all values, he has to click on another command button to save the data.

In the code behind, I want each edited item to call the Update method defined in the ObjectDataSource.

But I get a GridException :
"Only items with IsInEditMode set to true can be updated"

I don't know why.

Here is the ASPX code for the DataSource + RadGrid:

<asp:ObjectDataSource runat="server" ID="ODS_FaceShoulderLength"
    SelectMethod="GetFaceShoulderLengthsForOneValue"
    TypeName="VAMTestsDB.TestsManagement.MachiningInfoManager">
</asp:ObjectDataSource>
<asp:ObjectDataSource runat="server" ID="ODS_Values"
    onselecting="ODS_Values_Selecting" SelectMethod="GetMachiningInfoValues"
    TypeName="VAMTestsDB.TestsManagement.MachiningInfoManager"
    OldValuesParameterFormatString="{0}"
    UpdateMethod="UpdateMachiningInfoValues">
    <UpdateParameters>
        <asp:Parameter Name="PTPId" Type="Int32" />
        <asp:Parameter Name="SpecimenId" Type="Int32" />
        <asp:Parameter Name="ThreadInterference1ValuePer100" Type="Decimal" />
        <asp:Parameter Name="SealInterference1ValuePer100" Type="Decimal" />
        <asp:Parameter Name="PinTaper1ValuePer100" Type="Decimal" />
        <asp:Parameter Name="BoxTaper1ValuePer100" Type="Decimal" />
        <asp:Parameter Name="ThreadInterference2ValuePer100" Type="Decimal" />
        <asp:Parameter Name="SealInterference2ValuePer100" Type="Decimal" />
        <asp:Parameter Name="PinTaper2ValuePer100" Type="Decimal" />
        <asp:Parameter Name="BoxTaper2ValuePer100" Type="Decimal" />
        <asp:Parameter Name="D1" Type="String" />
        <asp:Parameter Name="D2" Type="String" />
        <asp:Parameter Name="ThreadAxialGap" Type="String" />
        <asp:Parameter Name="W" Type="String" />
        <asp:Parameter Name="FaceShoulderLengthId" Type="Int32" />
        <asp:Parameter DbType="Guid" Name="UserId" />
    </UpdateParameters>
    <SelectParameters>
        <asp:Parameter Name="keys" Type="Object" />
    </SelectParameters>
</asp:ObjectDataSource>
<telerik:RadGrid runat="server" ID="RG_Values" DataSourceID="ODS_Values" AllowAutomaticUpdates="true"
    GridLines="None" AllowMultiRowEdit="true" AutoGenerateColumns="False"
    onitemcreated="RG_Values_ItemCreated"
    onitemcommand="RG_Values_ItemCommand" onprerender="RG_Values_PreRender">
    <MasterTableView DataSourceID="ODS_Values" EditMode="EditForms" DataKeyNames="PTPId,SpecimenId" CommandItemDisplay="TopAndBottom">
    <EditFormSettings EditFormType="Template">
        <FormTemplate>
                <tr>
                    <td colspan="5"> </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_TI1" MinValue="-50" MaxValue="150" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("ThreadInterference1ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.1" />
                            <NumberFormat AllowRounding="true" DecimalDigits="1" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_SI1" MinValue="-50" MaxValue="150" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("SealInterference1ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.1" />
                            <NumberFormat AllowRounding="true" DecimalDigits="1" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td colspan="3"> </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_PT1" MinValue="0" MaxValue="100" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("PinTaper1ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.01" />
                            <NumberFormat AllowRounding="true" DecimalDigits="2" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_BT1" MinValue="0" MaxValue="100" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("BoxTaper1ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.01" />
                            <NumberFormat AllowRounding="true" DecimalDigits="2" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td colspan="2"> </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_TI2" MinValue="-50" MaxValue="150" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("ThreadInterference2ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.1" />
                            <NumberFormat AllowRounding="true" DecimalDigits="1" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_SI2" MinValue="-50" MaxValue="150" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("SealInterference2ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.1" />
                            <NumberFormat AllowRounding="true" DecimalDigits="1" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td colspan="3"> </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_PT2" MinValue="0" MaxValue="100" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("PinTaper2ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.01" />
                            <NumberFormat AllowRounding="true" DecimalDigits="2" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td>
                        <telerik:RadNumericTextBox runat="server" AllowOutOfRangeAutoCorrect="true" ButtonsPosition="Right" DataType="System.Decimal" ID="NTB_BT2" MinValue="0" MaxValue="100" ShowSpinButtons="true" Type="Number" DbValue='<%#Bind("BoxTaper2ValuePer100") %>'>
                            <IncrementSettings InterceptArrowKeys="true" InterceptMouseWheel="true" Step="0.01" />
                            <NumberFormat AllowRounding="true" DecimalDigits="2" DecimalSeparator="." />
                            <ClientEvents OnKeyPress="onNumericKeyPress" />
                        </telerik:RadNumericTextBox>
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="TB_D1" TextMode="MultiLine" Rows="5" cols="20" Text='<%#Bind("D1") %>' />
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="TB_D2" TextMode="MultiLine" Rows="5" cols="20" Text='<%#Bind("D2") %>' />
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="TB_TAG" TextMode="MultiLine" Rows="5" cols="20" Text='<%#Bind("ThreadAxialGap") %>' />
                    </td>
                    <td>
                        <asp:TextBox runat="server" ID="TB_W" TextMode="MultiLine" Rows="5" cols="20" Text='<%#Bind("W") %>' />
                    </td>
                    <td>
                        <asp:DropDownList ID="DDL_FaceShoulderLength" runat="server"
                            DataSourceID="ODS_FaceShoulderLength" DataTextField="Name" DataValueField="Id"
                            SelectedValue='<%# Bind("FaceShoulderLengthId") %>'>
                        </asp:DropDownList>
                    </td>
                </tr>
        </FormTemplate>
    </EditFormSettings>
    <RowIndicatorColumn>
            <HeaderStyle Width="20px" />
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px" />
        </ExpandCollapseColumn>
        <Columns>
            <telerik:GridBoundColumn DataField="PTPReference" HeaderText="PTP"
                ReadOnly="true" SortExpression="PTPReference" UniqueName="PTPReference">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PinName" HeaderText="Pin" ReadOnly="true"
                SortExpression="PinName" UniqueName="PinName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BoxName" HeaderText="Box" ReadOnly="true"
                SortExpression="BoxName" UniqueName="BoxName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ThreadInterference1Type" HeaderText="TI1"
                HeaderTooltip="Thread Interference 1" ReadOnly="true"
                SortExpression="ThreadInterference1Type" UniqueName="ThreadInterference1Type">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SealInterference1Type" HeaderText="SI1"
                HeaderTooltip="Seal Interference 1" ReadOnly="true"
                SortExpression="SealInterference1Type" UniqueName="SealInterference1Type">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ThreadInterference1Value"
                DataType="System.Decimal" HeaderText="TI1 Value"
                HeaderTooltip="Thread Interference 1 Value" DataFormatString="{0:###.#%}"
                SortExpression="ThreadInterference1Value" UniqueName="ThreadInterference1Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SealInterference1Value" DataFormatString="{0:###.#%}"
                DataType="System.Decimal" HeaderText="SI1 Value"
                HeaderTooltip="Seal Interference 1 Value"
                SortExpression="SealInterference1Value" UniqueName="SealInterference1Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PinTaper1Type" HeaderText="PT1"
                HeaderTooltip="Pin Taper 1" ReadOnly="true" SortExpression="PinTaper1Type"
                UniqueName="PinTaper1Type">
            </telerik:GridBoundColumn>
            <telerik:GridCheckBoxColumn DataField="PinDoubleThreadTaper1"
                DataType="System.Boolean" HeaderText="PDTT 1 ?"
                HeaderTooltip="Pin Double Thread Taper 1 ?" ReadOnly="true"
                SortExpression="PinDoubleThreadTaper1" UniqueName="PinDoubleThreadTaper1">
            </telerik:GridCheckBoxColumn>
            <telerik:GridBoundColumn DataField="BoxTaper1Type" HeaderText="BT1"
                HeaderTooltip="Box Taper 1" ReadOnly="true" SortExpression="BoxTaper1Type"
                UniqueName="BoxTaper1Type">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PinTaper1Value" DataType="System.Decimal" DataFormatString="{0:###.##%}"
                HeaderText="PT1 Value" HeaderTooltip="Pin Taper 1 Value"
                SortExpression="PinTaper1Value" UniqueName="PinTaper1Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BoxTaper1Value" DataType="System.Decimal" DataFormatString="{0:###.##%}"
                HeaderText="BT1 Value" HeaderTooltip="Box Taper 1 Value"
                SortExpression="BoxTaper1Value" UniqueName="BoxTaper1Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ThreadInterference2Type" HeaderText="TI2"
                HeaderTooltip="Thread Interference 2" ReadOnly="true"
                SortExpression="ThreadInterference2Type" UniqueName="ThreadInterference2Type">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SealInterference2Type" HeaderText="SI2"
                HeaderTooltip="Seal Interference 2" ReadOnly="true"
                SortExpression="SealInterference2Type" UniqueName="SealInterference2Type">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="ThreadInterference2Value" DataFormatString="{0:###.#%}"
                DataType="System.Decimal" HeaderText="TI2 Value"
                HeaderTooltip="Thread Interference 2 Value"
                SortExpression="ThreadInterference2Value" UniqueName="ThreadInterference2Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="SealInterference2Value" DataFormatString="{0:###.#%}"
                DataType="System.Decimal" HeaderText="SI2 Value"
                HeaderTooltip="Seal Interference 2 Value"
                SortExpression="SealInterference2Value" UniqueName="SealInterference2Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PinTaper2Type" HeaderText="PT2"
                HeaderTooltip="Pin Taper 2" ReadOnly="true" SortExpression="PinTaper2Type"
                UniqueName="PinTaper2Type">
            </telerik:GridBoundColumn>
            <telerik:GridCheckBoxColumn DataField="PinDoubleThreadTaper2"
                DataType="System.Boolean" HeaderText="PDTT 2 ?"
                HeaderTooltip="Pin Double Thread Taper 2 ?" ReadOnly="true"
                SortExpression="PinDoubleThreadTaper2" UniqueName="PinDoubleThreadTaper2">
            </telerik:GridCheckBoxColumn>
            <telerik:GridBoundColumn DataField="BoxTaper2Type" HeaderText="BT2"
                HeaderTooltip="Box Taper 2" ReadOnly="true" SortExpression="BoxTaper2Type"
                UniqueName="BoxTaper2Type">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="PinTaper2Value" DataType="System.Decimal" DataFormatString="{0:###.##%}"
                HeaderText="PT2 Value" HeaderTooltip="Pin Taper 2 Value"
                SortExpression="PinTaper2Value" UniqueName="PinTaper2Value">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="BoxTaper2Value" DataType="System.Decimal" DataFormatString="{0:###.##%}"
                HeaderText="BT2 Value" HeaderTooltip="Box Taper 2 Value"
                SortExpression="BoxTaper2Value" UniqueName="BoxTaper2Value">
            </telerik:GridBoundColumn>
            <telerik:GridTemplateColumn HeaderText="D1" UniqueName="D1">
                <ItemTemplate>
                    <div class="autocut" title='<%#Eval("D1") %>'><%#Eval("D1") %></div>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="D2" UniqueName="D2" ItemStyle-Width="50px">
                <ItemTemplate>
                    <div class="autocut" title='<%#Eval("D2") %>'><%#Eval("D2")%></div>
                </ItemTemplate>
                <ItemStyle Width="50px" />
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="ThreadAxialGap" DataType="System.String"
                HeaderText="TAG" HeaderTooltip="Thread Axial Gap"
                SortExpression="ThreadAxialGap" UniqueName="ThreadAxialGap">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="W" DataType="System.String" HeaderText="W"
                SortExpression="W" UniqueName="W">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn HeaderText="FSL"
                HeaderTooltip="Face Shoulder Length" DataField="FaceShoulderLength" DataType="System.String" SortExpression="FaceShoulderLength" UniqueName="FaceShoulderLength">
            </telerik:GridBoundColumn>                       
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

And the code behind:

protected void RG_Values_ItemCreated(object sender, GridItemEventArgs e)
{
    if (e.Item is GridCommandItem)
    {
        GridCommandItem cmdItem = e.Item as GridCommandItem;
 
        cmdItem.Controls.Clear();
 
        TableCell commandCell = new TableCell();
        cmdItem.Controls.Add(commandCell);
        if (!this.IsInEditMode)
        {
            Button editButton = new Button();
            editButton.Text = "Edit";
            editButton.CommandName = "EditAll";
            commandCell.Controls.Add(editButton);
        }
        else
        {
            Button saveButton = new Button();
            saveButton.Text = "Save";
            saveButton.CommandName = "SaveAll";
            commandCell.Controls.Add(saveButton);
        }
    }
}
 
protected void RG_Values_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == "EditAll")
    {
        this.IsInEditMode = true;
    }
    else if (e.CommandName == "SaveAll")
    {
        foreach (GridDataItem item in this.RG_Values.EditItems)
        {
            if (item.Edit)
            {
                item.FireCommandEvent("Update", null);
                item.Edit = false;
            }
        }
    }
}
protected void RG_Values_PreRender(object sender, EventArgs e)
{
    foreach (GridItem item in this.RG_Values.Items)
    {
        item.Edit = this.IsInEditMode;
    }
    this.RG_Values.Rebind();
}

Any help would be appreciated !

Thanks

Vince


2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 19 Oct 2010, 01:00 PM
Hello Vince,

Since the items are in edit mode, try to access each item as 'GridEditFormItem' than GridDataItem. Sample code is given below for your reference.

C#:
protected void RG_Values_ItemCommand(object source, GridCommandEventArgs e)
 {
  if(e.CommandName == "SaveAll")
    {
      foreach (GridEditFormItem item in this.RG_Values.MasterTableView.GetItems(GridItemType.EditFormItem))
        {
          item.FireCommandEvent("Update", null);
          item.Edit = false;
        }
    }
 }

Also go through the following documentation:
Performing batch updates


Thanks,
Princy.
0
DEWISME Vincent
Top achievements
Rank 1
answered on 19 Oct 2010, 03:43 PM
Thanks a lot, it works fine!

Tags
Grid
Asked by
DEWISME Vincent
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
DEWISME Vincent
Top achievements
Rank 1
Share this question
or