views:

18

answers:

1

Hello.

I am trying to add a new row into a gridview but for some reason i'm having a problem in the for loop.

Directly goes to dtCurrentTable.Rows.Add(drCurrentRow) and of course, have an error "'row' argument cannot be null.Parameter name: row", because the dtcurrentTable.NewRow was not executed.

Why is this happening?

 Private Sub AddNewRowToGrid()
    Dim rowIndex As Integer = 0
    If Not IsNothing(ViewState("CurrentTable")) Then

        Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
        Dim drCurrentRow As DataRow = Nothing

        If dtCurrentTable.Rows.Count > 0 Then
            For i as Integer = 1 To i <= dtCurrentTable.Rows.Count
                ' Extraem-se os valores das Textbox
                Dim box1 As TextBox = Dados.Rows(rowIndex).Cells(0).FindControl("Artigo")
                Dim box2 As TextBox = Dados.Rows(rowIndex).Cells(1).FindControl("Descricao")
                Dim box3 As TextBox = Dados.Rows(rowIndex).Cells(2).FindControl("IVA")
                Dim box4 As TextBox = Dados.Rows(rowIndex).Cells(3).FindControl("PU")
                Dim box5 As TextBox = Dados.Rows(rowIndex).Cells(4).FindControl("Desconto")
                Dim box6 As TextBox = Dados.Rows(rowIndex).Cells(5).FindControl("UN")
                Dim box7 As TextBox = Dados.Rows(rowIndex).Cells(6).FindControl("Quantidade")
                Dim box8 As TextBox = Dados.Rows(rowIndex).Cells(7).FindControl("TotalLiquido")

                drCurrentRow = dtCurrentTable.NewRow

                dtCurrentTable.Rows(i - 1)("Artigo") = box1.Text
                dtCurrentTable.Rows(i - 1)("Descricao") = box2.Text
                dtCurrentTable.Rows(i - 1)("IVA") = box3.Text
                dtCurrentTable.Rows(i - 1)("PU") = box4.Text
                dtCurrentTable.Rows(i - 1)("Desconto") = box5.Text
                dtCurrentTable.Rows(i - 1)("UN") = box6.Text
                dtCurrentTable.Rows(i - 1)("Quantidade") = box7.Text
                dtCurrentTable.Rows(i - 1)("TotalLiquido") = box8.Text

                rowIndex += 1

            Next i
            dtCurrentTable.Rows.Add(drCurrentRow)
            ViewState("CurrentTable") = dtCurrentTable

            Dados.DataSource = dtCurrentTable
            Dados.DataBind()

        End If

    Else
        Response.Write("ViewState null")
    End If
    SetPreviousData()
End Sub
A: 

Your For loop is defined wrong, thats why you're getting an error:

For i as Integer = 1 To i <= dtCurrentTable.Rows.Count
evaluates to
For i as Integer = 1 To True
(because i is always <= Rows.Count) which VB translates as
For i as Integer = 1 To -1
which means your loop never runs.

It should be

For i as Integer = 1 To dtCurrentTable.Rows.Count

Also, there's something odd about the way you use drCurrentRow = dtCurrentTable.NewRow. Why is that inside the loop when you don't do anything with it in the loop? It gets executed multiple times and then dtCurrentTable.Rows.Add(drCurrentRow) only gets called once.

Its hard for me to correct because I can't figure out what you're trying to do, but that bit looks dodgy to me.

codeulike
It was that. :)Modified the for loop and now i have all working correctly.Thank you codeulike.
Filipe Costa