wpf-118137-controls-and-libraries-spreadsheet-examples-data-binding-how-to-bind-a-spreadsheet-to-an-ms-sql-server-database-part-2.md
This tutorial shows how to modify the data-bound spreadsheet application created in the How to: Bind a Spreadsheet to an MS SQL Server Database (Part 1) example to enable end-users to add, modify, and remove data in the connected data table.
To provide the capability to interact with the database, add the following UI elements to the spreadsheet application created in the How to: Bind a Spreadsheet to an MS SQL Server Database (Part 1) example.
|
UI Element
|
Implementation
| | --- | --- | |
Data Entry Form
|
On the template worksheet, create a data entry form that will be used to insert new records into the database. For this, do the following.
Unhide rows 3 through 10.
Format the cell range “B3:J9” as shown in the following image, enter the field captions, and add the Save and Cancel cells.
Hide rows 3 through 10 again.
| |
Ribbon Buttons
|
Create a new ribbon group and add the following buttons to it.
The table below contains the description of each bar button item.
|
Content
|
Name
|
Description
| | --- | --- | --- | |
Add Record
|
buttonAddRecord
|
Displays a data entry form on the template worksheet to add a new record to the Suppliers table.
| |
Remove Record
|
buttonRemoveRecord
|
Invokes the Delete dialog, which allows a user to remove the selected record from the database or cancel the delete operation.
| |
Apply Changes
|
buttonApplyChanges
|
Posts the updated data back to the database.
| |
Cancel Changes
|
buttonCancelChanges
|
Cancels the recent changes and loads the latest saved data from the data table.
|
|
When you bind a control to a DataTable containing data from a database and then change data by adding, deleting or modifying records, these changes are accumulated in the DataTable but are not automatically posted to the underlying database. You have to manually call the Update method of the TableAdapter to propagate the modified data to the data source. Before calling this method, make sure that the appropriate INSERT , UPDATE , and DELETE SQL statements are specified for the data adapter. Otherwise, the Update method will generate an exception. For the SuppliersTableAdapter used in the current example, the required commands were generated automatically when the adapter was originally configured.
The table below describes how to add, modify, or delete data in the connected data source step by step.
|
Action
|
Implementation
| | --- | --- | |
Add a record
|
Important
The SpreadsheetControl does not support inserting rows at the end of a data-bound range, while a DataTable supports only this kind of operation.
To avoid this restriction, the current example uses a data entry form to add new records to the data source.
All changes made in the data source are immediately reflected in the bound worksheet.
| |
Apply changes
|
In the Apply Changes button’s ItemClick event handler, call the Update method of the SuppliersTableAdapter to save the modified data to the database.
| |
Cancel changes
|
Handle the Cancel Changes button’s ItemClick event.
In the event handler, close the cell’s in-place editor if it’s currently active and then call the Fill method of the SuppliersTableAdapter to load the latest saved data from the database.
| |
Remove a record
|
Handle the Remove Record button’s ItemClick event. In the event handler, verify that the currently selected range belongs to the data-bound range.
Handle the SpreadsheetControl.RowsRemoving event that fires before a row is deleted. Add code that displays the Delete confirmation dialog to the event handler.
Handle the SpreadsheetControl.RowsRemoved event. This event occurs if a user confirmed the delete operation and the record was deleted. Call the SuppliersTableAdapter.Update method in the event handler to save changes to the database.
|
void spreadsheetControl_PreviewMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Point winPoint = e.GetPosition(spreadsheetControl);
System.Drawing.Point point = new System.Drawing.Point((int)winPoint.X, (int)winPoint.Y);
Cell cell = spreadsheetControl.GetCellFromPoint(point);
if (cell == null)
return;
Worksheet sheet = spreadsheetControl.ActiveWorksheet;
string cellReference = cell.GetReferenceA1();
// If the "Save" cell is clicked in the data entry form,
// add a row containing the entered values to the database table.
if (cellReference == "I4")
{
AddRow(sheet);
HideDataEntryForm(sheet);
ApplyChanges();
}
// If the "Cancel" cell is clicked in the data entry form,
// cancel adding new data and hide the data entry form.
else if (cellReference == "I6")
{
HideDataEntryForm(sheet);
}
}
void AddRow(Worksheet sheet)
{
try
{
// Append a new row to the "Suppliers" data table.
dataSet.Suppliers.AddSuppliersRow(
sheet["C4"].Value.TextValue, sheet["C6"].Value.TextValue, sheet["C8"].Value.TextValue,
sheet["E4"].Value.TextValue, sheet["E6"].Value.TextValue, sheet["E8"].Value.TextValue,
sheet.Cells["G4"].DisplayText, sheet.Cells["G6"].DisplayText);
}
catch (Exception ex)
{
string message = string.Format("Cannot add a row to a database table.\n{0}", ex.Message);
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
void HideDataEntryForm(Worksheet sheet)
{
CellRange range = sheet.Range.Parse("C4,C6,C8,E4,E6,E8,G4,G6");
range.ClearContents();
sheet.Rows.Hide(2, 9);
}
void ApplyChanges()
{
try
{
// Send the updated data back to the database.
adapter.Update(dataSet.Suppliers);
}
catch (Exception ex)
{
string message = string.Format("Cannot update data in a database table.\n{0}", ex.Message);
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
void spreadsheetControl_RowsRemoving(object sender, RowsChangingEventArgs e)
{
Worksheet sheet = spreadsheetControl.ActiveWorksheet;
CellRange rowRange = sheet.Range.FromLTRB(0, e.StartIndex, 16383, e.StartIndex + e.Count - 1);
CellRange boundRange = sheet.DataBindings[0].Range;
// If the rows to be removed belong to the data-bound range,
// display a dialog requesting the user to confirm the deletion of records.
if (boundRange.IsIntersecting(rowRange))
{
MessageBoxResult result = MessageBox.Show("Want to delete the selected supplier(s)?", "Delete",
MessageBoxButton.YesNo, MessageBoxImage.Question);
applyChangesOnRowsRemoved = result == MessageBoxResult.Yes;
e.Cancel = result == MessageBoxResult.No;
return;
}
}
void spreadsheetControl_RowsRemoved(object sender, RowsChangedEventArgs e)
{
if (applyChangesOnRowsRemoved)
{
applyChangesOnRowsRemoved = false;
// Update data in the database.
ApplyChanges();
}
}
void buttonAddRecord_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
CloseInplaceEditor();
Worksheet sheet = spreadsheetControl.ActiveWorksheet;
// Display the data entry form on the worksheet to add a new record to the "Suppliers" data table.
if (!sheet.Rows[4].Visible)
sheet.Rows.Unhide(2, 9);
spreadsheetControl.SelectedCell = sheet["C4"];
}
void buttonRemoveRecord_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
CloseInplaceEditor();
Worksheet sheet = spreadsheetControl.ActiveWorksheet;
CellRange selectedRange = spreadsheetControl.Selection;
CellRange boundRange = sheet.DataBindings[0].Range;
// Verify that the selected cell range belongs to the data-bound range.
if (!boundRange.IsIntersecting(selectedRange) || selectedRange.TopRowIndex < boundRange.TopRowIndex)
{
MessageBox.Show("Select a record first!", "Remove Record", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
// Remove the topmost row of the selected cell range.
sheet.Rows.Remove(selectedRange.TopRowIndex);
}
void buttonApplyChanges_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
CloseInplaceEditor();
// Update data in the database.
ApplyChanges();
}
void buttonCancelChanges_ItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
{
// Close the cell in-place editor if it's currently active.
CloseInplaceEditor();
// Load the latest saved data into the "Suppliers" data table.
adapter.Fill(dataSet.Suppliers);
}
void CloseInplaceEditor()
{
if (spreadsheetControl.IsCellEditorActive)
spreadsheetControl.CloseCellEditor(DevExpress.XtraSpreadsheet.CellEditorEnterValueMode.Default);
}
Private Sub spreadsheetControl_PreviewMouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
Dim winPoint As Point = e.GetPosition(spreadsheetControl)
Dim point As New System.Drawing.Point(CInt((winPoint.X)), CInt((winPoint.Y)))
Dim cell As Cell = spreadsheetControl.GetCellFromPoint(point)
If cell Is Nothing Then
Return
End If
Dim sheet As Worksheet = spreadsheetControl.ActiveWorksheet
Dim cellReference As String = cell.GetReferenceA1()
' If the "Save" cell is clicked in the data entry form,
' add a row containing the entered values to the database table.
If cellReference = "I4" Then
AddRow(sheet)
HideDataEntryForm(sheet)
ApplyChanges()
' If the "Cancel" cell is clicked in the data entry form,
' cancel adding new data and hide the data entry form.
ElseIf cellReference = "I6" Then
HideDataEntryForm(sheet)
End If
End Sub
Private Sub AddRow(ByVal sheet As Worksheet)
Try
' Append a new row to the "Suppliers" data table.
dataSet.Suppliers.AddSuppliersRow(sheet("C4").Value.TextValue, sheet("C6").Value.TextValue, sheet("C8").Value.TextValue, sheet("E4").Value.TextValue, sheet("E6").Value.TextValue, sheet("E8").Value.TextValue, sheet.Cells("G4").DisplayText, sheet.Cells("G6").DisplayText)
Catch ex As Exception
Dim message As String = String.Format("Cannot add a row to a database table." & ControlChars.Lf & "{0}", ex.Message)
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error)
End Try
End Sub
Private Sub HideDataEntryForm(ByVal sheet As Worksheet)
Dim range As CellRange = sheet.Range.Parse("C4,C6,C8,E4,E6,E8,G4,G6")
range.ClearContents()
sheet.Rows.Hide(2, 9)
End Sub
Private Sub ApplyChanges()
Try
' Send the updated data back to the database.
adapter.Update(dataSet.Suppliers)
Catch ex As Exception
Dim message As String = String.Format("Cannot update data in a database table." & ControlChars.Lf & "{0}", ex.Message)
MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error)
End Try
End Sub
Private Sub spreadsheetControl_RowsRemoving(ByVal sender As Object, ByVal e As RowsChangingEventArgs)
Dim sheet As Worksheet = spreadsheetControl.ActiveWorksheet
Dim rowRange As CellRange = sheet.Range.FromLTRB(0, e.StartIndex, 16383, e.StartIndex + e.Count - 1)
Dim boundRange As CellRange = sheet.DataBindings(0).Range
' If the rows to be removed belong to the data-bound range,
' display a dialog requesting the user to confirm the deletion of records.
If boundRange.IsIntersecting(rowRange) Then
Dim result As MessageBoxResult = MessageBox.Show("Want to delete the selected supplier(s)?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question)
applyChangesOnRowsRemoved = result = MessageBoxResult.Yes
e.Cancel = result = MessageBoxResult.No
Return
End If
End Sub
Private Sub spreadsheetControl_RowsRemoved(ByVal sender As Object, ByVal e As RowsChangedEventArgs)
If applyChangesOnRowsRemoved Then
applyChangesOnRowsRemoved = False
' Update data in the database.
ApplyChanges()
End If
End Sub
Private Sub buttonAddRecord_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
CloseInplaceEditor()
Dim sheet As Worksheet = spreadsheetControl.ActiveWorksheet
' Display the data entry form on the worksheet to add a new record to the "Suppliers" data table.
If Not sheet.Rows(4).Visible Then
sheet.Rows.Unhide(2, 9)
End If
spreadsheetControl.SelectedCell = sheet("C4")
End Sub
Private Sub buttonRemoveRecord_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
CloseInplaceEditor()
Dim sheet As Worksheet = spreadsheetControl.ActiveWorksheet
Dim selectedRange As CellRange = spreadsheetControl.Selection
Dim boundRange As CellRange = sheet.DataBindings(0).Range
' Verify that the selected cell range belongs to the data-bound range.
If (Not boundRange.IsIntersecting(selectedRange)) OrElse selectedRange.TopRowIndex < boundRange.TopRowIndex Then
MessageBox.Show("Select a record first!", "Remove Record", MessageBoxButton.OK, MessageBoxImage.Error)
Return
End If
' Remove the topmost row of the selected cell range.
sheet.Rows.Remove(selectedRange.TopRowIndex)
End Sub
Private Sub buttonApplyChanges_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
CloseInplaceEditor()
' Update data in the database.
ApplyChanges()
End Sub
Private Sub buttonCancelChanges_ItemClick(ByVal sender As Object, ByVal e As DevExpress.Xpf.Bars.ItemClickEventArgs)
' Close the cell in-place editor if it's currently active.
CloseInplaceEditor()
' Load the latest saved data into the "Suppliers" data table.
adapter.Fill(dataSet.Suppliers)
End Sub
Private Sub CloseInplaceEditor()
If spreadsheetControl.IsCellEditorActive Then
spreadsheetControl.CloseCellEditor(DevExpress.XtraSpreadsheet.CellEditorEnterValueMode.Default)
End If
End Sub
See Also
How to: Bind a Spreadsheet to an MS SQL Server Database (Part 1)