DataGridView显示行号

     /// <summary>

        /// DataGridView显示行号

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void dataGridWebResourceView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)

        {

            string strRowNumber = (e.RowIndex + 1).ToString();

            while (strRowNumber.Length < dataGridWebResourceView.RowCount.ToString().Length) strRowNumber = "0" + strRowNumber;

            SizeF size = e.Graphics.MeasureString(strRowNumber, this.Font);

            if (dataGridWebResourceView.RowHeadersWidth < (int)(size.Width + 20)) dataGridWebResourceView.RowHeadersWidth = (int)(size.Width + 20);



            Brush b = SystemBrushes.ControlText;

            e.Graphics.DrawString(strRowNumber, this.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));

        }

  

你可能感兴趣的:(datagridview)