I am using a DataGridView with a button column. I am also drawing an image on the button cells during the initialization of the DataGridView. I am setting the columns and number of rows at run time. The issue I am seeing is the buttons appear different when I add rows after the initial default number rows. For example, during the DataGridView setup I set the row count to 2. The button on this row looks good. However, if I add more rows later, all of the buttons in the button column look different and it is only on the rows that were added after the initial default rows created during initialization. I can also clear all rows and still the rows after the initial set up still look different. If I set the initial row count to 5 during run time initialization, clear all rows, then set the number of rows to 8, the 3 rows after the first 5 will look different. I have included images of what I am seeing.
Setup the Grid
Draw the button image:
Later I am setting the row count
![Name: 2013-07-29_0951.png
Views: 21
Size: 1.2 KB]()
Setup the Grid
Code:
With ProductDataGrid
.EditMode = DataGridViewEditMode.EditProgrammatically
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None
.ColumnCount = _numberOfGridColumns
.ColumnHeadersVisible = True
.RowCount = 2 'Initial row count including column headers
.RowHeadersVisible = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
'.ReadOnly = True
.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ColumnHeadersDefaultCellStyle.Font = New Font(Control.DefaultFont, FontStyle.Bold)
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
.RowsDefaultCellStyle.SelectionBackColor = OrderEntryTabPage.Colors.RowSelectionBack
.RowsDefaultCellStyle.SelectionForeColor = OrderEntryTabPage.Colors.RowSelectionFore
.Columns.Insert(GridColumns.column1ProdButn, _productButtonColumn)
End With
Code:
Private Sub ProductDataGrid_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles ProductDataGrid.CellPainting
If (e.ColumnIndex = 1 And e.RowIndex >= 0) Then
SetImageToDataGridViewButtonColumn(Images16x16.Images("ViewEnabled"), e)
End If
End Sub
Public Shared Sub SetImageToDataGridViewButtonColumn(ByRef img As Bitmap, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs)
e.Paint(e.CellBounds, DataGridViewPaintParts.All & (DataGridViewPaintParts.ContentBackground) & (DataGridViewPaintParts.ContentForeground))
Dim destRect As Rectangle = New Rectangle(e.CellBounds.X + (e.CellBounds.Width - img.Width) / 2, e.CellBounds.Y + (e.CellBounds.Height - img.Height) / 2, img.Width, img.Height)
Dim srcRect As Rectangle = New Rectangle(0, 0, img.Width, img.Height)
e.Graphics.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel)
e.Handled = True
End Sub
Code:
ProductDataGrid.RowCount = productRowCount + 1