在Microsoft Visual Studio 2005中,如何给DataGrid的RowHeader添加序列号?
例如第一行就是1,第二行2,第三行3...如此类推...
如何实现呢?

解决方案 »

  1.   

    void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
      {
            
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
          // Display the company name in italics.
          e.Row.Cells[0].Text = (e.RowGridViewRow.RowIndex + 1).ToString();
            
        }
      }
      

  2.   

    加一个模板列,在HTML里加上下面的代码
    <ItemTemplate>
    <%# Container.ItemIndex+1 %>
    </ItemTemplate>
      

  3.   

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