c# gridview添加一列为序号,我要在这一列添加24个序号如何写?

c# gridview添加一列为序号,我要在这一列添加24个序号如何写?
不连接数据库就直接添加1~24 这个24个数字如何添加。。

第1个回答  2014-11-04

using System.Drawing;
using System.Windows.Forms;

namespace test_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int i = 1; i < 24; i++)      //加24空行
            {
                dataGridView1.Rows.Add("", "");
            }
        }

 

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)                                        //RowPostPaint事件

        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
            e.RowBounds.Location.Y,
            dataGridView1.RowHeadersWidth - 4,
            e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
            rectangle,
            dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
            TextFormatFlags.VerticalCenter | TextFormatFlags.Right);

        }

    }
}

在dataGridView1的RowPostPaint事件加入代码,如上

本回答被提问者采纳
相似回答