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

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

'

'TextBox

'

Me.EnableContextMenu = True

Me.EnablePaste = True

Me.Name = "TextBox"

End Sub

#End Region

#Region " Variables "

Private m_EnPaste As Boolean = True

Private m_EnContextMenu As Boolean = True

#End Region

#Region " Property "

Property EnablePaste() As Boolean

Get

Return m_EnPaste

End Get

Set(ByVal Value As Boolean)

m_EnPaste = Value

Me.Invalidate()

End Set

End Property

Property EnableContextMenu() As Boolean

Get

Return m_EnContextMenu

End Get

Set(ByVal Value As Boolean)

m_EnContextMenu = Value

Me.Invalidate()

End Set

End Property

#End Region

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

Select Case m.Msg

Case &H302 'paste

RaiseEvent PasteEvent()

If Not m_EnPaste Then Return

Case &H7B 'contextmenu

If Not m_EnContextMenu Then Return

End Select

MyBase.WndProc(m)

End Sub

Public Event PasteEvent()

End Class

2。重寫datagridcolumnstyle(重點介紹內容):

Imports System.Drawing

Imports System.Windows.Forms

Public NotInheritable Class DataGridTextBoxColumnStyle

Inherits System.Windows.Forms.DataGridColumnStyle

#Region "Declare Property"

Private WithEvents m_TextBox As New Landlord.Component.XP_TextBox

Private IsEditing As Boolean

Private EditingRow As Integer = -1

Private m_oldvalue As String

#End Region

#Region " windows "

Sub New()

Me.m_TextBox.Visible = False

End Sub

Public Sub New(ByVal Container As System.ComponentModel.IContainer)

MyClass.New()

Container.Add(Me)

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

Private components As System.ComponentModel.IContainer

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

components = New System.ComponentModel.Container

End Sub

#End Region

#Region "Get Function"

Protected Overrides Function GetMinimumHeight() As Integer

Return m_TextBox.PreferredHeight + 2

End Function

Protected Overrides Function GetPreferredHeight(ByVal g As System.Drawing.Graphics, ByVal value As Object) As Integer

Return m_TextBox.PreferredHeight + 2

End Function

Protected Overrides Function GetPreferredSize(ByVal g As System.Drawing.Graphics, ByVal value As Object) As System.Drawing.Size

Return New Size(50, m_TextBox.PreferredHeight + 2)

End Function

#End Region

#Region "Paint"

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)

Paint(g, bounds, [source], rowNum, False)

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)

Dim brush_for As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)

Dim brush_bak As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)

Paint(g, bounds, [source], rowNum, brush_bak, brush_for, alignToRight)

brush_for.Dispose()

brush_bak.Dispose()

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 backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)

Dim str As String

If IsDBNull(GetColumnValueAtRow([source], rowNum)) Then

str = Me.NullText

Else

str = CType(GetColumnValueAtRow([source], rowNum), String)

End If

Dim brush As Brush = backBrush

Dim rect As System.Drawing.Rectangle = bounds

g.FillRectangle(brush, rect)

If Me.IsEditing And EditingRow = rowNum Then

brush = New SolidBrush(Color.White)

g.FillRectangle(brush, bounds)

End If

rect.Offset(0, 2)

rect.Height -= 2

brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)

If Me.DataGridTableStyle.DataGrid.IsSelected(rowNum) Then

brush = New SolidBrush(Me.DataGridTableStyle.SelectionBackColor)

Dim rectf As RectangleF = New RectangleF(bounds.X, bounds.Y, bounds.Width, bounds.Height)

g.FillRectangle(brush, rectf)

brush = New SolidBrush(Me.DataGridTableStyle.SelectionForeColor)

End If

If Me.Alignment = HorizontalAlignment.Center Then

Dim w As Integer = g.MeasureString(str, Me.DataGridTableStyle.DataGrid.Font, New SizeF(bounds.Width, bounds.Height)).Width

rect.X = rect.X + (bounds.Width - w) / 2

ElseIf Me.Alignment = HorizontalAlignment.Right Then

Dim w As Integer = g.MeasureString(str, Me.DataGridTableStyle.DataGrid.Font, New SizeF(bounds.Width, bounds.Height)).Width

rect.X = bounds.Right - w

End If

g.DrawString(str, Me.DataGridTableStyle.DataGrid.Font, brush, rect.X, rect.Y)

brush.Dispose()

End Sub

#End Region

#Region "Overrides Method"

Protected Overrides Function Commit(ByVal dataSource As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer) As Boolean

Me.m_TextBox.Bounds = Rectangle.Empty

If Not Me.IsEditing Then

Return True

End If

EditingRow = -1

IsEditing = False

Try

Dim value As Object

value = m_TextBox.Text

If NullText.Equals(value) Then

value = System.DBNull.Value

End If

