在DataGrid中添加图片列(VB.NET)

重写DataGridTextBoxColumn类


Imports  System
Imports  System.Windows.Forms
Imports  System.Drawing
Imports  System.Data
Imports  System.Collections


' / <summary>
'
/ Summary description for DataGridImageCell.
'
/ </summary>

Public   Class DataGridImageCell
   
Inherits DataGridTextBoxColumn
   
Private theImages1 As ArrayList
   
   
Public Sub New()
   
End Sub
 'New
   
   
   
Public Property theImages() As ArrayList
      
Get
         
Return theImages1
      
End Get
      
Set
         theImages1 
= value
      
End Set
   
End Property

    
    
Protected Overloads Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerByVal 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 NothingThen
            
Dim i As Integer = Fix(o)
            g.FillRectangle(backBrush, bounds)

            
Dim bmp As Bitmap = CType(theImages(i), Bitmap)

            
'GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.NoResize;
            'GridImageCellDrawOption cellDrawOption = GridImageCellDrawOption.FitProportionally;
            Dim cellDrawOption As GridImageCellDrawOption = GridImageCellDrawOption.FitToCell


            
Dim gu As System.Drawing.GraphicsUnit = System.Drawing.GraphicsUnit.Point

            
Dim srcRect As RectangleF = bmp.GetBounds(gu)
            
Dim destRect As Rectangle = Rectangle.Empty

            
Dim saveRegion As [Region] = g.Clip

            
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 (TrueThen
                        
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

                
Case Else
            
End Select

            
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)

                g.DrawImage(bmp, destRectF, srcRectF, gu)
            
End If
            g.Clip 
= saveRegion
        
End If
    
End Sub
 'Paint


    
Protected Overloads Overrides Sub Edit(ByVal [source] As System.Windows.Forms.CurrencyManager, ByVal rowNum As IntegerByVal bounds As System.Drawing.Rectangle, ByVal [readOnlyAs BooleanByVal instantText As StringByVal cellIsVisible As Boolean)
        
'overriden to avoid editing
    End Sub
 'Edit



    
Public Enum GridImageCellDrawOption
        FitToCell 
= 0
        NoResize 
= 1
        FitProportionally 
= 2
    
End Enum
 'GridImageCellDrawOption

End Class
  ' DataGridImageCell

测试代码

Private   WithEvents  dataGrid1  As  System.Windows.Forms.DataGrid

  
Private  nRows  As   Integer   =   5
    
Private  bitMaps  As  ArrayList

 
Private   Sub Form1_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles MyBase.Load

        
'create a datatable
        Dim dt As New DataTable("MyTable")

        dt.Columns.Add(
New DataColumn("Col0"))
        dt.Columns.Add(
New DataColumn("Images"GetType(Integer)))

        
Dim r As New Random()
        
Dim i As Integer

        
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

        
'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))

        
Dim tableStyle As New DataGridTableStyle()
        tableStyle.MappingName 
= "MyTable"

        
Dim tbc As New DataGridTextBoxColumn()
        tbc.MappingName 
= "Col0"
        tbc.HeaderText 
= "Column 1"
        tbc.Width 
= 50
        tableStyle.GridColumnStyles.Add(tbc)

        
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)

        
Me.dataGrid1.TableStyles.Add(tableStyle)
        
Me.dataGrid1.DataSource = dt
        dt.DefaultView.AllowNew 
= False

        
Dim rect As Rectangle = Me.dataGrid1.GetCellBounds(00)
        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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值