DataGridView control details

  • 2020-05-17 05:20:54
  • OfStack

1 what is DataGridView
With the DataGridView control, you can display and edit tabular data from a variety of different types of data sources.
The DataGridView control is highly configurable and extensible, providing a wide range of properties, methods, and events that can be used to customize the appearance and behavior of the control. When you need to display tabular data in an WinForm application, DataGridView (as opposed to other controls such as DataGrid) is preferred. If you want to display read-only data in a small grid, or allow users to edit millions of records, DataGridView will provide you with an easy to program and good performance solution.

DataGridView replaces DataGrid in previous versions and has more features than DataGrid. However, DataGrid is still retained for backward compatibility and future use. If you want to choose between the two, you can refer to the details of the differences between DataGrid and DataGridView given below.

1.1 differences between DataGridView and DataGrid
DataGridView offers a number of basic and advanced features that DataGrid doesn't have. In addition, the structure of DataGridView makes it easier to extend and customize than the DataGrid control.
The following table describes several major features that DataGridView provides and DataGrid does not.

DataGridView function

describe

Multiple column types

DataGridView provides more built-in column types than DataGrid. These column types meet most common needs and are easier to extend or replace than the column types in DataGrid.

Multiple data display methods

DataGrid is limited to displaying data from external data sources. DataGridView, on the other hand, can display unbound data, bound data sources, or both. You can also implement virtual mode in DataGridView for custom data management.

Multiple ways to customize data display

DataGridView provides a number of properties and events for formatting and displaying data. For example, you can change the appearance of cells, rows, and columns based on their contents, or you can replace one type of data with another type of data.

Multiple options for changing cells, rows, columns, header appearance, and behavior

DataGridView enables you to manipulate a single grid component in multiple ways. For example, you can freeze rows and columns to prevent them from being invisible by scrolling. Hide rows, columns, and header; Adjust the size of row, column and header; Provides tooltips (ToolTip) and shortcut menus for individual cells, rows, and columns.



The only feature that DataGridView does not provide is a hierarchical display of data in two related tables (such as the common master-slave table display). You must use two DataGridView to display data from two tables with master-slave relationships.

1.2 DataGridView bright spot
The following table highlights the main features of DataGridView, which will be covered in more detail later.

DataGridView control features

describe

Multiple column types

DataGridView provides columns of types TextBox, CheckBox, Image, Button, ComboBox, and Link, as well as the corresponding cell types.

Multiple data display methods

DataGrid is limited to displaying data from external data sources. DataGridView, on the other hand, can display unbound data, bound data sources, or both. You can also implement virtual mode in DataGridView for custom data management.

Customize the various ways in which data can be displayed and manipulated

DataGridView provides a number of properties and events for formatting and displaying data.

In addition, DataGridView provides a variety of ways to manipulate data. For example, you can:

Sort the data and display the corresponding sorting symbol (arrow with direction represents ascending and descending order) Multiple selection patterns for rows, columns, and cells; Multiple choice and single choice Copy the data to the clipboard in a variety of formats, including text, CSV (comma-separated values), and HTML Changes the way users edit the content of cells

Multiple options for changing cells, rows, columns, header appearance, and behavior

DataGridView enables you to manipulate a single grid component in multiple ways. For example, you can:

Freeze rows and columns to prevent them from being invisible as they roll; Hide rows, columns, and header; Adjust the size of row, column and header; Changing the user's selection mode for rows, columns and cells; Provides tooltips (ToolTip) and shortcut menus for individual cells, rows, and columns. Customize border styles for cells, rows, and columns.

Provides rich extensibility support

DataGridView provides an infrastructure that is easy to extend and customize the grid, such as:

Handling custom drawing events provides a custom look and feel for cells, columns, and rows. Inherits a built-in cell type to provide more behavior; Implement custom interfaces to provide a new editing experience.

The structure of 2 DataGridView
DataGridView and its related classes are designed to be a flexible, extensible architecture for displaying and editing tabular data. These classes are all located in the system.Windows.Forms namespace, and all have the common prefix "DataGridView" for their names.

2.1 structural elements (Architecture Elements)
The main DataGridView related class inherits from the DataGridViewElement class.
The DataGridViewElement class has two properties, 1 is DataGridView, which provides a reference to the DataGridView to which it belongs; 2 is State, which represents the current state, and its value is DataGridViewElementStates enumeration, which supports bit operations, which means you can set the combined state.

2.2 cells and groups (Cells and Bands)
DataGridView consists of two basic objects: cells (cell) and groups (band). All cells inherit from the DataGridViewCell base class. The two types of groups (or collections), DataGridViewColumn and DataGridViewRow, both inherit from the DataGridViewBand base class and represent a group of cells bound together at 1.
DataGridView interoperates with some classes, but the most common ones are DataGridViewCell, DataGridViewColumn, and DataGridViewRow.