SetColumnValueAtRow(dataSource, rowNum, value)

Catch ex As Exception

Abort(rowNum)

Return False

End Try

invalidate()

Return True

End Function

Protected Overrides Sub Abort(ByVal rowNum As Integer)

Me.m_TextBox.Text = m_oldvalue

EditingRow = -1

If m_TextBox.Focused Then

Me.DataGridTableStyle.DataGrid.Focus()

End If

Me.m_TextBox.Visible = False

Me.IsEditing = False

Me.Invalidate()

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, ByVal cellIsVisible As Boolean)

EditingRow = rowNum

IsEditing = True

Dim value As String

If IsDBNull(GetColumnValueAtRow(source, rowNum)) Then

value = Me.NullText

Else

value = CType(GetColumnValueAtRow(source, rowNum), String)

End If

m_oldvalue = value

If cellIsVisible Then

If Not Me.ReadOnly Then

Me.m_TextBox.Bounds = New Rectangle(bounds.X + 1, bounds.Y + 1, bounds.Width - 2, bounds.Height - 2)

Me.m_TextBox.Text = value

Me.m_TextBox.Select()

Me.m_TextBox.Focus()

Me.m_TextBox.SelectAll()

Me.m_TextBox.Visible = True

Me.m_TextBox.Flat = True

End If

Else

Me.m_TextBox.Text = value

'滾動時會丟失焦點

'Me.m_TextBox.Visible = False

End If

If Me.m_TextBox.Visible Then

DataGridTableStyle.DataGrid.Invalidate(bounds)

End If

End Sub

Protected Overloads Overrides Sub SetDataGridInColumn(ByVal value As System.Windows.Forms.DataGrid)

MyBase.SetDataGridInColumn(value)

If Not m_TextBox.Parent Is Nothing Then

m_TextBox.Parent.Controls.Remove(Me.m_TextBox)

End If

If Not value Is Nothing Then

value.Controls.Add(Me.m_TextBox)

End If

End Sub

Protected Overrides Sub ConcedeFocus()

EditingRow = -1

'否則先點到新增行,再回選非新行格時該列最後一行的值變爲(null)

IsEditing = False

Me.m_TextBox.Visible = False

invalidate()

End Sub

Protected Overrides Sub EnterNullValue()

Me.m_TextBox.Text = Me.NullText

End Sub

Private Sub m_TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles m_TextBox.KeyPress

If Not Char.IsControl(e.KeyChar) Then

Me.IsEditing = True

MyBase.ColumnStartedEditing(m_TextBox)

End If

End Sub

Private Sub m_TextBox_PasteEvent() Handles m_TextBox.PasteEvent

Me.IsEditing = True

Me.ColumnStartedEditing(m_TextBox)

End Sub

#End Region

End Class

3。使用:

新建一個空窗體,拖入datagrid,窗體load事件中代碼如下

Private idtb_temp As New DataTable

Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

idtb_temp = New DataTable("NameTable")

idtb_temp.Columns.Add(New DataColumn("normal"))

idtb_temp.Columns.Add(New DataColumn("textbox1"))

idtb_temp.Columns.Add(New DataColumn("combobox1"))

Dim dateColumns As DataColumn

dateColumns = New DataColumn("datetime1", Type.GetType("System.DateTime"))

idtb_temp.Columns.Add(dateColumns)

idtb_temp.Columns.Add(New DataColumn("checkbox1", Type.GetType("System.Boolean")))

Dim idrw_row As DataRow

Dim i As Integer

For i = 0 To 20

idrw_row = idtb_temp.NewRow

idrw_row.Item("normal") = "names"

idrw_row.Item("textbox1") = "nick"

idrw_row.Item("combobox1") = i.ToString

idrw_row.Item("datetime1") = "2004-06-04"

idrw_row.Item("checkbox1") = True

idtb_temp.Rows.Add(idrw_row)

Next

Me.DataGrid1.DataSource = idtb_temp

Dim myGridStyle As Windows.Forms.DataGridTableStyle = New Windows.Forms.DataGridTableStyle

myGridStyle.MappingName = "NameTable"

myGridStyle.PreferredRowHeight = 30

myGridStyle.SelectionBackColor = Color.Blue

myGridStyle.BackColor = Color.Yellow

Dim c1 As Windows.Forms.DataGridTextBoxColumn = New Windows.Forms.DataGridTextBoxColumn

With c1

.MappingName = "normal"

.Width = 100

.HeaderText = "normal"

.Alignment = HorizontalAlignment.Center

End With

myGridStyle.GridColumnStyles.Add(c1)

Dim c2 As Landlord.Component.DragGrid.DataGridTextBoxColumnStyle = New Landlord.Component.DragGrid.DataGridTextBoxColumnStyle

With c2

.MappingName = "textbox1"

.Width = 100

.HeaderText = "textbox1"

