实现效果:
实现代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace DataGridViewLineNum { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“companyDataSet.clerk”中。您可以根据需要移动或删除它。 this.clerkTableAdapter.Fill(this.companyDataSet.clerk); } private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { //绘制矩形区域 Rectangle rect = new Rectangle(e.RowBounds.Location.X,e.RowBounds.Location.Y,dataGridView1.RowHeadersWidth-10,e.RowBounds.Height); //绘制行号 TextRenderer.DrawText(e.Graphics,(e.RowIndex+1).ToString(),dataGridView1.RowHeadersDefaultCellStyle.Font,rect,dataGridView1.RowHeadersDefaultCellStyle.ForeColor,TextFormatFlags.VerticalCenter|TextFormatFlags.Right); } } }