AUTHOR: Attila Antal
DATE POSTED: November 09, 2018
For bulk data-editing, RadGrid has a built-in functionality called Batch Editing which is using client-side operation. In some scenarios, one would like to use bulk editing using server-side operations.
The following approach uses InPlace EditMode (server-side data editing) to do bulk updates on grid items.
This scenario, covers the following options:
<script type=
"text/javascript"
>
// Client-Side logic executed when the HeaderCheckBox selection is toggled
function
checkAllChanged(sender, args) {
// jQuery code to find all the checkboxes
var
checkboxes = $(
'.RadGrid .rgMasterTable .rgEditRow td .RadCheckBox'
).each(
(e) {
// this.control casts the jQuery object to RadCheckBox object, then using its API, setting its checked state
this
.control.set_checked(sender.get_checked());
})
}
</script>
<%--Enable AJAX for all control by wrapping the controls in a Panel and add the panel to the AjaxSettings of RadAjaxManager--%>
<
telerik:RadAjaxManager
ID
=
"RadAjaxManager1"
runat
"server"
DefaultLoadingPanelID
"RadAjaxLoadingPanel1"
AjaxSettings
telerik:AjaxSetting
AjaxControlID
"Panel1"
UpdatedControls
telerik:AjaxUpdatedControl
ControlID
/>
</
telerik:RadAjaxLoadingPanel
Skin
"Default"
></
div
asp:Panel
<%--Update Button--%>
telerik:RadButton
"btnUpdateAll"
Text
"Update All"
OnClick
"btnUpdateAll_Click"
<%--Update Selected Button--%>
"btnUpdateSelected"
"Update Selected"
"btnUpdateSelected_Click"
br
asp:Label
"Label1"
"Action:"
telerik:RadGrid
"RadGrid1"
AllowPaging
"True"
AutoGenerateColumns
"false"
PageSize
"5"
AllowMultiRowEdit
"true"
OnNeedDataSource
"RadGrid1_NeedDataSource"
OnInsertCommand
"RadGrid1_InsertCommand"
OnUpdateCommand
"RadGrid1_UpdateCommand"
OnDeleteCommand
"RadGrid1_DeleteCommand"
OnPreRender
"RadGrid1_PreRender"
MasterTableView
CommandItemDisplay
"Top"
DataKeyNames
"OrderID"
InsertItemDisplay
InsertItemPageIndexAction
"ShowItemOnLastPage"
EditMode
"InPlace"
RetainExpandStateOnRebind
CommandItemSettings
ShowAddNewRecordButton
Columns
telerik:GridBoundColumn
DataField
DataType
"System.Int32"
FilterControlAltText
"Filter OrderID column"
HeaderText
ReadOnly
SortExpression
UniqueName
telerik:GridDateTimeColumn
"OrderDate"
"System.DateTime"
"Filter OrderDate column"
"ShipName"
"Filter ShipName column"
<%--Template column is best choice for custom implementations--%>
telerik:GridTemplateColumn
"TemplateCheckboxColumn"
"IsChecked"
HeaderTemplate
<%--RadCheckBox that will appear in the Column header, bound to OnClientCheckedChanged client-side event--%>
telerik:RadCheckBox
"CheckAllCheckBox"
AutoPostBack
OnClientCheckedChanged
"checkAllChanged"
EditItemTemplate
<%--RadCheckBox that will appear in each row--%>
"RowCheckBox"
Checked='<%# Bind("IsChecked") %>'></
<%--Update button that will appear in each row--%>
"bntUpdate"
"Update"
CommandName
C#
public
DataTable SessionDataSource
{
get
string
sessionKey =
"SessionDataSource"
;
if
(Session[sessionKey] ==
null
|| !IsPostBack)
Session[sessionKey] = OrdersTable();
return
(DataTable)Session[sessionKey];
// READ (data binding)
protected
void
RadGrid1_NeedDataSource(
object
sender, GridNeedDataSourceEventArgs e)
RadGrid1.DataSource = SessionDataSource;
// UPDATE
RadGrid1_UpdateCommand(
sender, GridCommandEventArgs e)
GridEditableItem editedItem = e.Item
as
GridEditableItem;
(!UpdateRow(editedItem))
e.Canceled =
true
private
bool
UpdateRow(GridEditableItem editableItem)
//Locate the changed row in the DataSource
DataRow[] changedRows = SessionDataSource.Select(
.Format(
"OrderID = {0}"
, editableItem.GetDataKeyValue(
)));
(changedRows.Length != 1)
.Label1.Text +=
"Unable to locate the Order for updating."
false
//Update new values
Hashtable newValues =
new
Hashtable();
editableItem.OwnerTableView.ExtractValuesFromItem(newValues, editableItem);
changedRows[0].BeginEdit();
try
foreach
(DictionaryEntry entry
in
newValues)
changedRows[0][(
)entry.Key] = entry.Value;
changedRows[0].EndEdit();
catch
(Exception ex)
changedRows[0].CancelEdit();
Label1.Text +=
"Unable to update Orders. Reason: {0}"
, ex.Message);
DataTable OrdersTable()
DataTable dt =
DataTable();
dt.Columns.Add(
DataColumn(
,
typeof
(
int
(DateTime)));
"Freight"
decimal
"ShipCountry"
dt.PrimaryKey =
DataColumn[] { dt.Columns[
] };
for
i = 0; i < 10; i++)
index = i + 1;
DataRow row = dt.NewRow();
row[
] = index;
] =
DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index);
] = index * 0.1 + index * 0.01;
"Name "
+ index;
"Country "
] = i % 3 == 0;
dt.Rows.Add(row);
dt;
// Putt all items in edit mode
RadGrid1_PreRender(
sender, EventArgs e)
(GridItem item
RadGrid1.MasterTableView.Items)
(item
is
GridEditableItem)
GridEditableItem editableItem = item
GridDataItem;
editableItem.Edit =
RadGrid1.Rebind();
// Button to update all records
btnUpdateAll_Click(
// Loop through the grid items
(GridEditableItem item
RadGrid1.Items)
// Check whether the item is in edit mode
(item.IsInEditMode)
// update the item
UpdateRow(item);
btnUpdateSelected_Click(
// get reference to the checkbox from the Template column
RadCheckBox rCheckBox = item[
].FindControl(
)
RadCheckBox;
// Check if the checkbox is checked
(rCheckBox.Checked ==
Public
Property
SessionDataSource
As
DataTable
Get
Dim
sessionKey
String
If
Session(sessionKey)
Is
Nothing
OrElse
Not
IsPostBack
Then
Session(sessionKey) = OrdersTable()
End
Return
CType
(Session(sessionKey), DataTable)
Protected
Sub
ByVal
sender
Object
e
GridNeedDataSourceEventArgs)
RadGrid1.DataSource = SessionDataSource
GridCommandEventArgs)
editedItem
GridEditableItem = TryCast(e.Item, GridEditableItem)
UpdateRow(editedItem)
True
Private
Function
UpdateRow(
editableItem
Boolean
changedRows
DataRow() = SessionDataSource.[
Select
](
)))
changedRows.Length <> 1
Me
False
newValues
Hashtable =
New
Hashtable()
editableItem.OwnerTableView.ExtractValuesFromItem(newValues, editableItem)
changedRows(0).BeginEdit()
Try
For
Each
entry
DictionaryEntry
In
changedRows(0)(
CStr
(entry.Key)) = entry.Value
Next
changedRows(0).EndEdit()
Catch
ex
Exception
changedRows(0).CancelEdit()
, ex.Message)
OrdersTable()
dt
DataTable =
DataTable()
GetType
Integer
(DateTime)))
Decimal
DataColumn() {dt.Columns(
)}
i
= 0
To
10 - 1
index
= i + 1
row
DataRow = dt.NewRow()
row(
) = index
) =
DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0).AddHours(index)
) = index * 0.1 + index * 0.01
& index
) = i
Mod
3 = 0
dt.Rows.Add(row)
EventArgs)
item
GridItem
RadGrid1.MasterTableView.Items
TypeOf
GridEditableItem
GridEditableItem = TryCast(item, GridDataItem)
RadGrid1.Rebind()
RadGrid1.Items
item.IsInEditMode
UpdateRow(item)
rCheckBox
RadCheckBox = TryCast(item(
).FindControl(
), RadCheckBox)
rCheckBox.Checked =
Resources Buy Try