WebGrid.NET provides a new server-side event called OnBatchUpdate that you can handle to implement your own custom business logic.
In this topic, you will learn how to use Custom Object with Batch Update.
To use Custom Object with Batch Update
- First, you need to create DataLayer.cs in App_Code.
C# Copy Code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.OleDb; using System.Collections.Generic; namespace CustomObjects { [System.ComponentModel.DataObject] public class DataLayer { private static OleDbConnection _connection; private static OleDbConnection Connection { get { if (DataLayer._connection == null) { DataLayer._connection = new OleDbConnection(); DataLayer._connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString; } return DataLayer._connection; } } #region Customers DataLayer private static Customer CreateCustomerData(OleDbDataReader dataReader) { Customer customer = new Customer(); customer.CustomerID = dataReader.IsDBNull(0) ? string.Empty : dataReader.GetString(0); customer.CompanyName = dataReader.IsDBNull(1) ? string.Empty : dataReader.GetString(1); customer.ContactName = dataReader.IsDBNull(2) ? string.Empty : dataReader.GetString(2); customer.ContactTitle = dataReader.IsDBNull(3) ? string.Empty : dataReader.GetString(3); customer.Address = dataReader.IsDBNull(4) ? string.Empty : dataReader.GetString(4); customer.City = dataReader.IsDBNull(5) ? string.Empty : dataReader.GetString(5); customer.Region = dataReader.IsDBNull(6) ? string.Empty : dataReader.GetString(6); customer.PostalCode = dataReader.IsDBNull(7) ? string.Empty : dataReader.GetString(7); customer.Country = dataReader.IsDBNull(8) ? string.Empty : dataReader.GetString(8); customer.Phone = dataReader.IsDBNull(9) ? string.Empty : dataReader.GetString(9); customer.Fax = dataReader.IsDBNull(10) ? string.Empty : dataReader.GetString(10); return customer; } public static CustomerCollection GetCustomers() { OleDbCommand command = new OleDbCommand(); command.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax From Customers"; command.Connection = DataLayer.Connection; CustomerCollection customers = new CustomerCollection(); try { command.Connection.Open(); OleDbDataReader dataReader = command.ExecuteReader(); while (dataReader.Read()) { Customer customer = DataLayer.CreateCustomerData(dataReader); customers.Add(customer); } } finally { command.Connection.Close(); } return customers; } public static Customer GetCustomer(string customerID) { OleDbCommand command = new OleDbCommand(); command.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax From Customers WHERE CustomerID = ?"; command.Parameters.Add(DataLayer.CreateParameter("CustomerID", customerID)); command.Connection = DataLayer.Connection; try { command.Connection.Open(); OleDbDataReader dataReader = command.ExecuteReader(); if (dataReader.Read()) { Customer customer = DataLayer.CreateCustomerData(dataReader); return customer; } } finally { command.Connection.Close(); } return null; } public static int InsertCustomer(Customer customer) { OleDbCommand command = new OleDbCommand(); command.CommandText = "INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; command.Connection = DataLayer.Connection; command.Parameters.Add(DataLayer.CreateParameter("CustomerID", customer.CustomerID)); command.Parameters.Add(DataLayer.CreateParameter("CompanyName", customer.CompanyName)); command.Parameters.Add(DataLayer.CreateParameter("ContactName", customer.ContactName)); command.Parameters.Add(DataLayer.CreateParameter("ContactTitle", customer.ContactTitle)); command.Parameters.Add(DataLayer.CreateParameter("Address", customer.Address)); command.Parameters.Add(DataLayer.CreateParameter("City", customer.City)); command.Parameters.Add(DataLayer.CreateParameter("Region", customer.Region)); command.Parameters.Add(DataLayer.CreateParameter("PostalCode", customer.PostalCode)); command.Parameters.Add(DataLayer.CreateParameter("Country", customer.Country)); command.Parameters.Add(DataLayer.CreateParameter("Phone", customer.Phone)); command.Parameters.Add(DataLayer.CreateParameter("Fax", customer.Fax)); return DataLayer.ExecuteNonQuery(command); } public static int UpdateCustomer(Customer customer) { OleDbCommand command = new OleDbCommand(); command.CommandText = "UPDATE Customers SET CustomerID = ?, CompanyName = ?, ContactName = ?, ContactTitle = ?, " + "Address = ?, City = ?, Region = ?, PostalCode = ?, Country = ?, Phone = ?, Fax = ? WHERE CustomerID = ?"; command.Parameters.Add(DataLayer.CreateParameter("CustomerID", customer.CustomerID)); command.Parameters.Add(DataLayer.CreateParameter("CompanyName", customer.CompanyName)); command.Parameters.Add(DataLayer.CreateParameter("ContactName", customer.ContactName)); command.Parameters.Add(DataLayer.CreateParameter("ContactTitle", customer.ContactTitle)); command.Parameters.Add(DataLayer.CreateParameter("Address", customer.Address)); command.Parameters.Add(DataLayer.CreateParameter("City", customer.City)); command.Parameters.Add(DataLayer.CreateParameter("Region", customer.Region)); command.Parameters.Add(DataLayer.CreateParameter("PostalCode", customer.PostalCode)); command.Parameters.Add(DataLayer.CreateParameter("Country", customer.Country)); command.Parameters.Add(DataLayer.CreateParameter("Phone", customer.Phone)); command.Parameters.Add(DataLayer.CreateParameter("Fax", customer.Fax)); command.Parameters.Add(DataLayer.CreateParameter("OriginalCustomerID", customer.CustomerID)); command.Connection = DataLayer.Connection; return DataLayer.ExecuteNonQuery(command); } public static int DeleteCustomer(Customer customer) { OleDbCommand command = new OleDbCommand(); command.CommandText = "DELETE FROM Customers WHERE CustomerID = ?"; command.Connection = DataLayer.Connection; command.Parameters.Add(DataLayer.CreateParameter("OriginalCustomerID", customer.CustomerID)); return DataLayer.ExecuteNonQuery(command); } #endregion } }
- Switch to C# view code in your WebGrid and add the following namespaces.
using ISNet.WebUI.WebGrid;
using CustomObjects;
- Add the following code to bind the Customers table under Initialize_DataSource and PrepareDataBinding server-side events.
C# Copy Code protected void WebGrid1_InitializeDataSource(object sender, DataSourceEventArgs e) { e.DataSource = DataLayer.GetCustomers(); } protected void WebGrid1_PrepareDataBinding(object sender, DataSourceEventArgs e) { if (!IsPostBack) { WebGrid1.RetrieveStructure(); WebGrid1.RootTable.DataKeyField = "CustomerID"; } }
- Add the following code to map the changes to object.
C# Copy Code private void MapChangesToObject(List<webgridcelldata> data, CustomObjects.Customer customer) { foreach (WebGridCellData cellData in data) { string newText = cellData.NewText; switch (cellData.Column.DataMember) { case "CustomerID": customer.CustomerID = newText; break; case "Address": customer.Address = newText; break; case "City": customer.City = newText; break; case "CompanyName": customer.CompanyName = newText; break; case "ContactName": customer.ContactName = newText; break; case "ContactTitle": customer.ContactTitle = newText; break; case "Country": customer.Country = newText; break; case "Fax": customer.Fax = newText; break; case "Phone": customer.Phone = newText; break; case "PostalCode": customer.PostalCode = newText; break; case "Region": customer.Region = newText; break; } } }
- Add the following code to implement Batch Update with custom object under Batch_Update server-side event.
C# Copy Code protected void WebGrid1_BatchUpdate(object sender, BatchUpdateEventArgs e) { foreach (WebGridRowChanges rowChanges in e.PendingChanges) { CustomObjects.Customer customer = null; try { switch (rowChanges.RowState) { case RowState.Added: customer = new CustomObjects.Customer(); MapChangesToObject(rowChanges.Data, customer); DataLayer.InsertCustomer(customer); break; case RowState.Modified: customer = DataLayer.GetCustomer(rowChanges.KeyValue.ToString()); MapChangesToObject(rowChanges.Data, customer); DataLayer.UpdateCustomer(customer); break; case RowState.Deleted: customer = DataLayer.GetCustomer(rowChanges.KeyValue.ToString()); DataLayer.DeleteCustomer(customer); break; } } catch (Exception exp) { // adds the Exception to batch update list to take advantage of WebGrid's partial errors support. WebGrid1.AddBatchUpdateException(new WebGridBatchUpdateException(rowChanges, new Exception("Record '" + rowChanges.KeyValue.ToString() + "' has error '" + exp.Message + "'", exp))); } } }
- Run the project.