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

can't grab values from grid

3 Answers 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Amanda
Top achievements
Rank 2
Amanda asked on 15 Dec 2010, 08:41 PM
I have a rad grid on my web page and what I want to do is inline editing on only a few cells in a row.  When the quantity cell is changed, I would like to be able to make some calculations and change some of the other cells in the grid that are read only.  I have the grid up and have the edit button, but I don't know how to get those values to change the other cells before it updates the database.  I've tried a couple different ways and the only values I get are   for every cell. 
Here's my source code for the grid and sqldatasource.

<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" 
            Skin="Telerik" AllowAutomaticDeletes="True" 
            DataSourceID="SqlDataSourceQuoteItems" GridLines="None" 
            AllowAutomaticInserts="True" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted">
<MasterTableView DataSourceID="SqlDataSourceQuoteItems" CommandItemDisplay="TopAndBottom" AutoGenerateColumns="False"
                DataKeyNames="id">
<RowIndicatorColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
  
<ExpandCollapseColumn>
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
     <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                        <ItemStyle CssClass="MyImageButton" />
                    </telerik:GridEditCommandColumn>
        <telerik:GridBoundColumn DataField="id" DataType="System.Int32" HeaderText="id" 
            ReadOnly="True" SortExpression="id" UniqueName="id" Visible="False">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="QuoteNo" HeaderText="QuoteNo" 
            SortExpression="QuoteNo" UniqueName="QuoteNo" Visible="False">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Room" HeaderText="Room" 
            SortExpression="Room" UniqueName="Room" ColumnEditorID="GridTextBoxRoom">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Elevation" HeaderText="Elevation" 
            SortExpression="Elevation" UniqueName="Elevation" ColumnEditorID="GridTextBoxElevation">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="PartNo" HeaderText="PartNo" 
            SortExpression="PartNo" UniqueName="PartNo" ColumnEditorID="GridTextBoxPartNo" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Qty" DataType="System.Double" 
            HeaderText="Qty" SortExpression="Qty" UniqueName="Qty" ColumnEditorID="GridNumericQty">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="ListPrice" DataType="System.Double" 
            HeaderText="Price" SortExpression="ListPrice" UniqueName="ListPrice" ColumnEditorID="GridTextBoxListPrice" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="TotalPrice" DataType="System.Double" 
            HeaderText="Total Price" SortExpression="TotalPrice" UniqueName="TotalPrice"  ColumnEditorID="GridTextBoxTotalPrice" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="EachWeight" DataType="System.Double" 
            HeaderText="Weight" SortExpression="EachWeight" UniqueName="EachWeight" ColumnEditorID="GridTextBoxEachWeight" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="TotalWeight" DataType="System.Double" 
            HeaderText="Total Weight" SortExpression="TotalWeight" UniqueName="TotalWeight" ColumnEditorID="GridTextBoxTotalWeight" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="EachCubes" DataType="System.Double" 
            HeaderText="Cubes" SortExpression="EachCubes" UniqueName="EachCubes" ColumnEditorID="GridTextBoxEachCubes" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="TotalCubes" DataType="System.Double" 
            HeaderText="Total Cubes" SortExpression="TotalCubes" UniqueName="TotalCubes" ColumnEditorID="GridTextBoxTotalCubes" ReadOnly="true">
        </telerik:GridBoundColumn>
        <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                        UniqueName="DeleteColumn">
                        <ItemStyle HorizontalAlign="Center" CssClass="MyImageButton" />
                    </telerik:GridButtonColumn>
    </Columns>
     <EditFormSettings ColumnNumber="7" CaptionDataField="PartNo" CaptionFormatString="Edit properties of Product {0}">
        <FormTableItemStyle Wrap="False"></FormTableItemStyle>
                    <FormCaptionStyle CssClass="EditFormHeader"></FormCaptionStyle>
                    <FormMainTableStyle GridLines="None" CellSpacing="0" CellPadding="3" BackColor="White"
                        Width="100%" />
                    <FormTableStyle CellSpacing="0" CellPadding="2" Height="110px" BackColor="White" />
                    <FormTableAlternatingItemStyle Wrap="False"></FormTableAlternatingItemStyle>
                    <EditColumn ButtonType="ImageButton" InsertText="Insert Product" UpdateText="Update record"
                        UniqueName="EditCommandColumn1" CancelText="Cancel edit">
                    </EditColumn>
                    <FormTableButtonRowStyle HorizontalAlign="Right" CssClass="EditFormButtonRow"></FormTableButtonRowStyle>
     </EditFormSettings>
