该内容为自己在使用C# dataGridView1时的一些功能备忘,专业勿喷!
一、不显示出dataGridView1的最后一行空白
dataGridView1.AllowUserToAddRows = false;
二、固定行高显示
DataGridView控件的属性:
1、ColumnHeadersHeaderSize属性设为 EnableResizing
2、ColumnHeadersHeader 的值改为 25
3、RowTemplate属性下的Height,把值也设置为 25
4、设置AutoSizeRowsMode为none
SortMode是DataColumn的属性值,通过DataGridView获取DataColumn,然后将DataColumn的SortMode属性设置也可以达到同样的目的。
DataGridView中的Columns属性里面可以设置。进入“Edit Columns”窗口后,在相应的列属性设置for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
this.dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
this.dataGridView1.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}里面把SortMode属性选择为"NotSortable"就可以了。
四、 第一列显示DataGridView序号列
private void dgvData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
DataGridView dgv = sender as DataGridView;
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
dgv.RowHeadersWidth - 4,e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),dgv.RowHeadersDefaultCellStyle.Font,
rectangle,dgv.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
五、 第一列显示DataGridView序号列