C# Datagridview 首行自動顯示行號——RowPostPaint事件
阿新 • • 發佈:2018-12-11
點選控制元件的事件按鈕,找到RowPostPaint事件;
雙擊事件名稱的右邊空白部分建立事件的函式;
然後在函式中新增如圖中的程式碼;
private void DGV_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { Rectangle rect = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, DGV.RowHeadersWidth - 4, e.RowBounds.Height); TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), DGV.RowHeadersDefaultCellStyle.Font, rect, DGV.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right); }
效果如下,行號都顯示在首列。
Datagridview 首列新增行號的方法不唯一,還有其他的方法,各路大神可以多多嘗試。