</MasterTableView>
  
 <ClientSettings>
                <ClientEvents OnRowDblClick="RowDblClick" />
            </ClientSettings>
        </telerik:RadGrid>
          
         <telerik:GridTextBoxColumnEditor ID="GridTextBoxRoom" runat="server" TextBoxStyle-Width="25px" />
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxElevation" runat="server" TextBoxStyle-width="25px" />
             <telerik:GridTextBoxColumnEditor ID="GridTextBoxPartNo" runat="server" TextBoxStyle-width="90px" />
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxListPrice" runat="server" TextBoxStyle-width="45px" />
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxTotalPrice" runat="server" TextBoxStyle-width="45px" />
             <telerik:GridTextBoxColumnEditor ID="GridTextBoxEachWeight" runat="server" TextBoxStyle-width="35px" />
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxTotalWeight" runat="server" TextBoxStyle-width="35px" />
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxEachCubes" runat="server" TextBoxStyle-width="40px" />
            <telerik:GridTextBoxColumnEditor ID="GridTextBoxTotalCubes" runat="server" TextBoxStyle-width="40px" />
            <telerik:GridTextBoxColumnEditor ID="GridNumericQty" runat="server" TextBoxStyle-width="35px" />
            <telerik:RadWindowManager ID="RadWindowManager2" runat="server"></telerik:RadWindowManager>
          
        <asp:SqlDataSource ID="SqlDataSourceQuoteItems" runat="server" 
            ConnectionString="Data Source=DWDATA2;Initial Catalog=DWJobInfo;Integrated Security=True" 
            ProviderName="System.Data.SqlClient" 
              
            SelectCommand="SELECT * FROM [tblMetalCabsQuoteItems] WHERE ([QuoteNo] = @QuoteNo) ORDER BY [Room], [Elevation]" 
            DeleteCommand="DELETE FROM tblMetalCabsItems WHERE id = @id">
            <SelectParameters>
                <asp:ControlParameter ControlID="lblQuoteNo" Name="QuoteNo" PropertyName="Text" 
                    Type="String" />
            </SelectParameters>
            <DeleteParameters>
                <asp:Parameter Name="id" Type="Int16" />
            </DeleteParameters>
        </asp:SqlDataSource>

I know I have to add some parameters for the update sqldatasource yet.  But I figured I have to figure out this problem before i can even think about doing that part of it yet.  So, when I click edit and change the values I need to, how do I grab all the values to do some calculations and then to update the database to then refresh the grid?

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 16 Dec 2010, 08:53 AM
Hello Amanda,

The following code snippet shows how to get the edited values.
C#:
protected void RadGrid1_UpdateCommand(object sender, GridCommandEventArgs e)
    {
        GridEditFormItem editItem = (GridEditFormItem)e.Item;
        TextBox txtqty = (TextBox)editItem["Qty"].Controls[0];//accessing TextBox
        int quantity = Convert.ToInt32(txtqty);//getting new value
        // do your calculation
    }

Also you can try the ExtractValuesFromItem method as described in the following documentation :
Updating values in-place and with edit forms

Thanks,
Princy.
0
Amanda
Top achievements
Rank 2
answered on 16 Dec 2010, 03:13 PM
When I run the code you gave me, it errors on the first line.
Unable to cast object of type 'Telerik.Web.UI.GridDataItem' to type 'Telerik.Web.UI.GridEditFormItem'.

So I then tried this:
Dim editItem As GridEditFormItem = DirectCast(TryCast(e.Item, GridDataItem).EditFormItem, GridEditFormItem)

But then I get the error on the next line:
Object reference not set to an instance of an object.

I tried these two things:
Dim txtqty As TextBox = New TextBox
txtqty = DirectCast(editItem.FindControl("GridNumericQty"), TextBox)
Dim txtqty As TextBox = New TextBox
txtqty = DirectCast(editItem("Qty").Controls(0), TextBox)

For each way, I tried both 'Qty' and 'GridNumericQty' and I'm still getting the same error.

Any ideas?
0
Princy
Top achievements
Rank 2
answered on 17 Dec 2010, 06:37 AM
Hello Amanda,

If you are using  'Inplace' editing, please make the following modification in your code to avoid this error.

VB.NET:
Protected Sub RadGrid2_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    Dim editItem As GridEditableItem = DirectCast(e.Item, GridEditableItem)
    Dim txtqty As TextBox = DirectCast(editItem("Qty").Controls(0), TextBox)
    'accessing TextBox
End Sub

Thanks,
Princy.
Tags
Grid
Asked by
Amanda
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Amanda
Top achievements
Rank 2
Share this question
or