datagridcolumnstyle重写,实现插入不同控件列体验

最近谈论较多的就是Datagrid,特别新手最是郁闷为何没有更好的控件,来满足自已的需求。
其实通过重写可以达到很多不同的功能体验,在这里我们仅仅讨论关于datagridcolumnstyle重写的问题

Power by: landlordh
Datatime: 2005-08-04
转载请注明出处,谢谢

1。重写TextBox:

Public Class XP_TextBox
Inherits System.Windows.Forms.TextBox

#Region " Windows "

Public Sub New()
MyBase.New()

InitializeComponent()

End Sub

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

  1<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
  2'   
  3'TextBox   
  4'   
  5Me.EnableContextMenu = True   
  6Me.EnablePaste = True   
  7Me.Name = "TextBox" 
  8
  9End Sub 
 10
 11#End Region 
 12
 13#Region " Variables " 
 14
 15Private m_EnPaste As Boolean = True   
 16Private m_EnContextMenu As Boolean = True 
 17
 18#End Region 
 19
 20#Region " Property " 
 21
 22Property EnablePaste() As Boolean   
 23Get   
 24Return m_EnPaste   
 25End Get   
 26Set(ByVal Value As Boolean)   
 27m_EnPaste = Value   
 28Me.Invalidate()   
 29End Set   
 30End Property 
 31
 32Property EnableContextMenu() As Boolean   
 33Get   
 34Return m_EnContextMenu   
 35End Get   
 36Set(ByVal Value As Boolean)   
 37m_EnContextMenu = Value   
 38Me.Invalidate()   
 39End Set   
 40End Property 
 41
 42#End Region 
 43
 44Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)   
 45Select Case m.Msg   
 46Case &amp;H302 'paste   
 47RaiseEvent PasteEvent()   
 48If Not m_EnPaste Then Return   
 49Case &amp;H7B 'contextmenu   
 50If Not m_EnContextMenu Then Return   
 51End Select   
 52MyBase.WndProc(m)   
 53End Sub 
 54
 55Public Event PasteEvent() 
 56
 57End Class 
 58
 592。重写datagridcolumnstyle(重点介绍内容): 
 60
 61Imports System.Drawing   
 62Imports System.Windows.Forms 
 63
 64Public NotInheritable Class DataGridTextBoxColumnStyle   
 65Inherits System.Windows.Forms.DataGridColumnStyle 
 66
 67#Region "Declare Property" 
 68
 69Private WithEvents m_TextBox As New Landlord.Component.XP_TextBox   
 70Private IsEditing As Boolean   
 71Private EditingRow As Integer = -1   
 72Private m_oldvalue As String 
 73
 74#End Region 
 75
 76#Region " windows " 
 77
 78Sub New()   
 79Me.m_TextBox.Visible = False   
 80End Sub 
 81
 82Public Sub New(ByVal Container As System.ComponentModel.IContainer)   
 83MyClass.New() 
 84
 85Container.Add(Me)   
 86End Sub 
 87
 88Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)   
 89If disposing Then   
 90If Not (components Is Nothing) Then   
 91components.Dispose()   
 92End If   
 93End If   
 94MyBase.Dispose(disposing)   
 95End Sub 
 96
 97Private components As System.ComponentModel.IContainer 
 98
 99<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()   
