Click here to Skip to main content
15,891,688 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The Issue Is
I am adding Rows in a Datagridview
programmatically on Button Click
As
Dim s As Integer = Val(txtSlNo.Text)
        Dim c As Integer
        If s = 0 Then
            c = dgvInvoice.Rows.Count
            s = c + 1
            dgvInvoice.Rows.Add()
        Else
            c = s - 1
        End If
        dgvInvoice.Rows(c).Cells(0).Value = s.ToString()

This Snippet is on add Button click which is adding Row
and assigning Serial No to each row.
Next I have a button Delete Row .On the click event of Delete Button
am removing the Selected Rows as Below
For Each row As DataGridViewRow In dgvInvoice.SelectedRows
           Dim response As DialogResult = _
           MessageBox.Show( _
           "Are you sure you want to delete this row?", _
           "Delete row?", _
           MessageBoxButtons.YesNo, _
           MessageBoxIcon.Question, _
           MessageBoxDefaultButton.Button2)
           If (response = DialogResult.No) Then

           Else
               dgvInvoice.Rows.Remove(row)
           End If
       Next


Now the Problem is If the user Added Five rows In DatagridView
and removed the Row having Serial no 3 or any from the Datagridview.
I want that Serial no should also change Accordingly.
I think you understanding my Question.
How to Acheive this
Please assist.
Posted
Updated 17-Sep-12 21:34pm
v2

1 solution

inside dgvInvoice's RowsRemoved event
VB
For i=0 to dgvInvoice.Rows.count-1
    dgvInvoice.Rows(i).Cells(0).Value = (i+1).ToString()
Next

Happy coding!
:)
 
Share this answer
 
v2
Comments
Karwa_Vivek 18-Sep-12 3:47am    
Superb..Thanks a Ton
Aarti Meswania 18-Sep-12 3:55am    
thanks! :)

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