[Winform]DataGridView列自适应宽度
2020-12-13 16:45
标签:des winform datagridview style blog http io color ar 在做winform项目中,数据控件DataGridView的使用多多少少是会用到的,如果不设置它的属性,默认情况下是不会自适应宽度的,你想查看某项的数据,就不得不将标题栏拖来拖去,挺烦的。 建一个用于测试的Person类 winform绑定数据源,及自适应列宽代码 主要用到了DataGridViewAutoSizeColumnMode枚举,也可以通过设置DataGridView的属性来实现 没设置自适应列宽的 使用自适应列宽的 用到的东西很简单,只是在项目中用到了,对winform使用较少,就研究上网查了一下,这样可以提高用户体验,既然用到了就记录一下,方便自己,也方便他人。 参考: http://blog.csdn.net/ou_yangpengfei/article/details/6051701 其他网络资源 [Winform]DataGridView列自适应宽度 标签:des winform datagridview style blog http io color ar 原文地址:http://www.cnblogs.com/jameslif/p/4086535.html引言
方法
1 public class Person
2 {
3 [Description("姓名")]
4 public string Name { get; set; }
5 [Description("性别")]
6 public string Gender { get; set; }
7 [Description("地址")]
8 public string Address { get; set; }
9 }
1 public partial class PersonInfoForm : Form
2 {
3 public PersonInfoForm()
4 {
5 InitializeComponent();
6 }
7 int width = 0;
8 private void PersonInfoForm_Load(object sender, EventArgs e)
9 {
10 List
1 // 摘要:
2 // 定义用于指定如何调整列宽的值。
3 public enum DataGridViewAutoSizeColumnMode
4 {
5 // 摘要:
6 // 列的大小调整行为从 System.Windows.Forms.DataGridView.AutoSizeColumnsMode 属性继承。
7 NotSet = 0,
8 //
9 // 摘要:
10 // 列宽不会自动调整。
11 None = 1,
12 //
13 // 摘要:
14 // 调整列宽,以适合列标题单元格的内容。
15 ColumnHeader = 2,
16 //
17 // 摘要:
18 // 调整列宽,以适合该列中的所有单元格的内容,不包括标题单元格。
19 AllCellsExceptHeader = 4,
20 //
21 // 摘要:
22 // 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格。
23 AllCells = 6,
24 //
25 // 摘要:
26 // 调整列宽,以适合当前屏幕上显示的行的列中的所有单元格的内容,不包括标题单元格。
27 DisplayedCellsExceptHeader = 8,
28 //
29 // 摘要:
30 // 调整列宽,以适合当前屏幕上显示的行的列中的所有单元格的内容,包括标题单元格。
31 DisplayedCells = 10,
32 //
33 // 摘要:
34 // 调整列宽,使所有列的宽度正好填充控件的显示区域,只需要水平滚动保证列宽在 System.Windows.Forms.DataGridViewColumn.MinimumWidth
35 // 属性值以上。 相对列宽由相对 System.Windows.Forms.DataGridViewColumn.FillWeight 属性值决定。
36 Fill = 16,
37 }
结果
总结
上一篇:phpmyadmin常见错误
下一篇:c#基础01