The practice of a general DataGridTableStyle
Last Update:2017-02-28
Source: Internet
Author: User
The DataGrid a generic DataGridTableStyle approach
Ah, these two days like to write "universal" things.
This class, you can achieve adaptive column width, read-only, Time division display, events, arbitrary placement, click the background color settings, and so on, easy to operate. It's just a matter of time (going out tomorrow), it's not perfect today. For reference only, you can add something else. The following is only a list of code, not clear of their own trial to check the information on the line.
Public Class TableStyle
Private M_datagridtablestyle as DataGridTableStyle
Private m_DataGrid as DataGrid
Private m_datatable as DataTable
'//Add event handling, here only consider DataGridTextBoxColumn double-click event
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
'//Set DataGrid
Public Property [DataGrid] () as DataGrid
Get
Return m_DataGrid
End Get
Set (ByVal Value as DataGrid)
m_DataGrid = Value
End Set
End Property
'//Return template
Public ReadOnly Property [DataGridTableStyle] () as DataGridTableStyle
Get
Return M_datagridtablestyle
End Get
End Property
'//initialization
Public Sub Initialize ()
'//Judge Mdatagrid data Source Type
'//If the binding is a dataset or DataViewManager or no data source is bound, exit,
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
'//below consider two data sources, one is DataView, one is 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
'//Add 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
'//Bind to column of DataTable
With Mcolumnstyle
. MappingName = Mdatacolumn.columnname
. HeaderText = Mdatacolumn.columnname
End With
'//Join to DataGridTableStyle
M_DATAGRIDTABLESTYLE.GRIDCOLUMNSTYLES.ADD (Mcolumnstyle)
Next
'//Bind the DataGridTableStyle to the DataGrid
M_DataGrid.TableStyles.Clear ()
M_DATAGRID.TABLESTYLES.ADD (M_datagridtablestyle)
End Sub
'//Adaptive width
Public Sub Autoextend ()
If M_datagridtablestyle is nothing Then Exit Sub
' Take the maximum number of bytes in each field, including field names and values
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
' Refer to the graphics of the DataGrid to assign the actual width
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
'//Add a column after a column
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
'//does not show 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
'//If is the date type, displays the time
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
' Individual edits, except logical types
Public ReadOnly Property Textcolumnstyle (ByVal ColumnName as String) as DataGridTextBoxColumn
Get
If not M_datatable.columns (ColumnName) is no 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
' Test
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 ("name", "Hello")
Mytablestyle.autoextend ()
End Sub
Private Sub Button7_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button7.click
Mytablestyle.textcolumnstyle ("name"). 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