Update All Records in Code Behind

2 posts, 0 answers
  1. Voya Developer
    Voya Developer avatar
    3 posts
    Member since:
    Oct 2010

    Posted 07 Jan 2013 Link to this post

    Community,

    I have a RadGrid that I define my columns. Most of them are visible and read only; however, a few are updated. I also have a few columns that are not visible. I use these in my SQL update as my primary key. When you enter the RadGrid I automatically enable edit in every row of the entire grid for the editable columns. When a user is done doing a bulk update, they click my Update All button and the following code is ran:

    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "EditAll")
            {
                foreach (GridItem item in RadGrid1.MasterTableView.Items)
                {
                    if (item is GridEditableItem)
                    {
                        GridEditableItem editableItem = item as GridDataItem;
                        editableItem.Edit = true;
                    }
                }
            }
            if (e.CommandName == "UpdateAll")
            {
                //-- foreach (GridEditableItem editedItem in RadGrid1.EditItems)
                foreach (GridDataItem dataItem in RadGrid1.MasterTableView.Items)
                {
                    Hashtable newValues = new Hashtable();
                    e.Item.OwnerTableView.ExtractValuesFromItem(newValues, dataItem);
                    SqlDataSource1.UpdateCommand = String.Format("UPDATE MY_TABLE SET AOScan_Num='{0}', PLANNED_REMEDIATION_STATUS_ID='{1}', " +
                        "ServiceNow_Num='{2}', TICKET_CREATION_DATE='{3}' WHERE QID='{4}' AND IP='{5}' AND PORT='{6}'",
                        newValues["AOScan_Num"], 
                        newValues["PLANNED_REMEDIATION_STATUS_ID"], 
                        newValues["ServiceNow_Num"], 
                        newValues["TICKET_CREATION_DATE"],
                        dataItem["QID"].Text, 
                        dataItem["IP"].Text, 
                        Convert.ToInt32(dataItem["PORT"].Text));
                    SqlDataSource1.Update();
                    dataItem.Edit = false;
                }
            }
            RadGrid1.Rebind(); 
        }

    The problem I am running into is, when my code executes the SQL it returns the correct values from RadGrid for each row; however, the fields that are not visible and not editable are empty instead of holding the correct value. I was hoping someone has an idea of what might be going on.

    Kind Regards,

    Ben
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Jan 2013 Link to this post

    Hi,

    Since you have set the column's ReadOnly property as 'True', it will not rendered as TextBox in edit mode. One suggestion is to set the ReadOnly Column as the DataKeyName of the gridtableview and access the value as shown below.
    aspx:
    <telerik:RadGrid ID="RadGrid1" AllowMultiRowEdit="true" runat="server">
      <MasterTableView EditMode="InPlace" DataKeyNames="ID">
    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
     foreach (GridItem item in RadGrid1.EditItems)
     {
       GridEditableItem editedItem = (GridEditableItem)item;
       string value = editedItem.GetDataKeyValue("ID").ToString();
     }
    }

    Thanks,
    Shinu.
Back to Top