This example demonstrates how to move selected rows from one grid to another on a custom button click and update data sources on the server.
Create two grid controls and add custom moving buttons to the page. Use the client-side PerformCallback method to send custom callbacks to the server when a user clicks the corresponding button. Based on an action, create a command and pass it as a parameter.
var command = null;
function AddSelectedRows() {
command = 'addSelectedRows';
UpdateTargetGrid();
}
function UpdateTargetGrid() {
if (command != null)
targetGrid.PerformCallback(command);
else {
targetGrid.Refresh();
}
}
On the server, the PerformCallback
method raises the CustomCallback event. In the CustomCallback
event handler, call the GetSelectedFieldValues method to obtain the selected rows. Update the data sources of the grids based on the command passed to the server.
protected void TargetGrid_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e) {
rowValues = new List<object>();
switch (e.Parameters) {
case "addSelectedRows":
rowValues = SourceGrid.GetSelectedFieldValues(fieldNames);
var categoryIDs = new StringBuilder();
for (int i = 0; i < rowValues.Count(); i++) {
var categoryID = (rowValues[i] as object[])[0];
if (i < rowValues.Count() - 1)
categoryIDs.AppendFormat("{0}, ", categoryID);
else
categoryIDs.Append(categoryID);
}
if (categoryIDs.Length > 0) {
SourceDS.DeleteCommand = string.Format("DELETE FROM [Categories] WHERE [CategoryID] IN ({0})", categoryIDs);
SourceDS.Delete();
foreach (object[] rowValue in rowValues) {
TargetDS.InsertCommand = string.Format(
"INSERT INTO [CategoriesUpdated] ([CategoryID], [CategoryName], [Description]) VALUES ({0}, '{1}', '{2}')",
rowValue[0], rowValue[1], rowValue[2]);
TargetDS.Insert();
}
TargetGrid.DataBind();
}
break;
// ...
}
}
- Default.aspx (VB: Default.aspx)
- Default.aspx.cs (VB: Default.aspx.vb)