VB.NET: Error adding multiple rows in a DataGridView (not bound to a DataTable)
-
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)
-
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)
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 :)
-
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)
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.
-
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 :)
Hi Liqz, Thanks! That did the trick. I'll keep in mind that next time, I'll include the error message. Cheers.
-
Hi Liqz, Thanks! That did the trick. I'll keep in mind that next time, I'll include the error message. Cheers.
You're welcome :)