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
一个通用的DataGridTableStyle的做法
80酷酷网 80kuku.com