Sei sulla pagina 1di 17

Working with the

DataGridView
CHAPTER 8 – LESSON 3

Ottawa .NET Community MCTS 70-526 Exam Study Group


Contents

This lesson describes how to configure and work with


data in a DataGridView control. The DataGridView
is typically used to display the contents of a
DataTable in a DataSet.
This lesson shows how to:
 Use the DataGridView control to display and update
the data.
 Bind a DataGridView control to a data source.
 Configure a DataGridView to use multiple data
sources.
 Manage columns in a DataGridView control.
Contents

 Format a DataGridView control by using styles.


 Format a DataGridView control by using custom
painting.
 Configure the column and cell types of a
DataGridView.
 Add tables and columns to a DataGridView control.
 Delete columns in a DataGridView control.
 Respond to clicks in a DataGridView control.
 Validate input with a DataGridView control.
 Change data displayed in a DataGridView control at
run time.
Displaying a Dataset
in the DataGridView Control

 To display a dataset in a DataGridView control or, more specifically, to display


a Data-Table in a DataGridView, set the DataSource property of the
DataGridView to the DataSet and set the DataMember property of the
DataGridView to the name of the DataTable. For example, the following code
displays the Northwind Customers table in a DataGridView:
 ' VB
 DataGridView1.DataSource = NorthwindDataSet1
DataGridView1.DataMember = "Customers"
 //C#
 DataGridView1.DataSource = northwindDataSet1;DataGridView1.DataMember
= "Customers";

You can also set a DataGridView control to display a dataset using the smart
tag available on a DataGridView control by selecting the DataSet in the
Choose Data Source ComboBox available on the smart tag. The Choose Data
Source command allows you to select a DataSet and DataTable to display
from the DataSet list already defined in your project, or you can create a new
DataSet to display by selecting Add Project Data Source on the smart tag,
which will start the Data Source Configuration Wizard.
Configuring DataGridView Columns

 There are six built-in types of columns you can use in a DataGridView
 DataGridViewTextBoxColumn Use this column type to display text and
numeric values. A data-bound DataGridView automatically generates this type
of column when binding to strings and numeric values.
 DataGridViewCheckBoxColumn Use this column to display Boolean values. A
DataGridView automatically generates this type of column when binding to
Boolean values.
 DataGridViewImageColumn Use this column to display images. A DataGrid-
View automatically generates this type of column when binding to Image and
Icon objects. You can also bind a DataGridViewImage column to a byte array.
 DataGridViewButtonColumn Use this column to provide users with a button
control.
 DataGridViewComboBoxColumn Use this column type to present lists of
choices. This would typically be used for lookups to other tables.
DataGridViewLinkColumn Use this column type to display links to other data.
Configuring DataGridView Columns

 Custom Column If none of the preceding


column types provides the specific
functionality you require, you can always
create a custom column type. To create a
custom column, define your class to inherit
from DataGridViewColumn or any class with
a base class of DataGridViewColumn. (For
example, inherit from
DataGridViewTextBoxColumn to extend the
functionality of that type.)
Adding Tables and Columns to a
DataGridView
 To display a table in a DataGridView, you add and
define the columns to the DataGrid-View that make
up the schema of the table. You can add columns
to a DataGridView with Designers using the Add
Column dialog box or programmatically in code.
 First, decide which type of column to use (refer to
Table 8-1, shown previously), and then use one of
the following procedures to add the column to your
DataGridView.
 Adding Columns to a DataGridView Using the
Designer
 To add columns to a DataGridView in the Designer,
perform the following procedures:
Adding Columns to a DataGridView
Using the Designer
To add columns to a DataGridView in the Designer,
perform the following procedures:
1. Select the DataGridView on your form.
2. Display the smart tag of the DataGridView.
3. Select Add Column.
4. In the Add Column dialog box, define the
column by setting the appropriate values in
the dialog box.
Adding Columns to a DataGridView
Programmatically
To add columns to a DataGridView in code, create an instance of the
type of DataGrid-View column to create, define the column by
setting the appropriate properties, and then add the column to the
DataGridView.Columns collection.

For example, the following code sample creates a new text box column
named Product-Name:
// C#
DataGridViewTextBoxColumn ProductNameColumn = new
DataGridViewTextBoxColumn(); ProductNameColumn.Name
= "ProductName"; ProductNameColumn.HeaderText =
"Product Name";ProductNameColumn.ValueType =
System.Type.GetType("System.String");DataGridView1.Colu
mns.Add(ProductNameColumn); .
Deleting Columns in the DataGridView

To delete columns in a DataGridView using the


Designer, perform the following procedures: :
1. Select the DataGridView on your form.
2. Display the smart tag for the
DataGridView.
3. Select Edit Columns.
4. In the Edit Columns dialog box, select the
column you want to remove from the
DataGridView.
5. Click the Remove button.
Deleting Columns in the DataGridView
Programmatically

To delete columns in a DataGridView in code, call the


Remove method and provide the name of the
column you want to delete. For example, the
following code example deletes a column named
ProductName from DataGridView1:
' VB
DataGridView1.Columns.Remove("ProductName")
// C#
DataGridView1.Columns.Remove["ProductName"];
Determining the Clicked Cell in a
DataGridView

To determine the clicked cell, use the DataGridView.CurrentCell


property. The Current-Cell provides a reference to the currently
selected cell and provides properties to access the value of the
data in the cell as well as the row and column index of the cell’s
current location in the DataGridView.
' VB
Dim CurrentCellValue As String CurrentCellValue =
CustomersDataGridView.CurrentCell.Value.ToString
// C#
String CurrentCellValue;CurrentCellValue =
CustomersDataGridView.CurrentCell.Value.ToString();
Validating Input in the DataGridView

To validate input in an individual cell in a


DataGridView, handle the DataGridView
.CellValidating event and cancel the edit if the value
fails validation. The CellValidating event is raised
when a cell loses focus. Add code to the event
handler for the CellValidating event to verify that
the values in specific columns conform to your
business rules and application logic. The event
arguments contain the proposed value in the cell as
well as the row and column index of the cell being
edited.
Code Example  DEMO
Format a DataGridView Using Styles

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
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.

Code Example  DEMO


Lesson Summary

 The DataGridView is the preferred control for displaying


tabular data such as a DataTable.
 You can add and remove columns to a DataGridView in the
Designer, using the Add Column and Edit Column dialog boxes
available from the smart tag of the DataGridView.
 The DataGridView.CurrentCell property provides access to the
currently selected cell in a DataGridView.
 The DataGridView raises a CellValidating event through which
you can add code that verifies that the value in a column
conforms to your business rules and application logic.
 You can format the look of a DataGridView using styles and
custom painting.
ATTENTION VERIFY
QUESTION!
Did anybody noticed what’s happened to
Ottawa .net Community logo during
the presentation?!

Potrebbero piacerti anche