如何显示DataGridView行号?

解决方案 »

  1.   

    你要如何展示?如果在text中展示 很简单在datagridview的以下事件中添加 private void dataGridView1_SelectionChanged(object sender, EventArgs e)
            {
                this.textBox1.Text = this.dataGridView1.CurrentRow.Index.ToString();
            }如果希望在dataGridView上直接添加行标头 可以如下做 不过这个代码还有些bug 仅供参考 private void dataGridView1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {            int row = 0;            int yDelta = dataGridView1.GetCellDisplayRectangle(row, 0, false).Height + 1;            int y = dataGridView1.GetCellDisplayRectangle(row, 0, false).Top + 2;            CurrencyManager cm = (CurrencyManager)this.BindingContext[dataGridView1.DataSource, dataGridView1.DataMember];            while (y < dataGridView1.Height - yDelta && row < cm.Count)
                {                //get   &   draw   the   header   text...                     string text = string.Format("{0}", row);                e.Graphics.DrawString(text, dataGridView1.Font, new SolidBrush(Color.Black), 12, y);                y += yDelta;                row++;
                }                        }       
      

  2.   

    private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString(e.RowIndex.ToString(System.Globalization.CultureInfo.CurrentCulture), 
                        dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
                }
            }
      

  3.   


            private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                //添加行号
                using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
                {
                    string linenum = e.RowIndex.ToString();
                    int linen = 0;
                    linen = Convert.ToInt32(linenum) + 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);
                }
            }
      

  4.   

    private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
         {
            当前网格单元格.value=e.RowIndex
                
         }
      

  5.   

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

  6.   

    http://blog.csdn.net/yyj135/archive/2008/06/18/2561171.aspx
      

  7.   

    当前行.头单元格.VALUE= 当前单元格.ROWINDEX
    要是有些更高的要求,请使用自定控件,还不行,[email protected]