customdatagridcolumn.vb
来自「Samples are organized by chapter, and th」· VB 代码 · 共 163 行
VB
163 行
Public Class CustomDataGridColumn
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents grid As System.Windows.Forms.DataGrid
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.grid = New System.Windows.Forms.DataGrid()
CType(Me.grid, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'grid
'
Me.grid.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right)
Me.grid.DataMember = ""
Me.grid.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.grid.Location = New System.Drawing.Point(8, 8)
Me.grid.Name = "grid"
Me.grid.Size = New System.Drawing.Size(272, 232)
Me.grid.TabIndex = 0
'
'DataGridExample
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
Me.ClientSize = New System.Drawing.Size(288, 254)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.grid})
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "DataGridExample"
Me.Text = "DataGrid"
CType(Me.grid, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub DataGridExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim dsStore As New DataSet()
dsStore.ReadXmlSchema(Application.StartupPath & "\store.xsd")
dsStore.ReadXml(Application.StartupPath & "\store.xml")
' Create the column collection.
Dim Columns As New DataGridTableStyle()
Columns.MappingName = "Products"
' Create and configure the columns you want to display.
Dim PriceCol As New DataGridPriceIconColumn(100)
PriceCol.HeaderText = "Price"
PriceCol.MappingName = "UnitCost"
' Add the columns to the collection.
Columns.GridColumnStyles.Add(PriceCol)
' Configure the DataGrid to use these column settings.
grid.TableStyles.Add(Columns)
grid.ReadOnly = True
' Bind the grid.
grid.DataSource = dsStore.Tables("Products")
End Sub
End Class
Public Class DataGridPriceIconColumn
Inherits DataGridColumnStyle
Public NicePrice As Decimal
Public Sub New(ByVal nicePrice As Decimal)
Me.NicePrice = nicePrice
End Sub
Protected Overrides Sub Abort(ByVal rowNum As Integer)
' Do nothing.
End Sub
Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean
Return True
End Function
Protected Overloads Overrides 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)
' Do nothing.
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean)
' Do nothing.
End Sub
Protected Overloads Overrides 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)
' Do nothing.
End Sub
Protected Overrides Function GetMinimumHeight() As Integer
Return 20
End Function
Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer
Return 20
End Function
Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size
Return New Size(100, 20)
End Function
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 Integer, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
' Clear the cell.
g.FillRegion(backBrush, New Region(bounds))
Dim Price As Decimal = CType(Me.GetColumnValueAtRow(source, rowNum), Integer)
Dim PriceIcon As Icon
If Price < NicePrice Then
PriceIcon = New Icon(Application.StartupPath & "\happy2.ico")
' Draw the optional "nice price" icon.
g.DrawIcon(PriceIcon, New Rectangle(bounds.X, bounds.Y, 16, 16))
End If
' Draw the text.
g.DrawString(Price.ToString("C"), New Font("Tahoma", 8.25), Brushes.Black, bounds.X + 20, bounds.Y + 2)
End Sub
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 Integer, ByVal alignToRight As Boolean)
Me.Paint(g, bounds, source, rowNum, Brushes.White, Brushes.Black, alignToRight)
End Sub
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 Integer)
Me.Paint(g, bounds, source, rowNum, Brushes.White, Brushes.Black, False)
End Sub
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?