Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB.NET: Error adding multiple rows in a DataGridView (not bound to a DataTable)

VB.NET: Error adding multiple rows in a DataGridView (not bound to a DataTable)

Scheduled Pinned Locked Moved Visual Basic
helpcsharpgraphicsalgorithms
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dimitri Backaert
    wrote on last edited by
    #1

    Hi, I'm having problems adding multiple rows to a DataGridView, on the Load-event of a Windows Application Form. Adding one record doesn't cause a problem, but when adding the second record (using the identical code), an Exception is thrown. Here's my code so far (VB.NET 2008):

    Private Sub BWMFPForm\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dgvRow As New DataGridViewRow
        Dim dgvCell As DataGridViewCell
        Dim dgvButton As DataGridViewImageColumn
        Dim column As DataGridViewColumn
    
        'Disable sorting for all Columns
        For Each column In DataGridViewBrochures.Columns
            column.SortMode = DataGridViewColumnSortMode.NotSortable
        Next
    
        'Add Record
        'Column 1: Machine
        dgvCell = New DataGridViewTextBoxCell()
        dgvCell.Value = "Aficio MP 4000 / Aficio MP 5000"
        dgvRow.Cells.Add(dgvCell)
        'Column 2: Speed
        dgvCell = New DataGridViewTextBoxCell()
        dgvCell.Value = "40 PPM / 50 PPM"
        dgvRow.Cells.Add(dgvCell)
        'Column 3: Language
        dgvCell = New DataGridViewTextBoxCell()
        dgvCell.Value = "Nederlands"
        dgvRow.Cells.Add(dgvCell)
        'Column 4: Image Print
        dgvButton = New DataGridViewImageColumn
        dgvButton.Image = Drawing.Image.FromFile("D:\\DEVPROJECTS\\MarketingTouchScreen\\TouchScreen\\images\\Print\_Icon.jpg")
        dgvButton.ReadOnly = True
        dgvButton.ImageLayout = DataGridViewImageCellLayout.Normal
        DataGridViewBrochures.Columns.Add(dgvButton)
        'Add the Row
        DataGridViewBrochures.Rows.Add(dgvRow)
    
        'Add Record
        'Column 1: Machine
        dgvCell = New DataGridViewTextBoxCell()
        dgvCell.Value = "Aficio MP 4000 / Aficio MP 5000"
        dgvRow.Cells.Add(dgvCell)
        'Column 2: Speed
        dgvCell = New DataGridViewTextBoxCell()
        dgvCell.Value = "40 PPM / 50 PPM"
        dgvRow.Cells.Add(dgvCell)
        'Column 3: Language
        dgvCell = New DataGridViewTextBoxCell()
        dgvCell.Value = "Nederlands"
        dgvRow.Cells.Add(dgvCell)
        'Column 4: Image Print
        dgvButton = New DataGridViewImageColumn
        dgvButton.Image = Drawing.Image.FromFile("D:\\DEVPROJECTS\\MarketingTouchScreen\\TouchScreen\\images\\Print\_Icon.jpg")
        dgvButton.ReadOnly = True
        dgvButton.ImageLayout = DataGridViewImageCellLayout.Normal
        DataGridViewBrochures.Columns.Add(dgvButton)
    
    J K 2 Replies Last reply
    0
    • D Dimitri Backaert

      Hi, I'm having problems adding multiple rows to a DataGridView, on the Load-event of a Windows Application Form. Adding one record doesn't cause a problem, but when adding the second record (using the identical code), an Exception is thrown. Here's my code so far (VB.NET 2008):

      Private Sub BWMFPForm\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
          Dim dgvRow As New DataGridViewRow
          Dim dgvCell As DataGridViewCell
          Dim dgvButton As DataGridViewImageColumn
          Dim column As DataGridViewColumn
      
          'Disable sorting for all Columns
          For Each column In DataGridViewBrochures.Columns
              column.SortMode = DataGridViewColumnSortMode.NotSortable
          Next
      
          'Add Record
          'Column 1: Machine
          dgvCell = New DataGridViewTextBoxCell()
          dgvCell.Value = "Aficio MP 4000 / Aficio MP 5000"
          dgvRow.Cells.Add(dgvCell)
          'Column 2: Speed
          dgvCell = New DataGridViewTextBoxCell()
          dgvCell.Value = "40 PPM / 50 PPM"
          dgvRow.Cells.Add(dgvCell)
          'Column 3: Language
          dgvCell = New DataGridViewTextBoxCell()
          dgvCell.Value = "Nederlands"
          dgvRow.Cells.Add(dgvCell)
          'Column 4: Image Print
          dgvButton = New DataGridViewImageColumn
          dgvButton.Image = Drawing.Image.FromFile("D:\\DEVPROJECTS\\MarketingTouchScreen\\TouchScreen\\images\\Print\_Icon.jpg")
          dgvButton.ReadOnly = True
          dgvButton.ImageLayout = DataGridViewImageCellLayout.Normal
          DataGridViewBrochures.Columns.Add(dgvButton)
          'Add the Row
          DataGridViewBrochures.Rows.Add(dgvRow)
      
          'Add Record
          'Column 1: Machine
          dgvCell = New DataGridViewTextBoxCell()
          dgvCell.Value = "Aficio MP 4000 / Aficio MP 5000"
          dgvRow.Cells.Add(dgvCell)
          'Column 2: Speed
          dgvCell = New DataGridViewTextBoxCell()
          dgvCell.Value = "40 PPM / 50 PPM"
          dgvRow.Cells.Add(dgvCell)
          'Column 3: Language
          dgvCell = New DataGridViewTextBoxCell()
          dgvCell.Value = "Nederlands"
          dgvRow.Cells.Add(dgvCell)
          'Column 4: Image Print
          dgvButton = New DataGridViewImageColumn
          dgvButton.Image = Drawing.Image.FromFile("D:\\DEVPROJECTS\\MarketingTouchScreen\\TouchScreen\\images\\Print\_Icon.jpg")
          dgvButton.ReadOnly = True
          dgvButton.ImageLayout = DataGridViewImageCellLayout.Normal
          DataGridViewBrochures.Columns.Add(dgvButton)
      
      J Offline
      J Offline
      Jay Royall
      wrote on last edited by
      #2

      When adding your second record you are still using the same DataGridViewRow as for the first record. You will need to use the new keyword on dgvRow before adding cells to it. Also, in future it is helpful if you include any error messages in your post :)

      D 1 Reply Last reply
      0
      • D Dimitri Backaert

        Hi, I'm having problems adding multiple rows to a DataGridView, on the Load-event of a Windows Application Form. Adding one record doesn't cause a problem, but when adding the second record (using the identical code), an Exception is thrown. Here's my code so far (VB.NET 2008):

        Private Sub BWMFPForm\_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim dgvRow As New DataGridViewRow
            Dim dgvCell As DataGridViewCell
            Dim dgvButton As DataGridViewImageColumn
            Dim column As DataGridViewColumn
        
            'Disable sorting for all Columns
            For Each column In DataGridViewBrochures.Columns
                column.SortMode = DataGridViewColumnSortMode.NotSortable
            Next
        
            'Add Record
            'Column 1: Machine
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "Aficio MP 4000 / Aficio MP 5000"
            dgvRow.Cells.Add(dgvCell)
            'Column 2: Speed
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "40 PPM / 50 PPM"
            dgvRow.Cells.Add(dgvCell)
            'Column 3: Language
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "Nederlands"
            dgvRow.Cells.Add(dgvCell)
            'Column 4: Image Print
            dgvButton = New DataGridViewImageColumn
            dgvButton.Image = Drawing.Image.FromFile("D:\\DEVPROJECTS\\MarketingTouchScreen\\TouchScreen\\images\\Print\_Icon.jpg")
            dgvButton.ReadOnly = True
            dgvButton.ImageLayout = DataGridViewImageCellLayout.Normal
            DataGridViewBrochures.Columns.Add(dgvButton)
            'Add the Row
            DataGridViewBrochures.Rows.Add(dgvRow)
        
            'Add Record
            'Column 1: Machine
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "Aficio MP 4000 / Aficio MP 5000"
            dgvRow.Cells.Add(dgvCell)
            'Column 2: Speed
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "40 PPM / 50 PPM"
            dgvRow.Cells.Add(dgvCell)
            'Column 3: Language
            dgvCell = New DataGridViewTextBoxCell()
            dgvCell.Value = "Nederlands"
            dgvRow.Cells.Add(dgvCell)
            'Column 4: Image Print
            dgvButton = New DataGridViewImageColumn
            dgvButton.Image = Drawing.Image.FromFile("D:\\DEVPROJECTS\\MarketingTouchScreen\\TouchScreen\\images\\Print\_Icon.jpg")
            dgvButton.ReadOnly = True
            dgvButton.ImageLayout = DataGridViewImageCellLayout.Normal
            DataGridViewBrochures.Columns.Add(dgvButton)
        
        K Offline
        K Offline
        Kschuler
        wrote on last edited by
        #3

        For future reference, adding the text of the exception is always helpful. However from you're code I'm guessing it's something like "...row already exists in grid." ...or something like that. You're adding the same row object to the grid twice. You need a new row object the second time around. Otherwise all you are doing is updating the data in the first row you added. Hope this helps.

        1 Reply Last reply
        0
        • J Jay Royall

          When adding your second record you are still using the same DataGridViewRow as for the first record. You will need to use the new keyword on dgvRow before adding cells to it. Also, in future it is helpful if you include any error messages in your post :)

          D Offline
          D Offline
          Dimitri Backaert
          wrote on last edited by
          #4

          Hi Liqz, Thanks! That did the trick. I'll keep in mind that next time, I'll include the error message. Cheers.

          J 1 Reply Last reply
          0
          • D Dimitri Backaert

            Hi Liqz, Thanks! That did the trick. I'll keep in mind that next time, I'll include the error message. Cheers.

            J Offline
            J Offline
            Jay Royall
            wrote on last edited by
            #5

            You're welcome :)

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups