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!

' Form level variable 
Dim index As Integer = 1
    Dim dt As New DataTable
    Dim dc As New DataColumn("Numbers")
    Dim NoOfRows As Integer = 0

' Put it in form load event
            dc.DataType = System.Type.GetType("System.String")
            dt.Columns.Add(dc)
            DataGridView1.DataSource = dt
'' in Button add Click
             Try
            Dim dr As DataRow
            dr = dt.NewRow
            dr.Item(0) = index
            dt.Rows.Add(dr)
            DataGridView1.DataSource = dt
            index = index + 1
            NoOfRows = NoOfRows + 1
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
'' In buton remove click
            Try

            index = 1
            DataGridView1.DataSource = Nothing
            dt.Clear()
            For i As Integer = 0 To NoOfRows - 2
                Dim dr As DataRow
                dr = dt.NewRow
                dr.Item(0) = index
                dt.Rows.Add(dr)

                index = index + 1
            Next
            DataGridView1.DataSource = dt
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

try this out in seperate test project and u can try in ur main project later..

Hi Junior Poster in Training,

Thanks thanks so much! I got it..I just added added an index=1 and used it to assign to the dr.Item(0), instead of using the variable in my for loop:

My revised code for remove:

Dim indx As Integer = 1
            For x As Integer = 0 To (DataGridViewPassengers.Rows.Count() - 1)
                DataGridViewPassengers.Rows(x).Cells(0).Value = indx.ToString
                indx = indx + 1
            Next

Thanks so much! Appreciate it! :=)

ur welcum:) Please mark the thread as solved if it is solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.