Show row number in row header of a DataGridView

前端 未结 11 2328
野性不改
野性不改 2020-12-01 06:17

Is it possible to show row number in the row header of a DataGridView?

I\'m trying with this code, but it doesn\'t work:

    pri         


        
11条回答
  •  独厮守ぢ
    2020-12-01 06:55

    row.HeaderCell.Value = row.Index + 1;

    when applied on datagridview with a very large number of rows creates a memory leak and eventually will result in an out of memory issue. Any ideas how to reclaim the memory?

    Here is sample code to apply to an empty grid with some columns. it simply adds rows and numbers the index. Repeat button click a few times.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            dataGridView1.SuspendLayout();
            for (int i = 1; i < 10000; i++)
            {
                dataGridView1.Rows.Add(i);                
            }
            dataGridView1.ResumeLayout();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
                row.HeaderCell.Value = (row.Index + 1).ToString();
        }
    }
    

提交回复
热议问题