DataGridView几个基本属性

DataGridView 经常用到,但是很多东西都不熟悉,以至于总去上网查,这次我整理一下,全部都记下来,再用就方便了。

1、禁止用户新建行,就是去掉最后那个行标题上带星号的那个行

dataGridView1.AllowUserToAddRows = false;

2、去掉左侧的行标题

dataGridView1.RowHeadersVisible = false;

3、禁止用户调整列和行

dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.AllowUserToResizeRows = false; 

4、禁止用户编辑单元格

dataGridView1.ReadOnly = true;

5、直接选中一整行

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

 6、将单元格内的文本居中显示

dataGridView1.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

7、将列头内的文字居中显示

dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

 8、修改当前选中行的背景色和前景色

dataGridView1.DefaultCellStyle.SelectionBackColor = Color.FromArgb(139, 185, 201);
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

9、设置用户是否可以选中多行、多列、多单元格

dataGridView1.MultiSelect = true;
原文地址:https://www.cnblogs.com/rogation/p/3445936.html