Where can I find the CellPainting Event of the DataGridView?

Answered
0
0

Hello Developers!

I want to resize images in a DataGridView to fit them properly in the cells but I can’t find the CellPainting Event we use in WinForms.

Please, I need some help on this issue. Below is the code I use in WinForms.

 

  1. Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
  2. If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
  3. If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Byte()) Then
  4. CType(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex),
  5. DataGridViewImageCell).ImageLayout = DataGridViewImageCellLayout.Zoom
  6. End If
  7. End Sub

Thank you

  • You must to post comments
Best Answer
0
0

Okay. Thank for your support. Since I don’t create DataGridView Columns at Design time, I couldn’t see the UserPaint Property quickly. But I’ve seen it and set it at the FormLoad Event like this;

DataGridView1.Columns(“CustomerImage”).UserPaint = True

And I’ve set the CellPaint Event as below;

Private Sub DataGridView1_CellPaint(sender As Object, e As DataGridViewCellPaintEventArgs) Handles DataGridView1.CellPaint

If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return

If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Byte()) Then
CType(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex),
DataGridViewImageCell).ImageLayout = DataGridViewImageCellLayout.Zoom
End If

End Sub

They seem to work as expected.

Thank you

  • You must to post comments
0
0

Hi Ngabo,

The event you’re looking for is CellPaint, make sure to visit our documentation for additional help with the DataGridView!

You have to set the UserPaint property to true for it to work.

HTH,

Alaa

  • You must to post comments
Showing 2 results
Your Answer

Please first to submit.