100components = New System.ComponentModel.Container   
101End Sub 
102
103#End Region 
104
105#Region "Get Function" 
106
107Protected Overrides Function GetMinimumHeight() As Integer   
108Return m_TextBox.PreferredHeight + 2   
109End Function 
110
111Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer   
112Return m_TextBox.PreferredHeight + 2   
113End Function 
114
115Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size   
116Return New Size(50, m_TextBox.PreferredHeight + 2)   
117End Function 
118
119#End Region 
120
121#Region "Paint" 
122
123Protected 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)   
124Paint(g, bounds, [source], rowNum, False)   
125End Sub 
126
127Protected 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)   
128Dim brush_for As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)   
129Dim brush_bak As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)   
130Paint(g, bounds, [source], rowNum, brush_bak, brush_for, alignToRight)   
131brush_for.Dispose()   
132brush_bak.Dispose()   
133End Sub 
134
135Protected 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 Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)   
136Dim str As String   
137If IsDBNull(GetColumnValueAtRow([source], rowNum)) Then   
138str = Me.NullText   
139Else   
140str = CType(GetColumnValueAtRow([source], rowNum), String)   
141End If   
142Dim brush As Brush = backBrush   
143Dim rect As System.Drawing.Rectangle = bounds   
144g.FillRectangle(brush, rect)   
145If Me.IsEditing And EditingRow = rowNum Then   
146brush = New SolidBrush(Color.White)   
147g.FillRectangle(brush, bounds)   
148End If   
149rect.Offset(0, 2)   
150rect.Height -= 2   
151brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)   
152If Me.DataGridTableStyle.DataGrid.IsSelected(rowNum) Then   
153brush = New SolidBrush(Me.DataGridTableStyle.SelectionBackColor)   
154Dim rectf As RectangleF = New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)   
155g.FillRectangle(brush, rectf)   
156brush = New SolidBrush(Me.DataGridTableStyle.SelectionForeColor)   
157End If   
158If Me.Alignment = HorizontalAlignment.Center Then   
159Dim w As Integer = g.MeasureString(str, Me.DataGridTableStyle.DataGrid.Font, New SizeF(bounds.Width, bounds.Height)).Width   
160rect.X = rect.X + (bounds.Width - w) / 2   
161ElseIf Me.Alignment = HorizontalAlignment.Right Then   
162Dim w As Integer = g.MeasureString(str, Me.DataGridTableStyle.DataGrid.Font, New SizeF(bounds.Width, bounds.Height)).Width   
163rect.X = bounds.Right - w   
164End If   
165g.DrawString(str, Me.DataGridTableStyle.DataGrid.Font, brush, rect.X, rect.Y)   
166brush.Dispose()   
167End Sub 
168
169#End Region 
170
171#Region "Overrides Method" 
172
173Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean   
174Me.m_TextBox.Bounds = Rectangle.Empty   
175If Not Me.IsEditing Then   
176Return True   
177End If   
178EditingRow = -1   
179IsEditing = False   
180Try   
181Dim value As Object   
182value = m_TextBox.Text   
183If NullText.Equals(value) Then   
184value = System.DBNull.Value   
185End If   
186SetColumnValueAtRow(dataSource, rowNum, value)   
187Catch ex As Exception   
188Abort(rowNum)   
189Return False   
190End Try   
191invalidate()   
192Return True   
193End Function 
194
195Protected Overrides Sub Abort(ByVal rowNum As Integer)   
196Me.m_TextBox.Text = m_oldvalue 
197
198EditingRow = -1 
199
200If m_TextBox.Focused Then   
201Me.DataGridTableStyle.DataGrid.Focus()   
202End If   
203Me.m_TextBox.Visible = False 
204
205Me.IsEditing = False   
206Me.Invalidate()   
207End Sub 
208
209Protected 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)   
210EditingRow = rowNum   
211IsEditing = True   
212Dim value As String   
213If IsDBNull(GetColumnValueAtRow(source, rowNum)) Then   
214value = Me.NullText   
215Else   
216value = CType(GetColumnValueAtRow(source, rowNum), String)   
217End If   
218m_oldvalue = value   
219If cellIsVisible Then   
220If Not Me.ReadOnly Then   
221Me.m_TextBox.Bounds = New Rectangle(bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2)   
222Me.m_TextBox.Text = value   
223Me.m_TextBox.Select()   
224Me.m_TextBox.Focus()   
225Me.m_TextBox.SelectAll()   
226Me.m_TextBox.Visible = True   
227Me.m_TextBox.Flat = True   
228End If   
229Else   
230Me.m_TextBox.Text = value   
231'滚动时会丢失焦点   
232'Me.m_TextBox.Visible = False   
233End If   
234If Me.m_TextBox.Visible Then   
235DataGridTableStyle.DataGrid.Invalidate(bounds)   
236End If   
237End Sub 
238
239Protected Overloads Overrides Sub SetDataGridInColumn(ByVal value As System.Windows.Forms.DataGrid)   
240MyBase.SetDataGridInColumn(value)   
241If Not m_TextBox.Parent Is Nothing Then   
242m_TextBox.Parent.Controls.Remove(Me.m_TextBox)   
243End If   
244If Not value Is Nothing Then   
245value.Controls.Add(Me.m_TextBox)   
246End If   
247End Sub 
248
249Protected Overrides Sub ConcedeFocus()   
250EditingRow = -1   
251'否则先点到新增行,再回选非新行格时该列最后一行的值变为(null)   
252IsEditing = False   
253Me.m_TextBox.Visible = False   
254invalidate()   
255End Sub 
256
257Protected Overrides Sub EnterNullValue()   
258Me.m_TextBox.Text = Me.NullText   
259End Sub 
260
261Private Sub m_TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles m_TextBox.KeyPress   
262If Not Char.IsControl(e.KeyChar) Then   
263Me.IsEditing = True   
264MyBase.ColumnStartedEditing(m_TextBox)   
265End If   
266End Sub 
267
268Private Sub m_TextBox_PasteEvent() Handles m_TextBox.PasteEvent   
269Me.IsEditing = True   
270Me.ColumnStartedEditing(m_TextBox)   
271End Sub 
272
273#End Region 
274
275End Class 
276
2773。使用:   
278新建一个空窗体,拖入datagrid,窗体load事件中代码如下   
279Private idtb_temp As New DataTable 
280
281Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
282
283idtb_temp = New DataTable("NameTable")   
284idtb_temp.Columns.Add(New DataColumn("normal"))   
285idtb_temp.Columns.Add(New DataColumn("textbox1"))   
286idtb_temp.Columns.Add(New DataColumn("combobox1"))   
287Dim dateColumns As DataColumn   
288dateColumns = New DataColumn("datetime1", Type.GetType("System.DateTime"))   
289idtb_temp.Columns.Add(dateColumns)   
290idtb_temp.Columns.Add(New DataColumn("checkbox1", Type.GetType("System.Boolean"))) 
291
292Dim idrw_row As DataRow   
293Dim i As Integer   
294For i = 0 To 20   
295idrw_row = idtb_temp.NewRow   
296idrw_row.Item("normal") = "names"   
297idrw_row.Item("textbox1") = "nick"   
298idrw_row.Item("combobox1") = i.ToString   
299idrw_row.Item("datetime1") = "2004-06-04"   
300idrw_row.Item("checkbox1") = True   
301idtb_temp.Rows.Add(idrw_row)   
302Next 
303
304Me.DataGrid1.DataSource = idtb_temp 
305
306Dim myGridStyle As Windows.Forms.DataGridTableStyle = New Windows.Forms.DataGridTableStyle   
307myGridStyle.MappingName = "NameTable"   
308myGridStyle.PreferredRowHeight = 30   
309myGridStyle.SelectionBackColor = Color.Blue   
310myGridStyle.BackColor = Color.Yellow 
311
312  
313Dim c1 As Windows.Forms.DataGridTextBoxColumn = New Windows.Forms.DataGridTextBoxColumn   
314With c1   
315.MappingName = "normal"   
316.Width = 100   
317.HeaderText = "normal"   
318.Alignment = HorizontalAlignment.Center   
319End With   
320myGridStyle.GridColumnStyles.Add(c1) 
321
322Dim c2 As Landlord.Component.DragGrid.DataGridTextBoxColumnStyle = New Landlord.Component.DragGrid.DataGridTextBoxColumnStyle   
323With c2   
324.MappingName = "textbox1"   
325.Width = 100   
326.HeaderText = "textbox1"   
327End With   
328myGridStyle.GridColumnStyles.Add(c2) 
329
330Dim c3 As Landlord.Component.DragGrid.DataGridComboBoxColumnStyle = New Landlord.Component.DragGrid.DataGridComboBoxColumnStyle   
331With c3   
332.MappingName = "combobox1"   
333.HeaderText = "combobox1"   
334.Width = 100   
335.AddItem("111")   
336.AddItem("222")   
337.Alignment = HorizontalAlignment.Center   
338End With   
339myGridStyle.GridColumnStyles.Add(c3) 
340
341Dim c4 As Landlord.Component.DragGrid.DataGridDateTimePickerColumnStyle = New Landlord.Component.DragGrid.DataGridDateTimePickerColumnStyle   
342With c4   
343.MappingName = "datetime1"   
344.HeaderText = "datetime1"   
345.Width = "100"   
346.Alignment = HorizontalAlignment.Center   
347End With   
348myGridStyle.GridColumnStyles.Add(c4) 
349
350Dim c5 As Landlord.Component.DragGrid.DataGridCheckBoxColumnStyle = New Landlord.Component.DragGrid.DataGridCheckBoxColumnStyle   
351With c5   
352.MappingName = "checkbox1"   
353.HeaderText = "checkbox1"   
354.Width = 100   
355.TrueColor = Color.Red   
356End With   
357myGridStyle.GridColumnStyles.Add(c5) 
358
359Me.DataGrid1.TableStyles.Clear()   
360Me.DataGrid1.TableStyles.Add(myGridStyle) 
361
362End Sub 
363
3644。说明:   
365其他控件的加入原理基本一样,这里就不重复了 
366
3675。效果图: 
368
369![](http://blog.csdn.net/images/blog_csdn_net/landlordh/90355/o_datagrid.gif)</system.diagnostics.debuggerstepthrough()></system.diagnostics.debuggerstepthrough()>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus