一個通用的DataGridTableStyle的做法

來源:互聯網
上載者:User
datagrid 一個通用的DataGridTableStyle的做法



哈,這兩天都喜歡寫“通用”的東西。

這個類,可以實現自適應列寬、唯讀、時分顯示、事件、任意位置加列、單擊儲存格背景色設定等等,操作簡便。只是時間關係(明天要出去一趟),今天沒辦法完善。僅供參考,你可以加入別的東西。以下只列代碼了,不清楚的自己試用查資料就行了。



Public Class TableStyle



Private m_DataGridTableStyle As DataGridTableStyle

Private m_DataGrid As DataGrid

Private m_DataTable As DataTable

'//添加事件處理,在此只考慮DataGridTextBoxColumn雙擊事件

Public Delegate Sub ClickEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)

Public Event GridTextBoxDoubleClickEvent As ClickEventHandler

Public Sub GridTextBox_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)

RaiseEvent GridTextBoxDoubleClickEvent(sender, e)

End Sub

'//設定DataGrid

Public Property [DataGrid]() As DataGrid

Get

Return m_DataGrid

End Get

Set(ByVal Value As DataGrid)

m_DataGrid = Value

End Set

End Property

'//返回模板

Public ReadOnly Property [DataGridTableStyle]() As DataGridTableStyle

Get

Return m_DataGridTableStyle

End Get

End Property

'//初始化

Public Sub Initialize()

'//判斷mDataGrid資料來源類型

'//如果綁定的是DataSet或DataViewManager或沒有綁定任何資料來源,則退出,

If TypeOf m_DataGrid.DataSource Is System.Data.DataSet OrElse _

TypeOf m_DataGrid.DataSource Is System.Data.DataViewManager OrElse _

m_DataGrid.DataSource Is Nothing Then Exit Sub



'//以下分別考慮兩種資料來源,一是DataView,一是DataTable

If TypeOf m_DataGrid.DataSource Is System.Data.DataView Then

m_DataTable = CType(m_DataGrid.DataSource, DataView).Table

Else

m_DataTable = CType(m_DataGrid.DataSource, DataTable)

End If



m_DataGridTableStyle = New DataGridTableStyle

m_DataGridTableStyle.MappingName = m_DataTable.TableName

'//加ColumnStyle

Dim mDataColumn As DataColumn

Dim mColumnStyle As DataGridColumnStyle

For Each mDataColumn In m_DataTable.Columns



Select Case mDataColumn.DataType.Name

Case "Boolean"

mColumnStyle = New DataGridBoolColumn

Case Else

mColumnStyle = New DataGridTextBoxColumn

AddHandler CType(mColumnStyle, DataGridTextBoxColumn).TextBox.DoubleClick, AddressOf GridTextBox_DoubleClick

End Select



'//綁定到DataTable的Column

With mColumnStyle

.MappingName = mDataColumn.ColumnName

.HeaderText = mDataColumn.ColumnName

End With



'//加入到DataGridTableStyle

m_DataGridTableStyle.GridColumnStyles.Add(mColumnStyle)

Next



'//將DataGridTableStyle綁定到DataGrid

m_DataGrid.TableStyles.Clear()

m_DataGrid.TableStyles.Add(m_DataGridTableStyle)

End Sub

'//自適應寬度

Public Sub AutoExtend()

If m_DataGridTableStyle Is Nothing Then Exit Sub

'取各欄位的最大位元組數,包括欄位名和值

Dim mRow As DataRow

Dim mColumn As DataColumn

For Each mColumn In m_DataTable.Columns

m_DataGridTableStyle.GridColumnStyles(mColumn.ColumnName).Width = GetColumnMaxWidth(0, mColumn.ColumnName)

Next



For Each mRow In m_DataTable.Rows

For Each mColumn In m_DataTable.Columns

If Not IsDBNull(mRow(mColumn.ColumnName)) Then

m_DataGridTableStyle.GridColumnStyles(mColumn.ColumnName).Width = _

GetColumnMaxWidth(m_DataGridTableStyle.GridColumnStyles(mColumn.ColumnName).Width, mRow(mColumn.ColumnName).ToString)

End If

Next

Next



'參照DataGrid的Graphics賦實際寬度

For Each mColumnStyle As DataGridColumnStyle In m_DataGridTableStyle.GridColumnStyles

mColumnStyle.Width = ColumnWidth(mColumnStyle.Width)

Next

End Sub



Private Function GetColumnMaxWidth(ByVal MaxWidth As Integer, ByVal mString As String) As Integer

Dim mLength As Integer

