C# DataGrid 控件在winform里显示行号

        #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


posted @ 2013-02-06 14:35  真爱无限  阅读(288)  评论(0编辑  收藏  举报