Académique Documents
Professionnel Documents
Culture Documents
When executing commands asynchronously, you explicitly call the Begin and End
methods of the selected Command object. Calling the Begin method sends the com
mand (SQL statement or stored procedure call) to the database, and then you can per
form other operations in your application. When the command finishes executing,
the StatementCompleted event fires, notifying the application that it can call the End
method of the command and access the data for further processing.
The following code shows how you can continue processing even while a command
is in the process of executing:
'VB
Dim results As New System.Text.StringBuilder
NorthWindConnection.Open()
MessageBox.Show("The command has been executed but processing is free to display" & _
" this message before the results have been returned!")
Figure 7-11 Form1 in the VS IDE after configuring the DataAdapter and generating the
DataSet
' For this example we will turn off the ability to edit directly in a cell.
CustomersDataGridView.MultiSelect = False
CustomersDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect
CustomersDataGridView.EditMode = DataGridViewEditMode.EditProgrammatically
// C#
CustomersDataGridView.DataSource = NorthwindDataset1.Customers;
// For this example we will turn off the ability to edit directly in a cell.
CustomersDataGridView.MultiSelect = false;
CustomersDataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
CustomersDataGridView.EditMode = DataGridViewEditMode.EditProgrammatically;
12. Create a button-click event handler for the FillTableButton and add the following
code:
' VB
SqlDataAdapter1.Fill(NorthwindDataset1.Customers)
// C#
sqlDataAdapter1.Fill(NorthwindDataset1.Customers);
DataGridView1.Rows[e.RowIndex].ErrorText = "";
The following code validates that the ProductName column does not contain an empty
string (use this example for a DataGridView that is bound to data):
' VB
If DataGridView1.Columns(e.ColumnIndex).DataPropertyName = "ProductName" Then
If e.FormattedValue.ToString = "" Then
dataGridView1.Rows(e.RowIndex).ErrorText = "Product Name is a required field"
e.Cancel = True
Else
dataGridView1.Rows(e.RowIndex).ErrorText = ""
End If
End If
// C#
if (DataGridView1.Columns[e.ColumnIndex].DataPropertyName == "ProductName")
{
if (e.FormattedValue.ToString() == "")
else
DataGridView1.Rows[e.RowIndex].ErrorText = "";