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

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

14 Answers 570 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Raul
Top achievements
Rank 1
Raul asked on 10 Dec 2013, 10:35 PM
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,

14 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 11 Dec 2013, 08:37 AM
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
0
Smitha
Top achievements
Rank 1
answered on 05 Mar 2014, 12:47 AM
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?
0
Princy
Top achievements
Rank 2
answered on 05 Mar 2014, 06:08 AM
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
0
Smitha
Top achievements
Rank 1
answered on 05 Mar 2014, 05:15 PM
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
            }

        
        }
    }

0
Smitha
Top achievements
Rank 1
answered on 05 Mar 2014, 05:37 PM
Please find the attached image for how the hash values are shown in the debug mode.
0
Princy
Top achievements
Rank 2
answered on 06 Mar 2014, 04:08 AM
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
0
Princy
Top achievements
Rank 2
answered on 06 Mar 2014, 07:18 AM
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
0
Smitha
Top achievements
Rank 1
answered on 06 Mar 2014, 05:23 PM
Hi Princy,
  I am still not getting the value of the dropdown selection. I tried both your suggestions
Smitha.
0
Princy
Top achievements
Rank 2
answered on 07 Mar 2014, 06:05 AM
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
0
Peter
Top achievements
Rank 1
answered on 13 Apr 2015, 09:35 AM
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. 
0
Maria Ilieva
Telerik team
answered on 16 Apr 2015, 08:31 AM
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.

 
0
Jaya
Top achievements
Rank 1
answered on 22 Apr 2015, 06:20 AM

Hi

Admin

Can you solve this 

http://www.telerik.com/forums/how-to-designed-the-aspx-page-and-open-rad-window
0
Dipak
Top achievements
Rank 1
answered on 07 Apr 2017, 05:17 AM
Hi

I'm having the same problem. i bind dropdown with sqldatasource.and the items are showing as my data. but when try to find that dropdown with BatchEditCommand event its says items count  0. i don't understand the problem. please help..
0
Prashant
Top achievements
Rank 1
answered on 15 May 2018, 08:29 AM

I create batch edit radgrid programmatically  and i have gridtemplatecolumn in which i bind two templates item template and edititemtemplate. In Item template, i attached label that shows selected value of dropdownlist. In edit item template, i attached dropdownlist. when BatchEditcommand event fires, i found no commands in Command list. If removed this dropdown gridtemplate column then i found command list with values at insert.

FYI, I use itemplate to create template server side.Please help

Tags
Grid
Asked by
Raul
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Smitha
Top achievements
Rank 1
Peter
Top achievements
Rank 1
Maria Ilieva
Telerik team
Jaya
Top achievements
Rank 1
Dipak
Top achievements
Rank 1
Prashant
Top achievements
Rank 1
Share this question
or