Working With Data In A Connected Environment

  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Working With Data In A Connected Environment as PDF for free.

More details

  • Words: 642
  • Pages: 3
262

Chapter 6

Working with Data in a Connected Environment

Table 6-6

Asynchronous Specific Command Object Methods

Method

Description

BeginExecuteReader

Starts the asynchronous version of the ExecuteReader method.

BeginExecuteXmlReader

Starts the asynchronous version of the ExecuteXmlReader method.

EndExecuteNonQuery

Call this method after the StatementComplete event fires to complete execution of the command.

EndExecuteReader

Call this method after the StatementComplete event fires to return the DataReader with the data returned by the command.

EndExecuteXMLReader

Call this method after the StatementComplete event fires to return the XmlReader with the data returned by the command.

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 Dim NorthWindConnection As New SqlConnection("Data Source=.\;Initial Catalog=Northwind;" & _ Integrated Security=True; asynchronous processing = true") Dim command1 As New SqlCommand("WAITFOR DELAY '00:00:05'; " & _ Select * From [Order Details]", NorthWindConnection) NorthWindConnection.Open()

Dim r As IAsyncResult = command1.BeginExecuteReader

MessageBox.Show("The command has been executed but processing is free to display" & _ " this message before the results have been returned!") Dim reader As SqlDataReader = command1.EndExecuteReader(r)

378

Chapter 7

Create, Add, Delete, and Edit Data in a Disconnected Environment

Figure 7-11 Form1 in the VS IDE after configuring the DataAdapter and generating the DataSet

10. Add the System.Data.SqlClient namespace to your form. 11. Create a Form Load event handler and add the following code to the Form1_Load event handler: ' VB 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

// 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); Add a button to the form and set the following properties:

430

Chapter 8

Implementing Data-Bound Controls

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() == "")

{

DataGridView1.Rows[e.RowIndex].ErrorText = "Product Name is a required field"; e.Cancel = true; }

else

{

DataGridView1.Rows[e.RowIndex].ErrorText = "";

}

}

Format a DataGridView Using Styles Format the look of a DataGridView by setting the grid’s cell styles. Although each cell can have a specific style applied to it, many cells typically share the same style. The DataGridView provides several built-in default cell styles that you can customize and use, or you can create new cell styles and apply them to your DataGridView cells. The following example demonstrates how to apply the alternating rows style.

Format a DataGridView Control by Using Custom Painting To format a DataGridView using custom painting, you can handle the CellPainting event and insert your own custom painting code. When you handle the CellPainting event, the DataGridViewCellPaintingEventArgs provide access to many properties that simplify custom painting. When you handle the CellPainting event, be sure to set e.Handled to True so the grid will not call its own cell painting routine.

Related Documents