Results 1 to 10 of 10

Thread: Add new Row to datagrid from textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Location
    Lima, Peru
    Posts
    28

    Add new Row to datagrid from textbox

    Hi, I'm trying to add new rows to a datagrid from values of textboxes, it works fine but when the rows of the grid are bigger than the visible size of it, it gives an error of "incorrect number of line", i think it has to do something with the bookmark but i'm not very sure, can someone help plz?

    VB Code:
    1. ' Set new recordset to add the new row
    2. Set rsArray = New ADODB.Recordset
    3.             With rsArray
    4.                
    5.                .Fields.Append "ID_MONTO_ADICIONAL", adInteger
    6.                .Fields.Append "CONCEPTO_MONTO", adVarChar, 50
    7.                .Fields.Append "MONTO_ADICIONAL", adDouble
    8.                .CursorType = adOpenStatic
    9.                .LockType = adLockOptimistic
    10.                .Open
    11.  
    12.                '--load all the records that were before
    13.                Dim i As Integer
    14.                For i = 0 To can_filas - 1 ' can_filas has already the number of rows before adding a new one
    15.                    Me.dgMontosAdic.RowBookmark (i) [B]'--Get a row of the grid, gives error here when i insert record 11, and the visible size of the grid is 10[/B]
    16.  
    17. ' Add all rows that were before
    18.                    .AddNew
    19.                    ![ID_MONTO_ADICIONAL] = i + 1
    20.                    ![CONCEPTO_MONTO] = Me.dgMontosAdic.Columns(1).Text
    21.                    ![MONTO_ADICIONAL] = Me.dgMontosAdic.Columns(2).Text
    22.                Next
    23.                
    24.                '--Add the new record
    25.                .AddNew
    26.                ![ID_MONTO_ADICIONAL] = i + 1
    27.                ![CONCEPTO_MONTO] = txtConcepto.Text
    28.                ![MONTO_ADICIONAL] = txtMonto.Text
    29.                
    30.                .Update
    31. ' update the number of rows
    32.                can_filas = can_filas + 1
    33.             End With
    34.            
    35. ' update the grid with the new recordset and the new row.
    36.             Set Me.dgMontosAdic.DataSource = rsArray

    i hope u understand me, and thanks in advance for help

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Add new Row to datagrid from textbox

    You can only call RowBookmark for visible rows. Try GetBookMark instead.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Location
    Lima, Peru
    Posts
    28

    Re: Add new Row to datagrid from textbox

    i try using getbookmark like this:

    Me.dgMontosAdic.GetBookmark(i)

    but i get the same error but in the second row and not after the number of rows are bigger than the visible size like before.

    any ideas?

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Add new Row to datagrid from textbox

    The argument to GetBookmark is the row relative to the current row, not a specific row number.

    Also, use the Column's CellValue property instead of the Text property to access the data. CellValue requires the Bookmark of the Row from where you want to retrieve data.

    VB Code:
    1. Me.dgMontosAdic.Row = 0 'start the first row in the grid
    2. For i = 0 To can_filas - 1
    3.    'Me.dgMontosAdic.RowBookmark (i)
    4.    .AddNew
    5.    ![ID_MONTO_ADICIONAL] = i + 1
    6.    ![CONCEPTO_MONTO] = Me.dgMontosAdic.Columns(1).CellValue(Me.dgMontosAdic.GetBookmark(i))
    7.    ![MONTO_ADICIONAL] = Me.dgMontosAdic.Columns(2).CellValue(Me.dgMontosAdic.GetBookmark(i))
    8. Next

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Location
    Lima, Peru
    Posts
    28

    Re: Add new Row to datagrid from textbox

    i try that and i got the same error, the first row inserts just fine, so does the second row, but the third row gives me that error of "incorrect number of line", why it doesn't do it with the first two? and the error was once again in the bookmark when i use the CellValue Property

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Add new Row to datagrid from textbox

    Does the Can_Filas variable contain the number of Rows in the grid? Post your updated code.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Location
    Lima, Peru
    Posts
    28

    Re: Add new Row to datagrid from textbox

    yes, can_filas has the initial number of rows of the datagrid, in the load event i have this line:

    can_filas = rSetMontosAdic.RecordCount

    because i make an initial search to my database for previews values, and when i add a new row i increment this value

    VB Code:
    1. ' new recordset for the old values and the new value to add
    2. Set rsArray = New ADODB.Recordset
    3. With rsArray
    4.  
    5.        .Fields.Append "ID_MONTO_ADICIONAL", adInteger
    6.        .Fields.Append "CONCEPTO_MONTO", adVarChar, 50
    7.        .Fields.Append "MONTO_ADICIONAL", adDouble
    8.        .CursorType = adOpenStatic
    9.        .LockType = adLockOptimistic
    10.        .Open
    11.                
    12.        '-- we add all the previews values
    13.        Dim i As Integer
    14.        For i = 0 To can_filas - 1
    15.             .AddNew
    16.             ![ID_MONTO_ADICIONAL] = i + 1
    17.             ![CONCEPTO_MONTO] = Me.dgMontosAdic.Columns(1).CellValue(Me.dgMontosAdic.GetBookmark(i)) ' Now i get the error here because of the bookmark
    18.             ![MONTO_ADICIONAL] = Me.dgMontosAdic.Columns(2).CellValue(Me.dgMontosAdic.GetBookmark(i)) ' and here too
    19.        Next
    20.                
    21.        '--add the new value
    22.        .AddNew
    23.        ![ID_MONTO_ADICIONAL] = i + 1
    24.        ![CONCEPTO_MONTO] = strConcepto
    25.        ![MONTO_ADICIONAL] = strMonto
    26.                
    27.        .Update
    28.        can_filas = can_filas + 1 ' here i increment the number of rows that was already set in the load event
    29. End With
    30.  
    31. ' update the datagrid with the modified recordset
    32. Set Me.dgMontosAdic.DataSource = rsArray

  8. #8
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Add new Row to datagrid from textbox

    You did not include this line of code.

    Me.dgMontosAdic.Row = 0 'start the first row in the grid

    GetBookMark is relative to the current row so ensure the first row in the grid is the current row before starting the For Loop.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Location
    Lima, Peru
    Posts
    28

    Re: Add new Row to datagrid from textbox

    i got the same error but this time it was like the first time, i got the error after the number of rows is bigger than the visible size of the grid any ideas?

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2006
    Location
    Lima, Peru
    Posts
    28

    Re: Add new Row to datagrid from textbox

    now i've tried this:

    VB Code:
    1. ' New recordset for old values and new one
    2. Set rsArray = New ADODB.Recordset
    3. With rsArray
    4.     .Fields.Append "ID_MONTO_ADICIONAL", adInteger
    5.     .Fields.Append "CONCEPTO_MONTO", adVarChar, 50
    6.     .Fields.Append "MONTO_ADICIONAL", adDouble
    7.     .CursorType = adOpenStatic
    8.     .LockType = adLockOptimistic
    9.     .Open
    10.            
    11.     ' i give the recordset the old data that exist in database
    12.     dgMontosAdic.AllowAddNew = True
    13.     Set .DataSource = Me.dgMontosAdic.DataSource
    14.        
    15.     ' i'm not using this now
    16.     ' Me.dgMontosAdic.Row = 0
    17.     '--Bucle para recorrer el contenido de la grilla
    18.     Dim i As Integer
    19.     For i = 0 To can_filas - 1
    20.        
    21. '       .AddNew
    22. '       ![ID_MONTO_ADICIONAL] = i + 1
    23. '       ![CONCEPTO_MONTO] = Me.dgMontosAdic.Columns(1).CellValue(Me.dgMontosAdic.GetBookmark(i))
    24. '       ![MONTO_ADICIONAL] = Me.dgMontosAdic.Columns(2).CellValue(Me.dgMontosAdic.GetBookmark(i))
    25.     Next
    26.    
    27.     '--Add the new record
    28.    
    29.     .AddNew
    30.     ![ID_MONTO_ADICIONAL] = i + 1
    31.     ![CONCEPTO_MONTO] = strConcepto
    32.     ![MONTO_ADICIONAL] = strMonto
    33.    
    34.     .Update
    35.     can_filas = can_filas + 1
    36. End With
    37.  
    38. ' update the grid with the old rows and the new one.
    39. Set Me.dgMontosAdic.DataSource = rsArray

    it works fine, but the problem is now that if the grid has old values, when i try to add a new one, gives me an error "The actual Recordset does not allow actualizations. This can be a limitation of the provider or the type of lock selected", something like that, i'm not very good with translations

    thanks for helping

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width