code:
#region 行绘制事件 ,为DataGridView每行写上序号 /// <summary> /// 行绘制事件 ,为DataGridView每行写上序号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, this.dataGridView.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.dataGridView.RowHeadersDefaultCellStyle.Font, rectangle, this.dataGridView.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); } #endregion