找传奇、传世资源到传世资源站!

C#_DataGridView控件使用全攻略

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

【例子介绍】C# DataGridView控件使用完全攻略 

DataGridView是用于Windows Froms 2.0的新网格控件。它可以取代先前版本中DataGrid控 件,它易于使用并高度可定制,支持很多我们的用户需要的特性。 关于本文档: 本文档不准备...

C# DataGridView 控件使用完全攻略
DataGridView 是用于 Windows Froms 2.0 的新网格控件。它可以取代先前版本中 DataGrid 控
件,它易于使用并高度可定制,支持很多我们的用户需要的特性。
关于本文档:
本文档不准备面面俱到地介绍 DataGridView,而是着眼于深入地介绍一些技术点的高级特
性。
本文档按逻辑分为 5 个章节,首先是结构和特性的概览,其次是内置的列/单元格类型的介
绍,再次是数据操作相关的内容,然后是主要特性的综述,最后是最佳实践。
大部分章节含有一个“Q & A”部分,来回答该章节相关的一些常见问题。注意,某些问题会由
于知识点的关联性重复出现在多个章节。这些问题、答案及其附带的示例代码都包含在本文档的附
录部分。
注意:本文档的大部分内容来自 MSDN 中 DataGridView 控件的相关文档。
内容
1 何为DataGridView.........................................................................................................................................4
1.1 DataGridView和DataGrid 之间的区别 ...................................................................................................4
1.2 DataGridView的亮点................................................................................................................................4
2 DataGridView的结构................................................................................................... 错误!未定义书签。
2.1 结构元素....................................................................................................................................................6
2.2 单元格和组................................................................................................................................................6
2.3 DataGridView的单元格............................................................................................................................6
2.3.1 DataGridViewCell的工作机制 .......................................................................................................7
2.4 DataGridView的列 ...................................................................................................................................8
2.5 DataGridView的编辑控件........................................................................................................................9
2.6 DataGridView的行 .................................................................................................................................10
3 列/单元格类型揭密......................................................................................................................................11
3.1 DataGridViewTextBoxColumn..............................................................................................................11
3.2 DataGridViewCheckBoxColumn...........................................................................................................12
3.3 DataGridViewImageColumn..................................................................................................................12
3.4 DataGridViewButtonColumn.................................................................................................................12
3.5 DataGridViewComboBoxColumn..........................................................................................................13
3.5.1 DataError与ComboBox列.............................................................................................................13
3.6 DataGridViewLinkColumn ....................................................................................................................13
4 操作数据.......................................................................................................................................................15
4.1 数据输入和验证的相关事件..................................................................................................................15
4.1.1 数据验证相关事件的顺序 ............................................................................................................15
4.1.2 验证数据........................................................................................................................................15
4.1.3 在新行中的数据输入....................................................................................................................16
4.2 关于Null值..............................................................................................................................................18
4.2.1 NullValue属性...............................................................................................................................19
4.2.2 DataSourceNullValue属性............................................................................................................19
4.3 DataError事件.........................................................................................................................................19
4.4 数据绑定模式..........................................................................................................................................20
4.4.1 非绑定模式....................................................................................................................................20
Page 1 of 67
DataGridView 控件.doc
4.4.2 绑定模式........................................................................................................................................21
4.4.3 虚拟模式........................................................................................................................................21
4.4.4 混合模式........................................................................................................................................22
5 Overview of features....................................................................................................................................23
5.1 Styling......................................................................................................................................................23
5.1.1 The DataGridViewCellStyle Class................................................................................................23
5.1.2 Using DataGridViewCellStyle Objects.........................................................................................23
5.1.3 Style Inheritance ...........................................................................................................................24
5.1.4 Setting Styles Dynamically............................................................................................................26
5.2 Custom painting......................................................................................................................................27
5.2.1 Paint Parts......................................................................................................................................27
5.2.2 Row Pre Paint and Post Paint.......................................................................................................28
5.3 Autosizing................................................................................................................................................28
5.3.1 Sizing Options in the Windows Forms DataGridView Control..................................................29
5.3.2 Resizing with the Mouse...............................................................................................................30
5.3.3 Automatic Sizing ...........................................................................................................................31
5.3.4 Programmatic Resizing .................................................................................................................32
5.3.5 Customizing Content-based Sizing Behavior...............................................................................32
5.3.6 Content-based Sizing Options.......................................................................................................33
5.4 Selection modes.......................................................................................................................................33
5.4.1 Programmatic Selection................................................................................................................34
5.5 Scrolling...................................................................................................................................................34
5.5.1 Scroll event....................................................................................................................................34
5.5.2 Scroll bars.......................................................................................................................................34
5.5.3 Scrolling Properties.......................................................................................................................34
5.6 Sorting .....................................................................................................................................................35
5.6.1 Programmatic Sorting ...................................................................................................................36
5.6.2 Custom Sorting..............................................................................................................................37
5.7 Border styles............................................................................................................................................37
5.7.1 Standard Border Styles ..................................................................................................................38
5.7.2 Advanced Border Styles ................................................................................................................38
5.8 Enter-Edit modes ....................................................................................................................................39
5.9 Clipboard copy modes.............................................................................................................................39
5.10 Frozen columns/rows..............................................................................................................................40
5.11 Implementing Custom cells and editing controls/cells.........................................................................40
5.11.1 IDataGridViewEditingControl......................................................................................................40
5.11.2 IDataGridViewEditingCell............................................................................................................41
5.12 Virtual mode............................................................................................................................................41
5.12.1 Bound Mode and Virtual Mode....................................................................................................41
5.12.2 Supplementing Bound Mode.........................................................................................................41
5.12.3 Replacing Bound Mode.................................................................................................................41
5.12.4 Virtual-Mode Events.....................................................................................................................42
5.12.5 Best Practices in Virtual Mode......................................................................................................43
5.13 Capacity...................................................................................................................................................43
6 Best Practices................................................................................................................................................44
6.1 Using Cell Styles Efficiently...................................................................................................................44
6.2 Using Shortcut Menus Efficiently..........................................................................................................44
6.3 Using Automatic Resizing Efficiently....................................................................................................44
6.4 Using the Selected Cells, Rows, and Columns Collections Efficiently.................................................45
Page 2 of 67
DataGridView 控件.doc
6.5 Using Shared Rows..................................................................................................................................45
6.6 Preventing Rows from Becoming Unshared..........................................................................................46
附录 A – 常见问题:............................................................................................................................................48
1. 如何使指定的单元格不可编辑? ............................................................................................................48
2. 如何让一个单元格不可用? ....................................................................................................................48
3. 如何避免用户将焦点设置到指定的单元格?......................................................................................50
4. 如何使所有单元格总是显示控件(不论它是否处于编辑状态)?..........................................................50
5. Why does the cell text show up with “square” characters where they should be new lines? ............50
6. 如何在单元格内同时显示图标和文本?..............................................................................................50
7. 如何隐藏一列?......................................................................................................................................52
8. 如何避免用户对列排序?......................................................................................................................52
9. 如何针对多个列排序?............................................................................................................................52
10. 如何为编辑控件添加事件处理函数? ....................................................................................................57
11. 应在何时移除编辑控件的事件处理函数?.............................................................................................57
12. 如何处理ComboBox列中ComboBox控件的SelectIndexChanged事件?..............................................57
13. 如何通过拖放调整行的顺序?..............................................................................................................58
14. 如何调整最后一列的宽度使其占据网格的剩余客户区?..................................................................59
15. 如何让TextBox类型的单元格支持换行?...............................................................................................59
16. 如何使Image列不显示任何图像(字段值为null时)?.............................................................................59
17. 如何能够在ComboBox类型的单元格中输入数据?..............................................................................60
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?.............................................................................................................................................................60
19. 如何在用户编辑控件的时候(而不是在验证时)就显示错误图标?......................................................61
20. 如何同时显示绑定数据和非绑定数据?..............................................................................................63
21. How do I show data that comes from two tables?.................................................................................65
22. 如何显示主从表?..................................................................................................................................65
23. 如何在同一DataGridView中显示主从表?..........................................................................................67
24. 如何避免用户对列排序?........................................................................................................................67
25. 如何在点击工具栏按钮的时候将数据提交到数据库?......................................................................67
26. 如何在用户删除记录时显示确认对话框?..........................................................................................67

【相关图片】

C#_DataGridView控件使用全攻略 C#语言基础-第1张

C#_DataGridView控件使用全攻略 C#语言基础-第2张C#_DataGridView控件使用全攻略 C#语言基础-第3张

【源码结构】
.
├── C#_DataGridView控件使用全攻略.pdf
├── C#_DataGridView控件使用全攻略.zip
└── 5t6t网_C#_DataGridView控件使用全攻略.zip

0 directories, 3 files

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复