2.3 cells of DataGridView (DataGridViewCell)
The cell (cell) is the basic unit that operates DataGridView. Display is centered on cells, and data entry is often performed through cells The cell contained in line 1 can be accessed through the Cells collection property of the DataGridViewRow class, the currently selected cell through the SelectedCells collection property of DataGridView, and the current cell through the CurrentCell property of DataGridView.

DataGridViewCell class diagram

Cell related classes and properties



DataGridViewCell is an abstract base class from which all cell types inherit. DataGridViewCell and its inheritance types are not Windows Forms controls, but some of them are hosted on Windows Forms controls. The editing capabilities supported by a cell are usually handled by its host controls.

The DataGridViewCell object does not control its own appearance and draw (painting) characteristics as the Windows Forms control does; instead, DataGridView is responsible for the appearance of the cells it contains. With the properties and events of the DataGridView control, you can profoundly influence the appearance and behavior of cells. If you have specific requirements for cell customization beyond what DataGridView provides, you can inherit DataGridViewCell or one of its subclasses to meet those requirements.

2.3.1 working mechanism of DataGridViewCell
An important part of understanding the DataGridView structure is understanding how DataGridViewCell works:

The value of the cell (A Cell's Value)
The value of the cell is its essence. If the cell is in a column that is not a bound column, and DataGridView is not Virtual Mode, then its value is held and maintained by itself. For cells generated by bindings, they simply don't "know" what values to hold, and they certainly don't maintain them. When DataGridView needs the value of a cell, it queries the data source for the value that cell should display. Under Virtual Mode, it is very similar to data binding except that the CellValueNeeded event is triggered to get the value of the corresponding cell. At the cell level, all of this is controlled by the DataGridViewCell.GetValue () method.
By default, the value of the cell is of type object. When a column is bound, its ValueType property is set, and the ValueType of the cell it contains is updated. The ValueType of the cell is important for the formatting of the next step.

Formatted display (Formatting for Display)
Note: when DataGridView needs to know "how to display this cell," it needs FormattedValue of the cell, not Value. This is a complex process, because formatting something on the screen usually requires converting it to a string. For example, although you set the value of the cell (Value) to an integer value of 155, you still need to format it when displaying it. The FormattedValueType property of the cell and its column determines the type used to display it. Most columns use string types, while cells/columns of types Image and CheckBox use other types. Cells and columns of type Image use Image as the default FormattedValueType, and its built-in implementation knows how to display an Image. The FormattedValueType property for cells/columns of type CheckBox depends on the value of the property ThreeState. At the cell level, all of this is controlled by DataGridViewCell.GetFormattedValue ().

By default, DataGridView USES TypeConverter to convert the value of a cell (Value) to a formatted value (FormattedValue). DataGridView retrieves the timed TypeConverter based on the cell's ValueType and FormattedValueType attributes.

For a single cell, FormattedValue gets multiple requests (that is, it is used in multiple places) : when a cell is drawn, when the column automatically resizes according to the cell's content, and even when the mouse passes over the cell's content. Every time you need FormattedValue, DataGridView will trigger the CellFormatting event, and you will have the opportunity to modify the formatted display of the cell.
If the cell cannot get its formatted value, it triggers the DataError event.

Formatting the display cell also includes the preferred size to display it. The preferred size is a combination of the cell's FormattedValue, the filled area (padding), the attached display, and the border.

Draw the display of cells (Painting the Display)
Once you get FormattedValue, the cell is responsible for drawing its contents. The cell determines the correct style to use for the drawing process (see the styles section in chapter 5 of this document) and draws. Remember: if you don't to draw his cell, the cell will not have any content to get drawing (that is, the cell's drawing only by its own), in charge of row and column does not draw any content, so be sure to at least draw the background of the cell (background), otherwise the cell's rectangle is still invalid (i.e., without the drawing).

Display of parse cells (Parsing the Display)
Once the user starts interacting with the cell, it is possible to edit the cell's value. One thing to remember is that the user is actually editing the FormattedValue of the cell. When the user submits the edited value, FormattedValue needs to convert back to the value of the cell (Value), a process known as parsing (parsing). At the cell level, all this work is controlled by the cell's DataGridViewCell.ParseFormattedValue (int rowIndex) method.
By default, TypeConverter will be used again to resolve FormattedValue to the true value of the cell. This will trigger the CellParsing event for DataGridView, and you will have the opportunity to modify how the cell is resolved.

If the cell is not parsed correctly, the DataError event is triggered.

Related articles: