• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Having a problem with datagrid in VB.Net

I have button name rept_rec on click , it will repeat new row below selected row in a datagrid. And calls public sub calamount() which calculate the amount and display at the bottom row in 5th col and also calculate the total rows and display it at the bottom row in 4th col

But once I repeat the row first time everything is fine. But second time when I want to repeat any row below the repeated row it will increments the Datagrid1.CurrentRowIndex automatically by 1.

But it will not happen if I repeat any row above the repeated row.

Please suggest.

Below is code: -

  Private Sub Rept_Rec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rept_Rec.Click
        flagcount = False

        'MsgBox(Me.Datagrid1.CurrentRowIndex & "At Begin")

        If Datagrid1.IsSelected(Datagrid1.CurrentRowIndex) = True Then
            Dim col1 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("1").ToString
            Dim col2 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("2").ToString
            Dim col3 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("3").ToString
            Dim col4 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("4").ToString
            Dim col5 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("5").ToString
            Dim col6 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("6").ToString
            Dim col7 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("7").ToString
            Dim col8 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("8").ToString
            Dim col9 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("9").ToString
            Dim col10 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("10").ToString
            Dim col11 As String = datatable1.Rows(Datagrid1.CurrentRowIndex).Item("11").ToString

            Dim dtrow = datatable1.NewRow

            dtrow.Item("1") = col1
            dtrow.Item("2") = col2
            dtrow.Item("3") = col3
            dtrow.Item("4") = col4
            dtrow.Item("5") = col5
            dtrow.Item("6") = col6
            dtrow.Item("7") = col7
            dtrow.Item("8") = col8
            dtrow.Item("9") = col9
            dtrow.Item("10") = col10
            dtrow.Item("11") = col11
            datatable1.Rows.InsertAt(dtrow, Datagrid1.CurrentRowIndex + 1)
            ' MsgBox(Me.Datagrid1.CurrentRowIndex & "Before Calculate")
            Dim curval As Integer = Datagrid1.CurrentRowIndex
            '  MsgBox(Me.Datagrid1.CurrentRowIndex & "After Cal")
            flagcount = True
            MsgBox("Please Select The Row First")
            Exit Sub
        End If
    End Sub

    Public Sub calamount()

        Dim i As Integer
        Dim totalval As Double
        MsgBox(Me.Datagrid1.CurrentRowIndex & "-While cal 1")
        For i = 1 To datatable1.Rows.Count - 1
            Dim cyrval As String = datatable1.Rows(i).Item("11").ToString
            totalval = totalval + Val(datatable1.Rows(i).Item("11"))
        MsgBox(Me.Datagrid1.CurrentRowIndex & "-While cal 2")

        datatable1.Rows(datatable1.Rows.Count - 1).Item("5") = totalval
        datatable1.Rows(datatable1.Rows.Count - 1).Item("4") = datatable1.Rows.Count

        MsgBox(Me.Datagrid1.CurrentRowIndex & "-While cal 4")

    End Sub
  • 2
1 Solution
Bob LearnedCommented:
Why don't you keep track of the CurrentRowIndex, and then reset it after adding the duplicate row.

rvsglobalAuthor Commented:
can please suggest the code to reset .
Bob LearnedCommented:
This line in calamount is useless:


It would better be served here:

    Datagrid1.Select(curval)    ' <----- Reset old row index.
    '  MsgBox(Me.Datagrid1.CurrentRowIndex & "After Cal")
    flagcount = True


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now