关于datagridview的一些操作

1、绑定datatable时,会显示出不需要显示的列可以加datagridview.AutoGenerateColumns = false;

2、如果datagridview的某列是数值型的,有小数,如果需要固定显示的小数,

只需在初始化的时候,sl.DefaultCellStyle.Format = "0.00";或者sl.DefaultCellStyle.Format = "N2"//表示sl这一列只显示2位小数,千分位

sl.DefaultCellStyle.Format = "d" 表示为时间格式

3、check.Frozen = true;表示datagridview的该列不动,拉动滚动条也不动

4、当需要显示列的序号时,可以用datagridview.RowPostPaint

private void dgv_RowPostPaint(object sender,DataGridViewRowPostPaintEventArgs e)

{

using(SolidBrush b = new SolidBrush(dgv.RowHeaderDefaultCellStyle.ForeColor))

e.Graphics.DrawString(Convert.ToString(e.RowIndex+1),e.InheritedRowStyle.Font,b,e.RowBounds.Location.X,e.RowBounds.Location.Y);

}

原文地址:https://www.cnblogs.com/JohnnyBao/p/3850304.html