重写DataGridTextBoxColumn类
Imports
System
Imports
System.Windows.Forms
Imports
System.Drawing
Imports
System.Data
Imports
System.Collections
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
'
/ <summary>
'
/ Summary description for DataGridImageCell.
'
/ </summary>
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public
Class DataGridImageCell
Class DataGridImageCell
Inherits DataGridTextBoxColumn
Private theImages1 As ArrayList
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Sub New()Sub New()
End Sub 'New
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Property theImages()Property theImages() As ArrayList
Get
Return theImages1
End Get
Set
theImages1 = value
End Set
End Property
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Sub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
Dim o As Object = Me.GetColumnValueAtRow([source], rowNum)
If Not (o Is Nothing) Then
Dim i As Integer = Fix(o)
g.FillRectangle(backBrush, bounds)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim bmp As Bitmap = CType(theImages(i), Bitmap)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.NoResize;
'GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitProportionally;
Dim cellDrawOption As GridImageCellDrawOption = GridImageCellDrawOption.FitToCell
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim gu As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Point
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim srcRect As RectangleF = bmp.GetBounds(gu)
Dim destRect As Rectangle = Rectangle.Empty
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim saveRegion As [Region] = g.Clip
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Select Case cellDrawOption
Case GridImageCellDrawOption.FitToCell
destRect = bounds
Case GridImageCellDrawOption.NoResize
destRect = New Rectangle(bounds.X, bounds.Y, Fix(srcRect.Width), Fix(srcRect.Height))
g.Clip = New [Region](bounds)
Case GridImageCellDrawOption.FitProportionally
If (True) Then
Dim srcRatio As Single = srcRect.Width / srcRect.Height
Dim tarRatio As Single = System.Convert.ToSingle(bounds.Width) / bounds.Height
destRect = bounds
If tarRatio < srcRatio Then
destRect.Height = Fix(destRect.Width * srcRatio)
Else
destRect.Width = Fix(destRect.Height * srcRatio)
End If
End If
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Case Else
End Select
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
If Not destRect.IsEmpty Then
'g.DrawImage(bmp, destRect, srcRect, gu)
Dim destRectF As New RectangleF(destRect.X, destRect.Y, destRect.Width, destRect.Height)
Dim srcRectF As New RectangleF(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
g.DrawImage(bmp, destRectF, srcRectF, gu)
End If
g.Clip = saveRegion
End If
End Sub 'Paint
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overloads Overrides Sub Edit()Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
'overriden to avoid editing
End Sub 'Edit
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Enum GridImageCellDrawOptionEnum GridImageCellDrawOption
FitToCell = 0
NoResize = 1
FitProportionally = 2
End Enum 'GridImageCellDrawOption
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
End Class
'
DataGridImageCell
测试代码
Private
WithEvents
dataGrid1
As
System.Windows.Forms.DataGrid
![None.gif](/Images/OutliningIndicators/None.gif)
Private
nRows
As
Integer
=
5
Private
bitMaps
As
ArrayList
![None.gif](/Images/OutliningIndicators/None.gif)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
Private
Sub Form1_Load()
Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'create a datatable
Dim dt As New DataTable("MyTable")
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
dt.Columns.Add(New DataColumn("Col0"))
dt.Columns.Add(New DataColumn("Images", GetType(Integer)))
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim r As New Random()
Dim i As Integer
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
For i = 0 To nRows - 1
Dim dr As DataRow = dt.NewRow()
dr(0) = String.Format("row{0}", i)
dr(1) = r.Next(4)
dt.Rows.Add(dr)
Next
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
'store bitmaps in an arraylist
bitMaps = New ArrayList()
Dim strm As System.IO.Stream = [GetType]().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Blue hills.jpg")
bitMaps.Add(New Bitmap(strm))
strm = [GetType]().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Sunset.jpg")
bitMaps.Add(New Bitmap(strm))
strm = [GetType]().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Water lilies.jpg")
bitMaps.Add(New Bitmap(strm))
strm = [GetType]().Assembly.GetManifestResourceStream("ImageCellInDataGrid.Winter.jpg")
bitMaps.Add(New Bitmap(strm))
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim tableStyle As New DataGridTableStyle()
tableStyle.MappingName = "MyTable"
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim tbc As New DataGridTextBoxColumn()
tbc.MappingName = "Col0"
tbc.HeaderText = "Column 1"
tbc.Width = 50
tableStyle.GridColumnStyles.Add(tbc)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim width As Integer = Me.dataGrid1.ClientSize.Width - tbc.Width - Me.dataGrid1.RowHeaderWidth - 4
Dim tbc1 As New DataGridImageCell()
tbc1.MappingName = "Images"
tbc1.HeaderText = "Images"
tbc1.theImages = bitMaps
tbc1.Width = width
tableStyle.GridColumnStyles.Add(tbc1)
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Me.dataGrid1.TableStyles.Add(tableStyle)
Me.dataGrid1.DataSource = dt
dt.DefaultView.AllowNew = False
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
Dim rect As Rectangle = Me.dataGrid1.GetCellBounds(0, 0)
topPos = rect.Top
Dim height As Integer = (Me.dataGrid1.ClientSize.Height - topPos - nRows) / nRows
tableStyle.PreferredRowHeight = height
Me.dataGrid1.DataSource = Nothing
Me.dataGrid1.DataSource = dt
End Sub
'
Form1_Load
转载于:https://www.cnblogs.com/timsoft/articles/415000.html