DataGridView FAQ.doc

(1026 KB) Pobierz
DataGridView Control

DataGridView FAQ.doc

DataGridView Control

The DataGridView control is the new grid control for Windows Froms 2.0. It replaces the DataGrid control with an easy to use and extremely customizable grid that supports many of the features that are needed for our customers.

 

About this document:

This material should not be considered a complete coverage of DataGridView but it tries to capture the high-level features with some depth in specific areas.

 

This document is divided into about 5 logical sections starting with a feature and structure overview, followed by an overview of builtin column and cell types. Next is an overview of working with data, followed by an overview of specific major features. Lastly, a “best practice” section concludes the main part of this document.

 

Most sections contain a “Q & A” section that answers common questions regarding the specific feature or area covered by the section. Note that some questions are duplicated in multiple sections based upon the section relevancy. The question and answers with code samples/snippets are contained in this document’s only appendix to make for a “one-stop shop” of code.

 

Note that most of the context of this document comes from the DataGridView control documentation presented in MSDN.

 

Contents

1              What is the DataGridView              4

1.1              Differences between the DataGridView and DataGrid controls              4

1.2              Highlight of features              5

2              Structure of DGV              6

2.1              Architecture Elements              6

2.2              Cells and Bands              6

2.3              DataGridViewCell              6

2.3.1              How a DataGridViewCell works              7

2.4              DataGridViewColumn              9

2.5              DataGridView Editing Controls              9

2.6              DataGridViewRow              10

3              Out of the box column/cell types              11

3.1              DataGridViewTextBoxColumn              11

3.2              DataGridViewCheckBoxColumn              12

3.3              DataGridViewImageColumn              12

3.4              DataGridViewButtonColumn              13

3.5              DataGridViewComboBoxColumn              13

3.5.1              DataErrors and the combo box column              13

3.6              DataGridViewLinkColumn              14

4              Working with Data              15

4.1              Data Entry and Validation Events              15

4.1.1              Order of Validation Events              15

4.1.2              Validating Data              15

4.1.3              Data Entry in the New Row              16

4.2              Working with Null values              19

4.2.1              NullValue              19

4.2.2              DataSourceNullValue              19

4.3              DataError event              20

4.4              Databound modes              21

4.4.1              Unbound              21

4.4.2              Bound              21

4.4.3              Virtual              22

4.4.4              Mixed mode – Bound and Unbound              22

5              Overview of features              24

5.1              Styling              24

5.1.1              The DataGridViewCellStyle Class              24

5.1.2              Using DataGridViewCellStyle Objects              24

5.1.3              Style Inheritance              25

5.1.4              Setting Styles Dynamically              28

5.2              Custom painting              28

5.2.1              Paint Parts              28

5.2.2              Row Pre Paint and Post Paint              29

5.3              Autosizing              30

5.3.1              Sizing Options in the Windows Forms DataGridView Control              30

5.3.2              Resizing with the Mouse              31

5.3.3              Automatic Sizing              32

5.3.4              Programmatic Resizing              33

5.3.5              Customizing Content-based Sizing Behavior              34

5.3.6              Content-based Sizing Options              34

5.4              Selection modes              34

5.4.1              Programmatic Selection              35

5.5              Scrolling              35

5.5.1              Scroll event              35

5.5.2              Scroll bars              35

5.5.3              Scrolling Properties              36

5.6              Sorting              36

5.6.1              Programmatic Sorting              37

5.6.2              Custom Sorting              38

5.7              Border styles              39

5.7.1              Standard Border Styles              39

5.7.2              Advanced Border Styles              39

5.8              Enter-Edit modes              40

5.9              Clipboard copy modes              40

5.10              Frozen columns/rows              41

5.11              Implementing Custom cells and editing controls/cells              41

5.11.1              IDataGridViewEditingControl              42

5.11.2              IDataGridViewEditingCell              42

5.12              Virtual mode              42

5.12.1              Bound Mode and Virtual Mode              42

5.12.2              Supplementing Bound Mode              42

5.12.3              Replacing Bound Mode              43

5.12.4              Virtual-Mode Events              43

5.12.5              Best Practices in Virtual Mode              44

5.13              Capacity              44

6              Best Practices              45

6.1              Using Cell Styles Efficiently              45

6.2              Using Shortcut Menus Efficiently              45

6.3              Using Automatic Resizing Efficiently              45

6.4              Using the Selected Cells, Rows, and Columns Collections Efficiently              46

6.5              Using Shared Rows              46

6.6              Preventing Rows from Becoming Unshared              47

Appendix A – Common Questions and Answers              49

1.              How do I prevent a particular cell from being editable?              49

2.              How do I disable a cell?              49

3.              How do I restrict user from setting focus to a specific cell?              51

4.              How do I show controls in all cells regardless of edit?              51

5.              Why does the cell text show up with “square” characters where they should be new lines?              51

6.              How do I show icon and text in the same cell?              51

7.              How do I hide a column?              53

8.              How do I prevent the user from sorting on a column?              53

...

Zgłoś jeśli naruszono regulamin