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

Performing Batch Updates

1 Answer 67 Views
Grid
This is a migrated thread and some comments may be shown as answers.
berto
Top achievements
Rank 1
berto asked on 24 Nov 2012, 08:35 PM
Hello,

I tried to implement batch updates. In my first try I used following article:

http://www.telerik.com/help/aspnet-ajax/grid-performing-batch-updates.html

I only used different table from Northwind database and used checkboxes for update.

Here you can see my code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
        </telerik:RadScriptManager>
 
        <telerik:radgrid ID="RadGrid1" runat="server" AllowMultiRowEdit="True" DataSourceID="SqlDataSource1"
  OnItemCommand="RadGrid1_ItemCommand"
    OnItemDataBound="RadGrid1_ItemDataBound">
  <MasterTableView DataKeyNames="ProductID" AutoGenerateColumns="false" EditMode="InPlace"
    CommandItemDisplay="TopAndBottom">
    <Columns>
      <telerik:gridboundcolumn ReadOnly="true" DataField="ProductID" UniqueName="ProductID"
        HeaderText="ProductID">
      </telerik:gridboundcolumn>
      <telerik:gridboundcolumn ReadOnly="true" DataField="ProductName" UniqueName="ProductName"
            HeaderText="ProductName">
      </telerik:gridboundcolumn>
      <telerik:GridCheckBoxColumn DataField="Discontinued" DefaultInsertValue=""
                    HeaderText="Discontinued" UniqueName="Discontinued" DataType="System.int16">
      </telerik:GridCheckBoxColumn>
      <telerik:grideditcommandcolumn UniqueName="EditCommandColumn" />
    </Columns>
    <CommandItemTemplate>
      <asp:Button runat="server" ID="UpdateAll" Text="Update All" CommandName="UpdateAll" /></CommandItemTemplate>
  </MasterTableView>
</telerik:radgrid>
<asp:SqlDataSource ID="SqlDataSource1"
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [Discontinued] FROM [Products]"
  runat="server"></asp:SqlDataSource>
    </div>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "UpdateAll")
        {
            foreach (GridEditableItem 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);
                SqlDataSource1.UpdateCommand = String.Format("Update Products SET Discontinued='{0}' WHERE ProductID='{1}'"
                    , newValues["Discontinued"], editedItem.GetDataKeyValue("ProductID").ToString());
                SqlDataSource1.Update();
                editedItem.Edit = false;
            }
        }
        RadGrid1.Rebind();
    }
 
    protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.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;
        }
    }
}

It seems that batch update does not work. When I click edit I can change checkbox value but when I click edit on second record change I did disappers. somhow it is not remembered. Can you fix this problem, please?

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 28 Nov 2012, 01:08 PM
Hello Andrzej,

When the edit form is opened RadGrid is being recreated thus temporary data is being lost. However you can workaround this issue by storing the temporary changes and reapply them in ItemDataBound. On how to implement this please review the attached project which I created using your code.

All the best,
Angel Petrov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
berto
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or