RadGrid Editable Columns - Update Function Not Working

5 posts, 1 answers
  1. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 04 Apr 2012 Link to this post

    HELP! (please)
    I have a RadGrid.  I have two updatable columns.  When I try to update, nothing happens.

    Here is my front end....
    <telerik:RadGrid Width="550px" ID="RadGrid1" AllowMultiRowEdit="True" runat="server"
        OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemDataBound="RadGrid1_ItemDataBound">
        <MasterTableView DataKeyNames="HandlingPriceEstimationID" AutoGenerateColumns="False" EditMode="InPlace"
            CommandItemDisplay="Bottom">
            <Columns>
                <telerik:GridBoundColumn ReadOnly="true" DataField="HandlingPriceEstimationID" UniqueName="HandlingPriceEstimationID"
                    Visible="False">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn ReadOnly="true" DataField="UoMCode" UniqueName="UoMCode"
                    Visible="False">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn ReadOnly="true" DataField="UoMName" UniqueName="UoMName"
                    HeaderText="UOM">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn ReadOnly="true" DataField="DimCode" UniqueName="DimCode"
                    HeaderText="IBU">
                </telerik:GridBoundColumn>
                <telerik:GridNumericColumn ReadOnly="False" DataField="FirstItem" UniqueName="FirstItem"
                    HeaderText="First Item" />
                <telerik:GridNumericColumn ReadOnly="False" DataField="RemainItem" UniqueName="RemainItem"
                    HeaderText="Remaining Item" />
                <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
            </Columns>
            <CommandItemTemplate>
                <asp:Button runat="server" ID="UpdateAll" Text="Update" CommandName="UpdateAll" />
            </CommandItemTemplate>
        </MasterTableView>
    </telerik:RadGrid>

    Here is my back end...  (my data source is in a data access layer)
    protected void RadGrid1_NeedDataSource(Object source, GridNeedDataSourceEventArgs e)
            {
                CP_PortalDb db = new CP_PortalDb();
                DataTable dataTable = new DataTable();
                dataTable = db.StoredProcedures.stp_HandlingPriceEstimationGETALL_Command();
     
                string prevHPEID = "";
     
                foreach (DataRow dRow in dataTable.Rows)
                {
                    string newHPEID = dataTable.Rows[0]["HandlingPriceEstimationID"].ToString();
     
                    if (prevHPEID != newHPEID)
                    {
                        dRow["HandlingPriceEstimationID"] = Convert.ToInt32(dataTable.Rows[0]["HandlingPriceEstimationID"]);
                        dRow["DimCode"] = dataTable.Rows[0]["DimCode"].ToString();
                        dRow["UoMCode"] = dataTable.Rows[0]["UoMCode"].ToString();
                        dRow["UoMName"] = dataTable.Rows[0]["UoMName"].ToString();
                        dRow["FirstItem"] = dataTable.Rows[0]["FirstItem"].ToString();
                        dRow["RemainItem"] = dataTable.Rows[0]["RemainItem"].ToString();
                    }
                    prevHPEID = newHPEID;
                }
     
                RadGrid1.DataSource = dataTable;
            }
     
            protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
            {
                CP_PortalDb db = new CP_PortalDb();
     
                if (e.CommandName == "UpdateAll")
                {
                    if (RadGrid1.EditIndexes.Count == 0)
                    {
                        return;
                    }
     
                    foreach (GridDataItem editedItem in RadGrid1.EditItems)
                    {
                        Hashtable newValues = new Hashtable();
                        //The GridTableView will fill the values from all editable columns in the hash
                        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem );
     
                        int returnedID = Convert.ToInt32(editedItem.GetDataKeyValue("HandlingPriceEstimationID"));
                         
     
                        if (e.Item is GridDataItem)
                        {
                            GridDataItem fItem = (GridDataItem) e.Item;
                            GridDataItem rItem = (GridDataItem) e.Item;
     
                            double fItemHandPrice = Convert.ToDouble(fItem["FirstItem"]);
                            double rItemHandPrice = Convert.ToDouble(rItem["RemainingItem"]);
     
                            db.StoredProcedures.stp_MaterialHandlingEstimationUPDATE_Command(returnedID, fItemHandPrice, rItemHandPrice);
                        }
     
                        editedItem.Edit = false;
     
                    }
                    e.Item.OwnerTableView.Rebind();
                }
            }
     
            protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem && e.Item.IsInEditMode)
                {
                    GridDataItem dataItem = e.Item as GridDataItem;
                    //Hides the Update button for each edit form
                    dataItem["EditCommandColumn"].Controls[0].Visible = false;
                }
            }


    My update is a stored procedure that requires three Parameters... the ID of the edited row, the value of the edited column, or columns.
    I can't get it to do the actual update.  I've looked at samples, and tried to match the examples (with  minor changes for my stored procedures - and population).  However, I'm still not able to get the update working.

    If you can pinpoint my error and help me correct it, I'd be very much obliged.

    Thanks, in advance. :-)
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 04 Apr 2012 Link to this post

    Hi Mark,

    Try doing the update functionality in the GridEditableItem loop. Please take a look into the following code.

    C#:
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "UpdateAll")
        {
            foreach (GridEditableItem editedItem in RadGrid1.EditItems)
            {
                //your code for updating
            }
        }
    }


    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 04 Apr 2012 Link to this post

    Thank you for your quick response...
    I tried this within the loop...
    protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
    {
        CP_PortalDb db = new CP_PortalDb();
     
        if (e.CommandName == "UpdateAll")
        {
            if (RadGrid1.EditIndexes.Count == 0)
            {
                return;
            }
     
            foreach (GridDataItem editedItem in RadGrid1.EditItems)
            {
                Hashtable newValues = new Hashtable();
                //The GridTableView will fill the values from all editable columns in the hash
                e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem );
     
                db.StoredProcedures.stp_MaterialHandlingEstimationUPDATE_Command(
                    Convert.ToDouble(newValues["FirstItem"]), Convert.ToDouble(newValues["RemainItem"]),
                    Convert.ToInt32(editedItem.GetDataKeyValue("HandlingPriceEstimationID")));
     
                editedItem.Edit = false;
     
            }
            //e.Item.OwnerTableView.Rebind();
            RadGrid1.Rebind();
        }
    }


    And it still does not update.  Suggestions?

    By the way, I took my code from this example: http://www.telerik.com/help/aspnet-ajax/grid-performing-batch-updates.html
  5. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 04 Apr 2012 Link to this post

    Also, a note to mention - the values ARE present when hitting my stored procedure...
    if I step through the code, and hover over the parameters, the values show up... as if they ARE going to update... but it just DOESN'T.

    I'm confused.
  6. Mark
    Mark avatar
    25 posts
    Member since:
    Feb 2012

    Posted 04 Apr 2012 Link to this post

    OK!  I GOT IT!!!
    The problem wasn't with my RadGrid Coding... it was with my code for my Stored procedure.
    cmd.ExecuteNonQuery();

    Sometimes, you just need to step away for a moment and come back with a fresh set of eyes.

    Thank you, anyway, for your suggestion. :-)
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017