Visual Basic.NET - Texto e imagen en celda de DataGridView (codigo)

 
Vista:

Texto e imagen en celda de DataGridView (codigo)

Publicado por Preguntador VB.NET (22 intervenciones) el 24/05/2007 10:56:49
Hace dias, puse un mensaje para intentar resolver algo sobre un codigo interesante que encontre y

Harold V. me ayudo (gracias de nuevo). No se de quien es pero os lo pondre porque creo que merece

la pena compartirlo.

A continuacion os describo como crear columnas en un control DataGridView donde se pueden incluir

imagen y texto en una misma celda (el control no dispone de esa propiedad). El codigo esta

escrito en VB 2005, no se si funciona en 2003.

En vuestro proyecto, añadis dos nuevas clases, una llamada TextAndImageColumn.vb y otra llamada

TextAndImageCell.vb (en los siguientes dos mensajes os pongo el codigo de esas dos clases).

Luego añadis un control DataGridView al formulario e incluis este codigo para crear dos columnas

(yo lo he puesto en el evento Load del formulario):

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

MyBase.Load
'Se crea la primera columna
Dim Col0 As New TextAndImageColumn
Col0.Image = Image.FromFile("C:\Imagen.bmp")
Me.DataGridView1.Columns.Insert(0, Col0)

'Se crea la segunda columna
Dim Col1 As New TextAndImageColumn
Col1.Image = Image.FromFile("C:\Imagen.bmp")
Me.DataGridView1.Columns.Insert(1, Col1)
End Sub

Para cambiar el texto y la imagen de una celda, debeis utilizar este codigo (yo modifico la celda

que esta en la primera fila y segunda columna):

DirectCast(DataGridView1.Item(1, 0), TextAndImageCell).Value = "Nuevo texto"
DirectCast(DataGridView1.Item(1, 0), TextAndImageCell).Image =

Image.FromFile("C:\NuevaImagen.bmp")


NOTA: Para cambiar unicamente el texto de una celda tambien podeis utilizar el codigo que se

emplea para cambiarlo en una celda de una columna de texto normal (no se puede cambiar la imagen

mediante este metodo):

Me.DataGridView1.Rows(0).Cells(1).Value = "Nuevo texto"
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

TextAndImageColumn.vb

Publicado por Preguntador VB.NET (22 intervenciones) el 24/05/2007 10:57:54
Public Class TextAndImageColumn

Inherits DataGridViewTextBoxColumn
Private imageValue As Image
Private imageSize As Size

Public Sub New()
Me.CellTemplate = New TextAndImageCell
End Sub

Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageColumn = CType(TryCast(MyBase.Clone, TextAndImageColumn), TextAndImageColumn)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function

Public Property Image() As Image
Get
Return Me.imageValue
End Get
Set(ByVal value As Image)
If Not Me.Image Is value Then
Me.imageValue = value
Me.imageSize = value.Size
If Not (Me.InheritedStyle Is Nothing) Then
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.DefaultCellStyle.Padding = New Padding(imageSize.Width, inheritedPadding.Top, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End If
End Set
End Property

Private ReadOnly Property TextAndImageCellTemplate() As TextAndImageCell
Get
Return CType(TryCast(Me.CellTemplate, TextAndImageCell), TextAndImageCell)
End Get
End Property

Friend ReadOnly Property ImageSize_() As Size
Get
Return imageSize
End Get
End Property
End Class
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

TextAndImageCell.vb

Publicado por Preguntador VB.NET (22 intervenciones) el 24/05/2007 10:58:49
Public Class TextAndImageCell
Inherits DataGridViewTextBoxCell
Private imageValue As Image
Private imageSize As Size

Public Overloads Overrides Function Clone() As Object
Dim c As TextAndImageCell = CType(TryCast(MyBase.Clone, TextAndImageCell), TextAndImageCell)
c.imageValue = Me.imageValue
c.imageSize = Me.imageSize
Return c
End Function

Public Property Image() As Image
Get
If Me.OwningColumn Is Nothing OrElse Me.OwningTextAndImageColumn Is Nothing Then
Return imageValue
Else
If Not (Me.imageValue Is Nothing) Then
Return Me.imageValue
Else
Return Me.OwningTextAndImageColumn.Image
End If
End If
End Get
Set(ByVal value As Image)
If Not Me.Image Is value Then
Me.imageValue = value
Me.imageSize = value.Size
Dim inheritedPadding As Padding = Me.InheritedStyle.Padding
Me.Style.Padding = New Padding(imageSize.Width, inheritedPadding.Top + 5, inheritedPadding.Right, inheritedPadding.Bottom)
End If
End Set
End Property

Protected Overloads Overrides Sub Paint(ByVal graphics As Graphics, ByVal clipBounds As Rectangle, ByVal cellBounds As Rectangle, ByVal rowIndex As Integer, ByVal cellState As DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As DataGridViewCellStyle, ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle, ByVal paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts)
If Not (Me.Image Is Nothing) Then
Dim container As System.Drawing.Drawing2D.GraphicsContainer = graphics.BeginContainer
graphics.SetClip(cellBounds)
graphics.DrawImage(Me.Image, cellBounds.Location.X, cellBounds.Location.Y, Me.Image.Width, Me.Image.Height)
graphics.EndContainer(container)
End If
End Sub

Private ReadOnly Property OwningTextAndImageColumn() As TextAndImageColumn
Get
Return CType(TryCast(Me.OwningColumn, TextAndImageColumn), TextAndImageColumn)
End Get
End Property
End Class
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar