在win开发的过程中,显示数据是必不可少的,而DataGridView是最常用的控件之一。我在运用过程中发现比人有显示行号,这对用户来说是很友好的。 于是我搜集资料,找找应用,发现实现行号显示与否是很容易的。记录于此,以便查阅。 一、用控件自身OnPaint绘图方式: #region "绘制行号" protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { base.OnPaint(e); //显示行号 if (DisplayRowNumber && this.RowHeadersVisible && this.ColumnCount > 0) { if (this.DataSource == null) return; int iRowNumStart = 0; int yPos = 0; int iRowNumEnd = iRowNumStart + this.RowCount ; while (iRowNumStart < iRowNumEnd) { yPos = this.GetRowDisplayRectangle(iRowNumStart++,true ).Y + 6; string strRowNum = string.Format(" {0}", iRowNumStart); e.Graphics.DrawString(strRowNum, this.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Black),0, yPos); } } } #endregion 二、向控件的行标写行号: void dataGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e) { int v_lineNo = 0; v_lineNo = e.RowIndex + 1; string v_line = v_lineNo.ToString(); dataGrid.Rows[e.RowIndex].HeaderCell.Value = v_line; } 至于两者的用法有什么弊端,这个道没有研究,个人倾向于第二种,好像sql server 2005网格结果显示的行号也是这种方式
DataGrideView绘制行号
最新推荐文章于 2017-11-26 20:57:00 发布