Having a problem with datagrid in VB.Net

Posted on 2005-04-26
Last Modified: 2010-08-05

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
Question by:rvsglobal
    LVL 96

    Expert Comment

    by:Bob Learned
    Why don't you keep track of the CurrentRowIndex, and then reset it after adding the duplicate row.


    Author Comment

    can please suggest the code to reset .
    LVL 96

    Accepted Solution

    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

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Suggested Solutions

    Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now