在默认情况下,datagridview的显示效果:
1。禁用最后一行空白。
默认情况下,最后一行空白表示自动新增行,对于需要在控件中进行编辑,可以保留
dataGridView1。AllowUserToAddRows=false;
上述禁用,仅是将用户界面交互的自动新增行禁了,但还是可以通过代码:dataGridView1.Rows.Add();来新增一行空白。
2。禁用“删除”键的删除功能。
默认情况,鼠标选中一整行,按删除键可以删除当前一整行
dataGridView1。AllowUserToDeleteRows=false; >之前上述禁用,仅是将用户界面交互的自动新增行禁了,但还是可以通过代码:
dataGridView1.Rows。Remove (DataGridViewRow DataGridViewRow);或者
dataGridView1.Rows。RemoveAt (int指数);来删除指定行数据。
3。启用鼠标拖拽列功能
启用后,可以通过鼠标拖拽,对列的顺序进行重排序。但是拖拽不会影响各列通过代码访问时的列序号(保持原来的序号),只是展示效果变化。
dataGridView1。AllowUserToOrderColumns=true;>之前4。禁用鼠标拖动行高度,列宽度
禁用后,不能通过鼠标交互改变列的宽度和行的高度。不影响通过代码设置
dataGridView1。AllowUserToResizeColumns=false;//禁拖动列宽度 dataGridView1。AllowUserToResizeRows=false;//禁拖动行高度 >之前5。禁用鼠标拖动行标题(最左侧空白列)宽度
dataGridView1。RowHeadersWidthSizeMode=DataGridViewRowHeadersWidthSizeMode.DisableResizing;//枚举,可以枚举位自适应大小,
6。禁用单元格编辑功能
dataGridView1。只读的=true; >之前7。点击选中整行,整列
dataGridView1。SelectionMode=DataGridViewSelectionMode.FullRowSelect;//单击选中整行,枚举SelectionMode为枚举类型:
dataGridView1。多?false; >之前
9。设置表格网格线颜色等样式
dataGridView1.AdvancedCellBorderStyle。顶级=DataGridViewAdvancedCellBorderStyle.InsetDouble;//设置边框样式(上边框),枚举:双线内陷边框//? dataGridView1。GridColor=Color.SeaGreen;//边框线颜色 >之前10。自动行序号
没有直接的设置属性,需要借助控件渲染事件:dataGridView1.CellPainting +=dataGridView1_CellPainting;
//在单元格需要绘制时发生。 私人空间dataGridView1_CellPainting(对象发送方,DataGridViewCellPaintingEventArgs e) { 如果(e。ColumnIndex & lt;0,,e。RowIndex祝辞=0)//绘制自动序号 { e.Paint (e。ClipBounds DataGridViewPaintParts.All); 矩形vRect=e.CellBounds; vRect。充气(2,2); TextRenderer.DrawText (e。图形(e。e.CellStyle RowIndex + 1) .ToString ()。字体、vRect e.CellStyle。前景色,TextFormatFlags。对| TextFormatFlags.VerticalCenter); e。处理=true; }//- - - - - -其它样式设置- - - - - - - 如果(e。RowIndex % 2==0) {//行序号为双数(含0)时 e.CellStyle。背景色=Color.White; } 其他的 { e.CellStyle。背景色=Color.Honeydew; } e.CellStyle。SelectionBackColor=Color.Gray;//选中单元格时,背景色 e.CellStyle。对齐=DataGridViewContentAlignment.MiddleCenter;//单位格内数据对齐方式 } >之前显示效果:
,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
c#窗体控件DataGridView常用设置