Thread: GridView
View Single Post
  #1  
Old 06-01-2009, 04:46 PM
bholus7
Guest
 
Posts: n/a
Default GridView

GridView

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 built in 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 DataGridView****onColumn 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
9. How do I sort on multiple columns? 54
10. How do I hook up events on the editing control? 58
11. When should I remove event handlers from the editing
control? 58
12. How do I handle the SelectedIndexChanged event? 58
13. How do I perform drag and drop reorder of rows? 59
14. How do I make the last column wide enough to occupy all
the remaining client area of the grid? 60


15. How do I have the cell text wrap? 60
16. How do I make the image column not show any images? 61
17. How do I enable typing in the combo box cell? 61
18. How do I have a combo box column display a sub set of
data based upon the value of a different combo box column? 61
19. How do I show the error icon when the user is editing
the cell? 62


20. How do I show unbound data along with bound data? 65
21. How do I show data that comes from two tables? 66
22. How do I show master-details? 66
23. How do I show master-details in the same DataGridView? 68
24. How do I prevent sorting? 68


25. How do I commit the data to the database when clicking
on a toolstrip ****on? 68
26. How do I display a confirmation dialog when the user
tries to delete a row? 68
Reply With Quote