Hi,
I need help in numbering rows in datagrid. I have a column that should number 1,2,3 so on when adding a new row. When I delete a row, those numbers should re-number still to 1,2,3. So when I delete row 2, the row that's been added as 3 should now number to 2. Then when I add new rows, these should re-number again, and so on..
My code:
'when adding a new row
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim indx As Integer = DataGridViewPassengers.Rows.Add()
DataGridViewPassengers.Rows(indx).Cells(0).Value = (indx + 1).ToString
DataGridViewPassengers.Rows(indx).Cells(1).Value = txtdocnumber.Text
DataGridViewPassengers.Rows(indx).Cells(2).Value = txtdatebook.Text
For x As Integer = 1 To (indx + 1)
lblnumpass.Text = x.ToString + " Passenger(s) added for this Flight"
Next
'when deleting a row
Private Sub btnremovepass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnremovepass.Click
DataGridViewPassengers.Rows.Remove(DataGridViewPassengers.CurrentRow)
For x As Integer = 1 To (DataGridViewPassengers.Rows.Count() - 1)
DataGridViewPassengers.Rows(x).Cells(0).Value = x.ToString
Next
lblnumpass.Text = (DataGridViewPassengers.Rows.Count() - 1).ToString + " Passenger(s) added for this Flight."
My problem is with the for loop when deleting a row. I'm missing something and can't solve it.
Wish someone would help me, would really appreciate it. Thanks so much in advance!