winfrom给DataGridView的RowHead添加序号

winfrom给DataGridView的RowHead添加序号:

    public class ClsPubFuctions
    {
        public static void DisplayRowIndex(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
        {
            dgv.TopLeftHeaderCell.Value = "序号";
            using (SolidBrush sb = new SolidBrush(dgv.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, CultureInfo.CurrentUICulture),
                                      e.InheritedRowStyle.Font, sb, e.RowBounds.Location.X + 14,
                                      e.RowBounds.Location.Y + 5);
            }
        }
    }

调用方法(DataGridView的RowPostPaint事件):

        private void dgv_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ClsPubFuctions.DisplayRowIndex(dgv, e);
        }

实际效果:

winfrom给DataGridView的RowHead添加序号_第1张图片

你可能感兴趣的:(winfrom给DataGridView的RowHead添加序号)