Question : C# Windows Application - Datagridview autonumber

Dear Experts

In my C# windows application, I have a unbound datagridview. What I want is
1 - When user inserts a row, it should display the autonumber staring from 1
2 - Let us say user inserts 4 rows. The numbering will 1,2,3,4. Now user deletes row 3, then it should renumber as 1,2,3.

Answer : C# Windows Application - Datagridview autonumber

Hi!

see this as well. Images have some description to understand.


-Shahan
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Columns.Add("Autonumber", "Autonumber");
            dataGridView1.Columns.Add("Name", "Name");
            dataGridView1.Columns.Add("Address", "Address");
            for (int i = 0; i < 4; i++)
            {
                DataGridViewRow dr = new DataGridViewRow();
                dr.CreateCells(dataGridView1,new object[] {(i+1), "Shahan" + (i+1),"fdsfasfds"});
                dataGridView1.Rows.Add(dr);  
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
                dataGridView1.Rows.RemoveAt(2); //to remove 3rd record; 3-1 = 2 , because index is zero-based
                for (int i = 2; i < 4-1; i++) // 4-1, because 1 row has been deleted.
                    dataGridView1.Rows[i].Cells[0].Value = ((int)dataGridView1.Rows[i].Cells[0].Value) - 1;
        }
Random Solutions  
 
programming4us programming4us