mLength = System.Text.Encoding.Default.GetBytes(mString).Length()

If MaxWidth < mLength Then

Return mLength

Else

Return MaxWidth

End If

End Function

Private Function ColumnWidth(ByVal MaxWidth As Integer) As Integer

Dim mGraphics As Graphics = m_DataGrid.CreateGraphics

Dim mColWidth As Single

mColWidth = mGraphics.MeasureString(New String(CType("A", Char), MaxWidth), m_DataGrid.Font).Width + 2

Return CType(mColWidth, Integer)

End Function

'//在某列後添加一列

Public Sub AddColumn(ByVal PosColumnName As String, ByVal ColumnName As String)

If m_DataGridTableStyle Is Nothing Then Exit Sub

If Not m_DataTable.Columns.Contains(PosColumnName) Then Exit Sub

If m_DataTable.Columns.Contains(ColumnName) Then Exit Sub

Dim tmpStyle As New DataGridTableStyle

For Each mColumnStyle As DataGridColumnStyle In m_DataGridTableStyle.GridColumnStyles

tmpStyle.GridColumnStyles.Add(mColumnStyle)

If mColumnStyle.HeaderText.Equals(PosColumnName) Then

Dim tmpTextColumn As New DataGridTextBoxColumn

m_DataTable.Columns.Add(ColumnName)

tmpTextColumn.HeaderText = ColumnName

tmpTextColumn.MappingName = ColumnName

tmpStyle.GridColumnStyles.Add(tmpTextColumn)

End If

Next

m_DataGrid.TableStyles.Clear()

tmpStyle.MappingName = m_DataGridTableStyle.MappingName

m_DataGridTableStyle = tmpStyle

m_DataGrid.TableStyles.Add(m_DataGridTableStyle)

End Sub

'//不顯示NULL

Public WriteOnly Property NotShowNull() As Boolean

Set(ByVal Value As Boolean)

For Each mColumnStyle As DataGridColumnStyle In m_DataGridTableStyle.GridColumnStyles

If Value Then

mColumnStyle.NullText = ""

Else

mColumnStyle.NullText = "(NULL)"

End If

Next

End Set

End Property

'//如果是日期類型,顯示時間

Public WriteOnly Property ShowTimeFormat() As Boolean

Set(ByVal Value As Boolean)

For Each mColumnStyle As DataGridColumnStyle In m_DataGridTableStyle.GridColumnStyles

If Not mColumnStyle.MappingName = "" AndAlso m_DataTable.Columns(mColumnStyle.MappingName).DataType.Name.IndexOf("Date") <> -1 Then

If Value Then

CType(mColumnStyle, DataGridTextBoxColumn).Format = "yyyy-MM-dd hh:mm:ss"

Else

CType(mColumnStyle, DataGridTextBoxColumn).Format = "yyyy-MM-dd"

End If

End If

Next

End Set

End Property

'個別編輯,除邏輯類型外

Public ReadOnly Property TextColumnStyle(ByVal ColumnName As String) As DataGridTextBoxColumn

Get

If Not m_DataTable.Columns(ColumnName) Is Nothing AndAlso Not m_DataTable.Columns(ColumnName).DataType.Name.Equals("Boolean") Then

Return CType(m_DataGridTableStyle.GridColumnStyles(ColumnName), DataGridTextBoxColumn)

Else

Return Nothing

End If

End Get

End Property

End Class



'測試

Dim myTableStyle As New TableStyle

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim ds As New DataSet

Me.SqlConnection1.Open()

Me.SqlDataAdapter1.Fill(ds)

Me.SqlConnection1.Close()

Me.DataGrid1.DataSource = ds.Tables(0)
AddHandler myTableStyle.GridTextBoxDoubleClickEvent, AddressOf


End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

With myTableStyle

.DataGrid = Me.DataGrid1

.Initialize()

End With

Me.DataGridTextColumn_DoubleClick

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

myTableStyle.NotShowNull = True

myTableStyle.ShowTimeFormat = True

End Sub



Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

myTableStyle.NotShowNull = False

myTableStyle.ShowTimeFormat = False

End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

myTableStyle.DataGridTableStyle.GridColumnStyles(2).ReadOnly = True

End Sub



Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

myTableStyle.AddColumn("姓名", "Hello")

myTableStyle.AutoExtend()

End Sub



Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

myTableStyle.TextColumnStyle("姓名").Width=0

End Sub



Private Sub DataGridTextColumn_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)

Dim mTextBox As TextBox = CType(sender, TextBox)

mTextBox.BackColor = System.Drawing.Color.Blue

MsgBox(mTextBox.Text)

End Sub




相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。