this.dataGridView1.RowHeadersVisible = false;  
可以隐藏 我想说的那列。  我现在想做的是  把这列 给添加一个自动增长的编号 。  我该怎么操作这列呢?

解决方案 »

  1.   


    private void button1_Click(object sender, EventArgs e)
    {
        DataTable dt = ((DataTable)(dataGridView1.DataSource));
        DataColumn dc = new DataColumn();
        dc.DataType = System.Type.GetType("System.Int32");  //指定该列的数据类型
        dc.AutoIncrement = true;                            //该列为自动增涨列
        dc.AutoIncrementSeed = 1;                           //初始值
        dc.AutoIncrementStep = 1;                           //增量
        dc.Caption = "id";                                  //设置列的标题
        dc.ColumnName = "序号";                             //设置 列集合对象中的列的名称,datagrid中显示该列名.
        dc.Unique = true;                                   //为此列创建唯一性约
        dc.AllowDBNull = false;                             //不允许为空
        dt.Columns.Add(dc);

      

  2.   


            /// <summary>
            /// 行头写入行号和重绘行头
            /// </summary>
            protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e) {
                Rectangle rectangle = new Rectangle(
                    e.RowBounds.Location.X,
                    e.RowBounds.Location.Y,
                    this.RowHeadersWidth - 1,
                    e.RowBounds.Height);            bool mouseOver = e.RowBounds.Contains(this.PointToClient(Cursor.Position));            LinearGradientBrush brush = new LinearGradientBrush(
                    e.RowBounds,
                    mouseOver ? Color.PeachPuff : Color.LightGray,
                    Color.WhiteSmoke,
                    LinearGradientMode.Vertical);            e.Graphics.FillRectangle(brush, rectangle);//重绘行头内部
                e.Graphics.DrawRectangle(new Pen(mouseOver ? Color.PeachPuff : Color.LightGray), rectangle);//重绘行头边框            if (this.Rows[e.RowIndex].Selected == true) {
                    brush = new LinearGradientBrush(
                    e.RowBounds,
                    Color.CadetBlue,
                    Color.WhiteSmoke,
                    LinearGradientMode.Vertical);
                    e.Graphics.FillRectangle(brush, rectangle);
                    e.Graphics.DrawRectangle(new Pen(Color.CadetBlue), rectangle);
                }            //行头写入行号
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), this.RowHeadersDefaultCellStyle.Font, rectangle, this.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);            base.OnRowPostPaint(e);
            }
      

  3.   

    private void dataGridView1_RowPostPaint(object sender,    DataGridViewRowPostPaintEventArgs e)
    {
         Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,dataGridView1.RowHeadersWidth, e.RowBounds.Height);     TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dataGridView1.RowHeadersDefaultCellStyle.Font, rectangle, dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                    TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
    }
      

  4.   

    添加datagridview的RowPostPaint事件前加上以下代码,把其中的dgvCommon替换为你的datagridviewRectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvCommon.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvCommon.RowHeadersDefaultCellStyle.Font, rectangle, dgvCommon.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);