Commit RadGrid data changes using EditMode="Batch" manually (not using the SqlDataSource)

13 posts, 1 answers
  1. Raul
    Raul avatar
    5 posts
    Member since:
    Dec 2013

    Posted 10 Dec 2013 Link to this post

    Hello,

    I am using theEditMode ="Batch"  RadGrid's  feature to modify Data and send those changes to the database, I am populating the RadGrid from from a DataTable using the event OnNeedDataSource,

    My question is, How I could send/commit the changes to the database manually? (in the examples seems to be automatic using the SqlDataSource for CRUD operations, if you could provide a code sample would be very appreciate it,

    Thanks in advance,
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 11 Dec 2013 Link to this post

    Hi Raul,

    You can do the operations manually in the code behind in the BatchEditCommand event. Below is the code to update, similarly you can do for insert and delete:

    C#:
    protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
    {
        foreach (GridBatchEditingCommand command in e.Commands)
        {
           // For Update
            if ((command.Type == GridBatchEditingCommandType.Update))
            {
                Hashtable newValues = command.NewValues;
                Hashtable oldValues = command.OldValues;
                string ID = newValues["ID"].ToString();
                string Name = newValues["Name"].ToString();       
              // Code to Update to DB
            }
        }
    }

    Thanks,
    Princy
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Smitha
    Smitha avatar
    7 posts
    Member since:
    Mar 2014

    Posted 04 Mar 2014 in reply to Princy Link to this post

    Hashtable newValues = command.NewValues;

    When I do this way, the NewValues have the column names, but the value on those is empty. Why could that be?
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Mar 2014 in reply to Smitha Link to this post

    Hi Smitha,

    I'm not able to replicate the issue. Please take a look at the sample code and attached screenshot. Provide your code snippet for further help.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource" OnBatchEditCommand="RadGrid1_BatchEditCommand">
        <MasterTableView EditMode="Batch" CommandItemDisplay="Top" DataKeyNames="OrderID">
            <BatchEditingSettings OpenEditingEvent="Click" EditType="Cell" />
            <Columns>
                <telerik:GridCheckBoxColumn UniqueName="IsTrue" HeaderText="IsTrue" DataField="IsTrue">
                </telerik:GridCheckBoxColumn>
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID"/>
                <telerik:GridBoundColumn DataField="ShipName" HeaderText="ShipName" UniqueName="ShipName" />
            </Columns>
        </MasterTableView
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
      foreach (GridBatchEditingCommand command in e.Commands)
      {
        if (command.Type == GridBatchEditingCommandType.Update)
        {
            Hashtable newValues = command.NewValues;         
            Hashtable oldValues = command.OldValues;
            string OrderID = newValues["OrderID"].ToString();
            string ShipName = newValues["ShipName"].ToString();         
        }
      }
    }

    Thanks,
    Princy
  6. Smitha
    Smitha avatar
    7 posts
    Member since:
    Mar 2014

    Posted 05 Mar 2014 in reply to Princy Link to this post

    Hi Princy,

    This is my aspx code. I have 2 combobox controls in the 2 columns. And I am not able to get the values entered in those cells and so the database insert fails with the error message that the value is null .

    <telerik:RadGrid ID="RadGridPCBA"  ClipboardPasteMode="Cells,AllSelectedCells" runat="server"  AllowPaging="True" AutoGenerateColumns="False"  GridLines="None"
                      OnBatchEditCommand="RadGridPCBA_BatchEditCommand"
                      OnItemDeleted="RadGridPCBA_ItemDeleted" OnItemInserted="RadGridPCBA_ItemInserted" OnItemUpdated="RadGridPCBA_ItemUpdated"
                      OnPreRender="RadGridPCBA_PreRender" PageSize="10" Skin="Default" Width="750px" OnNeedDataSource="GetPCBAData" >
                     <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="TopAndBottom" EditMode="Batch" HorizontalAlign="NotSet" >
                         <BatchEditingSettings EditType="Cell" />
                         <SortExpressions>
                             <telerik:GridSortExpression FieldName="pcba_ID" SortOrder="Descending" />
                         </SortExpressions>
                         <Columns>
                             <telerik:GridTemplateColumn DataField="Site_ID" HeaderStyle-Width="180px" HeaderText="Site" UniqueName="Site_ID" >
                                 <ItemTemplate>
                                     <%# Eval("Site_Name") %>
                                 </ItemTemplate>
                                 <EditItemTemplate>
                                     <telerik:RadDropDownList ID="SiteNameDropDown" runat="server" DataSourceID="SqlDataSourceSite" DataTextField="Site_Name" DataValueField="Site_ID">
                                     </telerik:RadDropDownList>
                                 </EditItemTemplate>
                             </telerik:GridTemplateColumn>
                             <telerik:GridTemplateColumn DataField="CM_ID" HeaderStyle-Width="180px" HeaderText="CM" UniqueName="CM_ID">
                                 <ItemTemplate>
                                     <%# Eval("CM_Name") %>
                                 </ItemTemplate>
                                 <EditItemTemplate>
                                     <telerik:RadDropDownList ID="CMNameDropDown" runat="server" DataSourceID="SqlDataSourceCM" DataTextField="CM_Name" DataValueField="CM_ID">
                                     </telerik:RadDropDownList>
                                 </EditItemTemplate>
                             </telerik:GridTemplateColumn>
                             <telerik:GridBoundColumn DataField="pcba_ID" HeaderStyle-Width="210px" HeaderText="ID" SortExpression="pcba_ID" UniqueName="pcba_ID" Visible="false">
                             </telerik:GridBoundColumn>
                             <telerik:GridButtonColumn ButtonType="ImageButton" CommandName="Delete" ConfirmDialogType="RadWindow" ConfirmText="Delete this row?" ConfirmTitle="Delete" HeaderStyle-Width="50px" HeaderText="Delete" Text="Delete" UniqueName="DeleteColumn">
                             </telerik:GridButtonColumn>
                         </Columns>
                     </MasterTableView>
                 </telerik:RadGrid>


    Here is my C# code.
     protected void RadGridPCBA_BatchEditCommand(object sender, Telerik.Web.UI.GridBatchEditingEventArgs e)
        {
            SavedChangesList.Visible = true;
            foreach (GridBatchEditingCommand command in e.Commands)
            {
                // For Update        

                if ((command.Type == GridBatchEditingCommandType.Insert))
                {
                    Hashtable newValues = command.NewValues;
                    Hashtable oldValues = command.OldValues;
                    string ID = newValues["ID"].ToString();
                    string Name = newValues["Name"].ToString();
                    // Code to Update to DB
                }

            
            }
        }

  7. Smitha
    Smitha avatar
    7 posts
    Member since:
    Mar 2014

    Posted 05 Mar 2014 in reply to Smitha Link to this post

    Please find the attached image for how the hash values are shown in the debug mode.
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 05 Mar 2014 in reply to Smitha Link to this post

    Hi Smitha,

    When using template columns please try the following code snippet.

    C#:
    protected void RadGridPCBA_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
    {   
      foreach (GridBatchEditingCommand command in e.Commands)
      {
      if ((command.Type == GridBatchEditingCommandType.Insert))
        {
          Hashtable newValues = command.NewValues;
          Hashtable oldValues = command.OldValues;
          RadDropDownList ddlDesc = RadGridPCBA.FindControl(RadGridPCBA.MasterTableView.ClientID + "_Site_ID").FindControl("SiteNameDropDown") as RadDropDownList;
          string text = ddlDesc.SelectedText;
          string id = ddlDesc.SelectedValue;     
        }       
      }
    }

    Thanks,
    Princy
  9. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 06 Mar 2014 in reply to Smitha Link to this post

    Hi Smitha,

    Your way of accessing is correct. Please take a look at the sample code snippet.The NewValues access the DataField of the column.

    ASPX:
    <telerik:GridTemplateColumn DataField="CM_ID" HeaderText="CM" UniqueName="CM_ID">
        <ItemTemplate>
            <%# Eval("CM_Name") %>
        </ItemTemplate>
        <EditItemTemplate>
            <telerik:RadDropDownList ID="CMNameDropDown" runat="server" DataSourceID="SqlDataSourceCM" DataTextField="CM_Name" DataValueField="CM_ID">
            </telerik:RadDropDownList>
        </EditItemTemplate>
    </telerik:GridTemplateColumn>

    C#:
    if (command.Type == GridBatchEditingCommandType.Insert)
    {
      Hashtable newValues = command.NewValues;
      string CustomerID = newValues["CM_ID"].ToString();
    }

    You can disregard the previous post.
    Thanks,
    Princy
  10. Smitha
    Smitha avatar
    7 posts
    Member since:
    Mar 2014

    Posted 06 Mar 2014 in reply to Princy Link to this post

    Hi Princy,
      I am still not getting the value of the dropdown selection. I tried both your suggestions
    Smitha.
  11. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 07 Mar 2014 in reply to Smitha Link to this post

    Hi Smitha,

    Unfortunately, I'm not able to replicate the issue at my end. Here is the sample code snippet i tried.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="false" OnNeedDataSource="RadGrid1_NeedDataSource"
        OnBatchEditCommand="RadGrid1_BatchEditCommand" AutoGenerateColumns="false">
        <MasterTableView EditMode="Batch" CommandItemDisplay="Top" DataKeyNames="OrderID">
            <BatchEditingSettings OpenEditingEvent="Click" EditType="Cell" />
            <Columns>          
                <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" ReadOnly="true" />       
                <telerik:GridTemplateColumn DataField="EmployeeID" HeaderText="ShipName" UniqueName="ShipName">
                    <ItemTemplate>
                        <%# Eval("ShipName")%>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <telerik:RadDropDownList ID="RadDropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="ShipName" DataValueField="EmployeeID">
                        </telerik:RadDropDownList>
                    </EditItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
     {
       RadGrid1.DataSource = GetDataTable("SELECT * FROM Orders");          
     }
    protected void RadGrid1_BatchEditCommand(object sender, GridBatchEditingEventArgs e)
        foreach (GridBatchEditingCommand command in e.Commands)
        {
           if (command.Type == GridBatchEditingCommandType.Insert)
             {
               Hashtable newValues = command.NewValues;        
               string ID = newValues["EmployeeID"].ToString();
             }
         }
    }
    public DataTable GetDataTable(string query)
    {
      String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
      SqlConnection conn = new SqlConnection(ConnString);
      SqlDataAdapter adapter = new SqlDataAdapter();
      adapter.SelectCommand = new SqlCommand(query, conn); 
      DataTable myDataTable = new DataTable();
         conn.Open();
           try
           {
           adapter.Fill(myDataTable);
           }
           finally
           {
           conn.Close();
           }
       return myDataTable;
      }

    Thanks,
    Princy
  12. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 13 Apr 2015 in reply to Princy Link to this post

    If I'm using an EntityDataSource with AutomaticUpdates enabled, how do I *set* a value. As I'm trying to use a Template Column (because I can't work out how to have an empty/null item in a GridDropDownColumn), with an empty item, I need to then need to manually set the value on the Entity. I could use EntityDataSource_Updating, but I don't know what value to use each time. 
  13. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 16 Apr 2015 Link to this post

    Hi Richard,

    I would suggest you to review the forum thread below that discuss similar matter. See the approach provided there and verify if it helps:
    http://www.telerik.com/forums/binding-with-entity-framework


    Regards,
    Maria Ilieva
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  14. Jaya
    Jaya avatar
    81 posts
    Member since:
    Mar 2011

    Posted 22 Apr 2015 in reply to Princy Link to this post


    Hi

    Admin

    Can you solve this 

    http://www.telerik.com/forums/how-to-designed-the-aspx-page-and-open-rad-window
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017