End With

myGridStyle.GridColumnStyles.Add(c2)

Dim c3 As Landlord.Component.DragGrid.DataGridComboBoxColumnStyle = New Landlord.Component.DragGrid.DataGridComboBoxColumnStyle

With c3

.MappingName = "combobox1"

.HeaderText = "combobox1"

.Width = 100

.AddItem("111")

.AddItem("222")

.Alignment = HorizontalAlignment.Center

End With

myGridStyle.GridColumnStyles.Add(c3)

Dim c4 As Landlord.Component.DragGrid.DataGridDateTimePickerColumnStyle = New Landlord.Component.DragGrid.DataGridDateTimePickerColumnStyle

With c4

.MappingName = "datetime1"

.HeaderText = "datetime1"

.Width = "100"

.Alignment = HorizontalAlignment.Center

End With

myGridStyle.GridColumnStyles.Add(c4)

Dim c5 As Landlord.Component.DragGrid.DataGridCheckBoxColumnStyle = New Landlord.Component.DragGrid.DataGridCheckBoxColumnStyle

With c5

.MappingName = "checkbox1"

.HeaderText = "checkbox1"

.Width = 100

.TrueColor = Color.Red

End With

myGridStyle.GridColumnStyles.Add(c5)

Me.DataGrid1.TableStyles.Clear()

Me.DataGrid1.TableStyles.Add(myGridStyle)

End Sub

4。說明:

其他控件的加入原理基本一樣,這裏就不重複了

datagridcolumnstyle重寫,實現插入不同控件列體驗
最近談論較多的就是Datagrid,特別新手最是郁悶爲何沒有更好的控件,來滿足自已的需求。其實通過重寫可以達到很多不同的功能體驗,在這裏我們僅僅討論關于datagridcolumnstyle重寫的問題===========================...查看完整版>>datagridcolumnstyle重寫,實現插入不同控件列體驗
 
r在richtextbox中插入動畫/控件
在richtextbox中插入圖片得方法很多,不過一般不能動,要實現動畫至少應該實現一個activex控件,不過.net對activex的支持很差,我試過用c#寫了個activex,不過效果很差,根本久不能動。(備注)這裏的差不多是從別人...查看完整版>>r在richtextbox中插入動畫/控件
 
客戶端插入控件
本文中的控件泛指任何合法的HTML標簽方法一:<div id="myDiv"></div> // 容器var strInnerHtml = "<input id="myInput" type="text">" ; ...查看完整版>>客戶端插入控件
 
給select控件在指定位置插入option(javascript)
問題描述 對于select控件 1 2 3,我們可以通過它的options.add(new Option(v,t))的方法動態的插入一個option選項,但是新增的option的位置只能在最後,卻不能插入到指定的任意位置。解決方法 既然select的...查看完整版>>給select控件在指定位置插入option(javascript)
 
從ASP.NET服務器控件插入客戶端腳本
  簡介   盡管從技術角度講,Microsoft? ASP.NET 服務器控件的所有功能都可以在服務器端執行,但通常情況下通過添加客戶端腳本可以大大增強服務器控件的可用性。例如,ASP.NET 驗證 Web 控件可以在服務器端執行所...查看完整版>>從ASP.NET服務器控件插入客戶端腳本
 
狀態條插入可視控件
首先,在FROM中放置一個狀態條控件Status。調節Status.Panels,在其中插入3個狀態條嵌板。把第二個嵌板的參數Style設置成psOwnerDraw。這一點很重要,如果沒有這樣做,將永遠無法顯示文字以外的東西。然後在狀態條的O...查看完整版>>狀態條插入可視控件
 
如何在PB中調用F1控件如何插入圖片(原創)
如何在PB中調用F1控件如何插入圖片(原創) 如何在PB中調用F1控件如何插入圖片(原創) 一、API聲明:SUBROUTINE CopyMemory(ref METAHEADER Destination , ref blob Source, long Length) LIBRARY "kerne...查看完整版>>如何在PB中調用F1控件如何插入圖片(原創)
 
r在richtextbox中插入動畫/控件
在richtextbox中插入圖片得方法很多,不過一般不能動,要實現動畫至少應該實現一個activex控件,不過.net對activex的支持很差,我試過用c#寫了個activex,不過效果很差,根本久不能動。(備注)這裏的差不多是從別人...查看完整版>>r在richtextbox中插入動畫/控件
 
如何在PB中調用F1控件如何插入圖片(原創)
一、API聲明:SUBROUTINE CopyMemory(ref METAHEADER Destination , ref blob Source, long Length) LIBRARY "kernel32" Alias for "RtlMoveMemory"SUBROUTINE CopyMemory2(ref blob Destination , ref blob Sou...查看完整版>>如何在PB中調用F1控件如何插入圖片(原創)
 
 
回到王朝網路移動版首頁