DateGridView的一些技巧

如果要把某个List的内容直接绑定到DGV,需要像下面这样做,否则内容无法显示出来:

        struct ReportListStringItem
        {
            public string reportDate { get; set; }
            public string projectName { get; set; }
            public string taskName { get; set; }
            public string timeSpent { get; set; }
            public string activity { get; set; }
        }
        List<ReportListStringItem> repoortListString;

 

                dgvHistory.AutoGenerateColumns = true;
                dgvHistory.DataSource = repoortListString;
                dgvHistory.ClearSelection();

 

 

如果上面绑定的List是一个没有内容的List,注意不是null,只是没有内容,在重新绑定之后如果点击DGV,会出现“索引-1没有价值”类似的异常,建议绑定前判断List至少有一项内容。

 

20120328 Update:

今天在CodeSmith生成的实体类中加了一个属性,但是怎么都显示不出来,后来看了看实体基类的代码,加上了下面的代码,ok了。

        [Bindable(BindableSupport.Yes)] 
        public string CustomerName
        {
            get;
            set;
        }

 

 

20120710 Add:

今天再次碰到一个问题,在DataGridViewX(注意不是DataGridView)中有一个复选框列,要通过编程的方式选中复选框,但是搜索网上的文章,99%都提供的是类似这样的方法:

 (dataGridView1.Rows[0].Cells[0] as DataGridViewCheckBoxCell).Value = true;

 但是我反复试了都不行,最后在MSDN论坛上找到一个回复解决了这个问题,至于为什么上面这种做法不行也只能暂时存疑了。

这个方法就是在绑定的数据源当中加上一列专门用来绑定到复选框的数据列,类似这样:

         [Bindable(BindableSupport.Yes)]

        public bool OnlyForCheck
        {
            get;
            set;
        }

