Click here to Skip to main content
15,946,988 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I'm trying to programmatically update my data in a dataGridView1 table. So, but pressing a button I add another row of data and it has an sequential index number (eg 1, 2, 3, 4... etc).

I have also a delete row button which will delete selected rows - what I'm trying to achieve is to update the index value for all the rows so when I delete lets say row 3 it will still count 1, 2, 3, 4 and not 1,2,4,5. Any ideas? The "index" is the line number, hence has to be sequential.


Thanks.

What I have tried:

<pre lang="c#">
        private void buttonDeleteRow_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dataGridView1.SelectedRows)
            {
                if (!row.IsNewRow)
                { this.dataGridView1.Rows.RemoveAt(row.Index); }
            }
        }
Posted
Updated 7-May-18 8:30am

1 solution

Ok, I found the solution!

foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (!row.IsNewRow)
                { row.Cells["Item"].Value = row.Index+1; }
            }
 
Share this answer
 
Comments
Maciej Los 7-May-18 15:53pm    
Good work, Kamil!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900