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

Update All Records in Code Behind

1 Answer 75 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Voya Developer
Top achievements
Rank 1
Voya Developer asked on 07 Jan 2013, 06:48 PM
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

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 08 Jan 2013, 05:40 AM
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.
Tags
General Discussions
Asked by
Voya Developer
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or