转载于:https://www.cnblogs.com/s5689412/archive/2011/10/08/2202266.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本文档不准备面面俱到地介绍DataGridView,而是着眼于深入地介绍一些技术点的高级特性。 本文档按逻辑分为5个章节,首先是结构和特性的概览,其次是内置的列/单元格类型的介绍,再次是数据操作相关的内容,然后是主要特性的综述,最后是最佳实践。 大部分章节含有一个“Q & A”部分,来回答该章节相关的一些常见问题。注意,某些问题会由于知识点的关联性重复出现在多个章节。这些问题、答案及其附带的示例代码都包含在本文档的附录部分。 一、DataGridView技术点的高级特性。 11 1 何为DataGridView 11 1.1 DataGridView和DataGrid 之间的区别 11 1.2 DataGridView的亮点 12 2 DataGridView的结构 13 2.1 结构元素(Architecture Elements) 13 2.2 单元格和组(Cells and Bands) 13 2.3 DataGridView的单元格 (DataGridViewCell) 13 2.3.1 DataGridViewCell的工作机制 14 2.3.2 常见问题 15 2.4 DataGridView的列(DataGridViewColumn) 16 2.5 DataGridView的编辑控件(Editing Controls) 16 2.6 DataGridViewRow 17 2.6.1 常见问题 17 3 列/单元格类型揭密(column/cell types) 17 3.1 DataGridViewTextBoxColumn 18 3.2 DataGridViewCheckBoxColumn 19 3.3 DataGridViewImageColumn 19 3.4 DataGridViewButtonColumn 19 3.5 DataGridViewComboBoxColumn 20 3.5.1 DataError事件和ComboBox列 20 3.5.2 常见问题 20 3.6 DataGridViewLinkColumn 21 4 操作数据(Working with Data) 21 4.1 数据输入和验证的相关事件 21 4.1.1 数据验证相关事件的顺序 21 4.1.2 验证数据 22 4.1.3 在新行中的数据输入(Data Entry in the New Row) 22 4.1.3.1 显示新行 22 4.1.3.2 为生成的新行添加默认值 22 4.1.3.4 在新行中输入数据 23 4.1.3.5 自定义新行的可视化效果 23 4.1.3.6 新行的排序 24 4.1.3.7 关于新行,还要注意: 24 4.1.3.8 Virtual Mode下的新行 24 4.2 关于Null值 24 4.2.1 NullValue属性 24 4.2.2 DataSourceNullValue属性 25 4.3 DataError事件 25 4.4 数据绑定模式(Databound modes) 26 4.4.1 非绑定模式(Unbound Mode) 26 4.4.2 绑定模式(Bound Mode) 26 4.4.2.1 有效的数据源 27 4.4.3 虚拟模式 27 4.4.4 混合模式 – 绑定与非绑定模式 27 4.4.5 常见问题 28 5 特性综览(Overview of features) 28 5.1 样式(Styling) 28 5.1.1 The DataGridViewCellStyle Class 29 5.1.2 Using DataGridViewCellStyle Objects 29 5.1.3 Style Inheritance 30 5.1.4 Setting Styles Dynamically 34 5.2 Custom painting 35 5.2.1 Paint Parts 35 5.3.1 在Windows窗体DataGridView控件调整大小选项 39 5.3.2 Resizing with the Mouse用鼠标调整大小 42 5.3.3 Automatic Sizing自动调整大小 43 5.3.4 Programmatic Resizing编程调整大小 45 5.3.5 Customizing Content-based Sizing Behavior自定义基于内容的调整大小行为 46 5.3.6 Content-based Sizing Options基于内容的调整大小选项 47 5.4 Selection modes选择模式 47 5.4.1 Programmatic Selection编程选择 49 5.5 滚动(Scrolling) 49 5.5.1 Scroll event Scroll事件 49 5.5.2 Scroll bars滚动条 50 5.5.3 Scrolling Properties滚动属性 50 5.6 Sorting排序 50 5.6.1 Programmatic Sorting编程排序 52 5.6.2 Custom Sorting自定义排序 53 5.6.3 Common questions and scenarios常见问题及案例 54 5.7 Border styles边框样式 55 5.7.1 Standard Border Styles标准边框样式 55 5.7.2 Advanced Border Styles高级边框风格 56 5.8 Enter-Edit modes输入,编辑模式 57 5.9 Clipboard copy modes剪贴板拷贝模式 58 5.10 Frozen columns/rows冻结的列/行 60 5.11 Implementing Custom cells and editing controls/cells实现自定义和编辑控制单元格/单元格 60 5.11.1 IDataGridViewEditingControl 接口 61 5.11.2 IDataGridViewEditingCell 接口 61 5.12 Virtual mode虚拟模式 61 5.12.1 Bound Mode and Virtual Mode绑定模式和虚拟模式 62 5.12.2 Supplementing Bound Mode补充绑定模式 62 5.12.3 Common questions and scenarios常见问题及案例 62 5.12.4 Replacing Bound Mode更换绑定模式 63 5.12.5 Virtual-Mode Events虚拟模式事件 63 5.12.6 Best Practices in Virtual Mode在虚拟模式下的最佳实践 66 5.13 容量(Capacity) 66 6 最佳实践(Best Practices) 67 6.1 Using Cell Styles Efficiently使用高效单元格样式 67 6.2 Using Shortcut Menus Efficiently使用高效快捷菜单 68 6.3 Using Automatic Resizing Efficiently使用自动调整大小高效 69 6.4 Using the Selected Cells, Rows, and Columns Collections Efficiently高效使用选定的单元格,行和列的集合 69 6.5 Using Shared Rows 使用共享行 70 6.6 Preventing Rows from Becoming Unshared 防止行成为非共享 72 附录 A – FAQ 75 1. 如何使指定的单元格不可编辑? 75 2. 如何让一个单元格不可用(disable)? 75 3. 如何避免用户将焦点设置到指定的单元格? 77 4. 如何使所有单元格总是显示控件(不论它是否处于编辑状态)? 77 5. Why does the cell text show up with “square” characters where they should be new lines(TODO,未能实现该效果)? 78 6. 如何在单元格内同时显示图标和文本? 78 7. 如何隐藏一列? 80 8. 如何避免用户对列排序? 81 9. 如何针对多个列排序? 81 9.1 将数据绑定到DataGridView时 81 9.2 Unbound DataGridView 取消绑定 82 9.2.1 Custom Sorting Using the SortCompare Event 使用排序结束时间实现用户自定义排序 82 9.2.2 Custom Sorting Using the IComparer Interface使用IComparer接口实现自定义排序 84 10. 如何为编辑控件添加事件处理函数? 86 11. 应在何时移除编辑控件的事件处理函数? 87 12. 如何处理ComboBox列中控件的SelectIndexChanged事件? 87 13. 如何通过拖放调整行的顺序? 87 14. 如何调整最后一列的宽度使其占据网格的剩余客户区? 89 15. 如何让TextBox类型的单元格支持换行? 89 16. 如何使Image列不显示任何图像(字段值为null时)? 90 17. 如何能够在ComboBox类型的单元格中输入数据? 90 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(TODO)? 91 19. 如何在用户编辑控件的时候(而不是在验证时)就显示错误图标? 92 20. 如何同时显示绑定数据和非绑定数据? 94 21. How do I show data that comes from two tables(TODO)?如何显示来自两个数据源的数据? 96 22. 如何显示主从表? 97 23. 如何在同一DataGridView中显示主从表? 99 24. 如何避免用户对列排序? 99 25. 如何在点击工具栏按钮的时候将数据提交到数据库? 99 26. 如何在用户删除记录时显示确认对话框? 99
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值