Public Class mybuttondatagridtablestyle1
Inherits System.Windows.Forms.DataGridTextBoxColumn
Region " Windows 窗体设计器生成的代码 "
Public Sub New ()
MyBase .New()
' 该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
' 在 InitializeComponent() 调用之后添加任何初始化
End Sub
'UserControl 重写 dispose 以清理组件列表。
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
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
' 注意:以下过程是 Windows 窗体设计器所必需的
' 可以使用 Windows 窗体设计器修改此过程。
' 不要使用代码编辑器修改它。
1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()
2
3components = New System.ComponentModel.Container()
4
5End Sub
6
7# End Region
8
9Public Delegate Sub DataGridCellButtonClickEventHandler( ByVal sender As Object , ByVal e As DataGridCellButtonClickEventArgs)
10
11Public Event CellButtonClicked As DataGridCellButtonClickEventHandler
12
13Private m_Face As Bitmap
14
15Private m_FacePressed As Bitmap
16
17Private m_columnNum As Integer
18
19Private m_Row As Integer
20
21Public Sub New ( ByVal colNum As Integer )
22
23m_columnNum = colNum
24
25m_Row = -1
26
27Try
28
29Dim strm As System.IO.Stream = Me .GetType().Assembly.GetManifestResourceStream("btnface.bmp")
30
31m_Face = New Bitmap(strm)
32
33strm = Me .GetType().Assembly.GetManifestResourceStream("btnpressed.bmp")
34
35m_FacePressed = New Bitmap(strm)
36
37Catch
38
39End Try
40
41End Sub
42
43Protected 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 )
44
45End Sub
46
47Public Sub HandleMouseUp( ByVal sender As Object , ByVal e As MouseEventArgs)
48
49Dim dg As DataGrid = Me .DataGridTableStyle.DataGrid
50
51Dim hti As DataGrid.HitTestInfo = dg.HitTest( New Point(e.X, e.Y))
52
53Dim isClickInCell As Boolean = hti.Column = Me .m_columnNum
54
55m_Row = -1
56
57Dim rect As New Rectangle(0, 0, 0, 0)
58
59If isClickInCell Then
60
61rect = dg.GetCellBounds(hti.Row, hti.Column)
62
63isClickInCell = e.X > rect.Right - Me .m_Face.Width
64
65End If
66
67If isClickInCell Then
68
69Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
70
71g.DrawImage( Me .m_Face, rect.Right - Me .m_Face.Width, rect.Y)
72
73g.Dispose()
74
75RaiseEvent CellButtonClicked( Me , New DataGridCellButtonClickEventArgs(hti.Row, hti.Column))
76
77End If
78
79End Sub
80
81Public Sub HandleMouseDown( ByVal sender As Object , ByVal e As MouseEventArgs)
82
83Dim dg As DataGrid = Me .DataGridTableStyle.DataGrid
84
85Dim hti As DataGrid.HitTestInfo = dg.HitTest( New Point(e.X, e.Y))
86
87Dim isClickInCell As Boolean = hti.Column = Me .m_columnNum
88
89Dim rect As New Rectangle(0, 0, 0, 0)
90
91If isClickInCell Then
92
93rect = dg.GetCellBounds(hti.Row, hti.Column)
94
95isClickInCell = e.X > rect.Right - Me .m_Face.Width
96
97End If
98
99If isClickInCell Then
100
101Dim g As Graphics = Graphics.FromHwnd(dg.Handle)
102
103g.DrawImage( Me .m_FacePressed, rect.Right - Me .m_FacePressed.Width, rect.Y)
104
105g.Dispose()
106
107m_Row = hti.Row
108
109End If
110
111End Sub
112
113' 重绘
114
115Protected 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 )
116
117Dim parent As DataGrid = Me .DataGridTableStyle.DataGrid
118
119' 如果该行是选中行 或者 当前单元格的行号 = 点击行的行号并且当前单元格的列号等于 NEW 的列号参数
120
121Dim current As Boolean = parent.IsSelected(rowNum) Or (parent.CurrentRowIndex = rowNum And parent.CurrentCell.ColumnNumber = Me .m_columnNum)
122
123Dim BackColor As Color
124
125If current Then BackColor = parent.SelectionBackColor Else BackColor = parent.BackColor
126
127Dim ForeColor As Color
128
129If current Then ForeColor = parent.SelectionForeColor Else ForeColor = parent.ForeColor
130
131' 请空单元格
132
133g.FillRectangle( New SolidBrush(BackColor), bounds)
134
135' 绘制值
136
137Dim s As String = Me .GetColumnValueAtRow([source], rowNum).ToString() 'parent[rowNum, 0].ToString() + ((parent[rowNum, 1].ToString())+ " ").Substring(0,2);
138
139g.DrawString(s, parent.Font, New SolidBrush(ForeColor), bounds.X, bounds.Y)
140
141Dim bm As Bitmap
142
143If m_Row = rowNum Then bm = Me .m_FacePressed Else bm = Me .m_Face
144
145g.DrawImage(bm, bounds.Right - bm.Width, bounds.Y)
146
147End Sub
148
149End Class
150
151调用代码:
152
153Private Function getdatagridstyle( ByVal table As DataTable) As DataGridTableStyle
154
155Dim style As New DataGridTableStyle()
156
157style.MappingName = table.TableName
158
159style.RowHeaderWidth = 15
160
161Dim i As Integer
162
163For i = 0 To table.Columns.Count - 1
164
165If i = 1 Then
166
167Dim textButtonColStyle As New mybuttondatagridtablestyle1(i) 'pass the column#
168
169textButtonColStyle.HeaderText = table.Columns(i).ColumnName
170
171textButtonColStyle.MappingName = table.Columns(i).ColumnName
172
173<span style="COLOR:</system.diagnostics.debuggerstepthrough()>