为Winform程序中DataGridView控件增加自动显示行号功能
网上找了几种显示行号的方法,采用了其中一种,在行头显示行号,很实用,如图:
代码如下:
private void gvTransferList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, gvTransferList.RowHeadersWidth, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), gvTransferList.RowHeadersDefaultCellStyle.Font, rectangle, gvTransferList.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); }