private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rowHeaderBounds = new Rectangle (2, e.RowBounds.Top,this.dataGridView1.RowHeadersWidth - 2, e.RowBounds.Height - 1);            using (Brush backbrush = new SolidBrush(SystemColors.Control))
            {
                e.Graphics.FillRectangle(backbrush, rowHeaderBounds);
            }            if (e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex)
            {
                using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
                {
                    int linen = 0;
                    linen = e.RowIndex + 1;
                    string line = linen.ToString();
                    e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
                    SolidBrush B = new SolidBrush(Color.Red);
                }
            }        }给datagridview添加序列号的代码。。

解决方案 »

  1.   


            //DataGrid的RowPostPaint事件
            private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                //取得要画图的区域
                Rectangle rowHeaderBounds = new Rectangle(2, e.RowBounds.Top, this.dataGridView1.RowHeadersWidth - 2, e.RowBounds.Height - 1);            //定义Brush
                using (Brush backbrush = new SolidBrush(SystemColors.Control))
                {
                    //填充区域
                    e.Graphics.FillRectangle(backbrush, rowHeaderBounds);
                }
                //判断如果列索引是第一个显示的列的索引
                if (e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex)
                {
                    using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
                    {
                        int linen = 0;
                        linen = e.RowIndex + 1;
                        string line = linen.ToString();
                        //写上文字
                        e.Graphics.DrawString(line, e.InheritedRowStyle.Font, b, e.RowBounds.Location.X, e.RowBounds.Location.Y + 5);
                        SolidBrush B = new SolidBrush(Color.Red);
                    }
                }        }
      

  2.   

                if (e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex)
    这句还是不太明白。、
      

  3.   

    判断你的DATAGRIDVIEW1里面有没有行?如果有的话,就添加序列号