Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
3920937.Doc

3920937.Doc

Ratings: (0)|Views: 302|Likes:
Published by GAGARAIN
How to use DataGridView in VB.NET
How to use DataGridView in VB.NET

More info:

Published by: GAGARAIN on Nov 10, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

02/15/2011

pdf

text

original

 
3920937.doc
DataGridView Control
The DataGridView control is the new grid control for Windows Froms 2.0. It replaces the DataGridcontrol with an easy to use and extremely customizable grid that supports many of the features that areneeded for our customers.
About this document:
This material should not be considered a complete coverage of DataGridView but it tries to capture thehigh-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 mainpart of this document.Most sections contain a “Q & A” section that answers common questions regarding the specific featureor area covered by the section. Note that some questions are duplicated in multiple sections based uponthe section relevancy. The question and answers with code samples/snippets are contained in thisdocument’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 documentationpresented in MSDN.
Contents
1 What is the DataGridView ..........................................................................................................................41.1 Differences between the DataGridView and DataGrid controls ........................................................41.2 Highlight of features ............................................................................................................................52 Structure of DGV ..........................................................................................................................................62.1 Architecture Elements ..........................................................................................................................62.2 Cells and Bands ......................................................................................................................................62.3 DataGridViewCell .................................................................................................................................62.3.1 How a DataGridViewCell works ..................................................................................................72.4 DataGridViewColumn ..........................................................................................................................92.5 DataGridView Editing Controls ...........................................................................................................92.6 DataGridViewRow ..............................................................................................................................103 Out of the box column/cell types ..............................................................................................................113.1 DataGridViewTextBoxColumn ..........................................................................................................123.2 DataGridViewCheckBoxColumn .......................................................................................................123.3 DataGridViewImageColumn ..............................................................................................................123.4 DataGridViewButtonColumn .............................................................................................................133.5 DataGridViewComboBoxColumn ......................................................................................................133.5.1 DataErrors and the combo box column ......................................................................................133.6 DataGridViewLinkColumn ................................................................................................................144 Working with Data .....................................................................................................................................154.1 Data Entry and Validation Events ......................................................................................................154.1.1 Order of Validation Events .........................................................................................................154.1.2 Validating Data ............................................................................................................................154.1.3 Data Entry in the New Row ........................................................................................................164.2 Working with Null values ..................................................................................................................19Page 1 of 69
 
3920937.doc4.2.1 NullValue .....................................................................................................................................194.2.2 DataSourceNullValue ..................................................................................................................194.3 DataError event ...................................................................................................................................204.4 Databound modes ................................................................................................................................214.4.1 Unbound .......................................................................................................................................214.4.2 Bound ............................................................................................................................................214.4.3 Virtual ...........................................................................................................................................224.4.4 Mixed mode – Bound and Unbound ...........................................................................................225 Overview of features ..................................................................................................................................245.1 Styling ..................................................................................................................................................245.1.1 The DataGridViewCellStyle Class ..............................................................................................245.1.2 Using DataGridViewCellStyle Objects .......................................................................................245.1.3 Style Inheritance ..........................................................................................................................255.1.4 Setting Styles Dynamically ..........................................................................................................285.2 Custom painting ..................................................................................................................................285.2.1 Paint Parts ....................................................................................................................................285.2.2 Row Pre Paint and Post Paint .....................................................................................................295.3 Autosizing ............................................................................................................................................305.3.1 Sizing Options in the Windows Forms DataGridView Control .............................................305.3.2 Resizing with the Mouse .............................................................................................................315.3.3 Automatic Sizing ..........................................................................................................................325.3.4 Programmatic Resizing ................................................................................................................335.3.5 Customizing Content-based Sizing Behavior .............................................................................345.3.6 Content-based Sizing Options .....................................................................................................345.4 Selection modes ...................................................................................................................................345.4.1 Programmatic Selection ...............................................................................................................355.5 Scrolling ...............................................................................................................................................355.5.1 Scroll event ...................................................................................................................................355.5.2 Scroll bars .....................................................................................................................................365.5.3 Scrolling Properties .....................................................................................................................365.6 Sorting ..................................................................................................................................................365.6.1 Programmatic Sorting ..................................................................................................................375.6.2 Custom Sorting .............................................................................................................................385.7 Border styles ........................................................................................................................................395.7.1 Standard Border Styles .................................................................................................................395.7.2 Advanced Border Styles ...............................................................................................................395.8 Enter-Edit modes .................................................................................................................................405.9 Clipboard copy modes .........................................................................................................................415.10 Frozen columns/rows ........................................................................................................................415.11 Implementing Custom cells and editing controls/cells ...................................................................425.11.1 IDataGridViewEditingControl ..................................................................................................425.11.2 IDataGridViewEditingCell ........................................................................................................425.12 Virtual mode ......................................................................................................................................425.12.1 Bound Mode and Virtual Mode ................................................................................................425.12.2 Supplementing Bound Mode .....................................................................................................425.12.3 Replacing Bound Mode .............................................................................................................435.12.4 Virtual-Mode Events .................................................................................................................435.12.5 Best Practices in Virtual Mode ..................................................................................................445.13 Capacity .............................................................................................................................................44Page 2 of 69
 
3920937.doc6 Best Practices ..............................................................................................................................................456.1 Using Cell Styles Efficiently ...............................................................................................................456.2 Using Shortcut Menus Efficiently ......................................................................................................456.3 Using Automatic Resizing Efficiently ................................................................................................456.4 Using the Selected Cells, Rows, and Columns Collections Efficiently ............................................466.5 Using Shared Rows ..............................................................................................................................466.6 Preventing Rows from Becoming Unshared .....................................................................................47Appendix A – Common Questions and Answers .......................................................................................491. How do I prevent a particular cell from being editable? ....................................................................492. How do I disable a cell? ........................................................................................................................493. How do I restrict user from setting focus to a specific cell? ...............................................................514. How do I show controls in all cells regardless of edit? .......................................................................515. Why does the cell text show up with “square” characters where they should be new lines? ..........516. How do I show icon and text in the same cell? ...................................................................................517. How do I hide a column? ......................................................................................................................538. How do I prevent the user from sorting on a column? .......................................................................539. How do I sort on multiple columns? ....................................................................................................5410. How do I hook up events on the editing control? .............................................................................5811. When should I remove event handlers from the editing control? ...................................................5812. How do I handle the SelectedIndexChanged event? ........................................................................5813. How do I perform drag and drop reorder of rows? ...........................................................................5914. How do I make the last column wide enough to occupy all the remaining client area of the grid? 6015. How do I have the cell text wrap? .....................................................................................................6016. How do I make the image column not show any images? ................................................................6117. How do I enable typing in the combo box cell? ................................................................................6118. How do I have a combo box column display a sub set of data based upon the value of a different combo box column? ......................................................................................................................................................6119. How do I show the error icon when the user is editing the cell? ....................................................6220. How do I show unbound data along with bound data? ....................................................................6521. How do I show data that comes from two tables? .............................................................................6622. How do I show master-details? ..........................................................................................................6623. How do I show master-details in the same DataGridView? .............................................................6824. How do I prevent sorting? ..................................................................................................................6825. How do I commit the data to the database when clicking on a toolstrip button? ..........................6826. How do I display a confirmation dialog when the user tries to delete a row? ................................68Page 3 of 69

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->