DataGridView控件在顯示數據時,我們有時候需要顯示行號,以便檢索查看方便使用。
但DataGridView默認沒有設置顯示行號的屬性。
此時我們只要在DataGridView的RowPostPaint事件中進行繪制,實現其效果:
private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { SolidBrush b = new SolidBrush(this.dataGridView1.RowHeadersDefaultCellStyle.ForeColor); e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), this.dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4); }
試試看!!