datagridview 控件左侧的固定列能显示出序号吗?
------------------------------------------------
我看到有的程序界面上,左侧固定列是可以显示序号的,但是我不知如何实现的。
左侧的固定列有点宽,而且有一个右箭头,所以我想让这一列能显示出递增的序号
请问如何实现呢?
我确实看到有的程序中用到这个控件,可以显示序列号呀。

解决方案 »

  1.   

    [code=C#][
    int rowNumber = 1; 
      foreach (DataGridViewRow row in DataGridView1.Rows) 
      { 
      row.HeaderCell.Value = rowNumber.ToString(); 
      rowNumber++; 
      } 
    /code]
      

  2.   


    int rowNumber = 1; 
      foreach (DataGridViewRow row in DataGridView1.Rows) 
      { 
      row.HeaderCell.Value = rowNumber.ToString(); 
      rowNumber++; 
      } 
      

  3.   

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

  4.   

    private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)  
      {  
      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);  
      }  
      }
      

  5.   

    3#的可以,不过再加个判断更好,避免不必要的序号绘制if(e.RowIndex >= dataGridView1.FirstDisplayedScrollingRowIndex)
    {
        using (SolidBrush b = new SolidBrush(dataGridOnlineCustomer.RowHeadersDefaultCellStyle.ForeColor))
      {
      string linenum = e.RowIndex.ToString();
      int 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);
      }}
      

  6.   


    我如何调用呢?这样调用不行,第二个参数必须要指定,可是第二个参数我如何输入呢?
    dataGridView1_RowPostPaint(dataGridView1,null);
      

  7.   


            private void dataGridView1_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
            {
                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);
                    }
                }
            }
    谢,这样的代码可以实现加序号的功能,但是发现一个细节,不知能否解决,
    就是行指示器(即那个向右的箭头),会与序号重合,不好看,
    这个问题应该如何处理呢?
      

  8.   

    <asp:TemplateColumn HeaderText="序号">
    <HeaderStyle HorizontalAlign="Center" Wrap="False" Width="40px"></HeaderStyle>
    <ItemStyle Wrap="False" HorizontalAlign="Center" Width="40px"></ItemStyle>
    <ItemTemplate>
    <asp:Label runat="server" Text='<%#MainGrid.Items.Count +1%>' ID="Label4" NAME="Label1">
    </asp:Label>
    </ItemTemplate>
    </asp:TemplateColumn>
    这样可以显示出行数很简单的
      

  9.   


    既然能画序号,背景自然也能画掉
    先画背景,再画序号
     private void dataGridView1_RowPostPaint_1(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);
                    }
                }
            }Rectangle rowHeaderBounds 
    这个矩形区域,你自己可以再调整一下