Working with DataGridView

Working with DataGridViewThis 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.
- 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 Datasetin the DataGridViewControl
- 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 DataGridViewColumns
- 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.

Website: www.ottawacommunity.net | Filesize: 9525kb
No of Page(s): 17
Content

Click to download Working with DataGridView.

Related Tutorial

Tags: , , , ,

Comments

Leave a Reply