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

RadGrid Editable Columns - Update Function Not Working

4 Answers 234 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 04 Apr 2012, 02:44 PM
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. :-)

4 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Apr 2012, 04:19 PM
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.
0
Mark
Top achievements
Rank 1
answered on 04 Apr 2012, 04:24 PM
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
0
Mark
Top achievements
Rank 1
answered on 04 Apr 2012, 04:27 PM
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.
0
Mark
Top achievements
Rank 1
answered on 04 Apr 2012, 06:54 PM
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. :-)
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mark
Top achievements
Rank 1
Share this question
or