A universal DataGridTableStyle practice
Ha, I like to write "universal" these two days.
This class can achieve adaptive column width, read-only, time division display, event, arbitrary location, column, click the cell background color setting, etc., easy to operate. It's just a time relationship (I have to go out tomorrow), there is no way to improve today. For reference only, you can join other things. The following is only the code, you don't know how to check the information yourself.
Public Class TableStyle
Private M_DataGridtableStyle As DataGridtablestyle
Private M_DataGrid As DataGrid
Private M_DataTable as DataTable
'// Add event handling, just consider DataGridTextBoxColumn Double-click Event
Public Delegate Sub ClickeVentrandler (Byval E AS ISTEM.EventArgs)
Public Event GridTextBoxDoubleClickevent AS CLICKEVENTHANDLER
Public Sub GridTextBox_doubleClick (Byval E 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 to the template
Public Readonly Property [DataGridTableStyle] () AS DataGridTableStyle
Get
Return M_DataGridTableStyle
END GET
End Property
'//initialization
Public Sub Initialize ()
'// Judgment MDATAGRID data source type
'// If the bind is DataSet or DataViewManager or not bind any data source, exit,
If TypeOf M_DataGrid.DataSource IS System.Data.Dataset ORELSE _
TypeOf M_DataGrid.datasource is System.Data.DataViewManager Oralse_
m_datagrid.datasource is nothing the exit SUB
'// The following considerations two data sources, one is DataView, one is DATATABLE
If TypeOf M_DataGrid.datasource IS System.Data.DataView Then
m_dataable = ctype (m_datagrid.datasource, dataview) .table
Else
m_dataable = ctype (m_datagrid.datasource, dataable)
END IF
m_datagridtablestyle = New DataGridTableStyle
m_datagridtablestyle.mappingname = m_dataable.tablename
'// plumnStyle
DIM MDATACOLUMN As Datacolumn
DIM MCOLUMNSTYLE AS DATAGRIDCOLUMNSTYLEFOR 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 DataBole Column
With mcolumnstyle
. Mappingname = mdatacolumn.columnname
.Headertext = mdatacolumn.columnname
End with
'// Add to DataGridTableStyle
m_datagridtablestyle.gridColumnStyles.Add (McolumnStyle)
NEXT
'// Bind DataGridTableStyle to DataGrid
m_datagrid.tablestyles.clear ()
m_datagrid.tablestyles.add (m_datagridtablestyle)
End Sub
'// Adaptive width
Public Sub AutoExtend ()
IF m_datagridtablestyle is nothing kilid Sub
'Take the maximum number of bytes of each field, including field name and value
Dim Mrow as DataRow
DIM MCOLUMN AS Datacolumn
For Each Mcolumn In M_DataTable.columns
m_datagridtablestyle.gridColumnStyles (McOLUMN.COLUMNNAME) .width = getColumnMaxwidth (0, MCOLUMN.COLUMNAME)
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_datagridtables (Mcolumn.columnname) .width, MROW (MCOLUMN.COLUMNAME) .TOSTRING)
END IF
NEXT
NEXT
'Refer to DataGrid's Graphics assigned 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 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 kilid Sub If not m_dataable.columns.contains (PoscolumnName) THEN EXIT SUB IF m_dataable.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 '// Do 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 kil McolumnStyle.nullText = "" Else McolumnStyle.nullText = "(NULL)" END IF NEXT End set End Property '// If it is the date type, display 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 kil 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 editing, except for logic types Public Readonly Property TextColumnStyle (Byval ColumnName As String) AS DataGridTextBoxColumn Get IF not m_dataable.columns (columnname) is nothing andalso not m_datatable.columns (colornname) .DATATYPE.NAME.EQUALS ("Boolean") THEN Return CType (M_DataGridtableLele.GridColumnStyles (ColumnName), DataGridTextBoxColumn) Else Return Nothing END IF END GET End Property END CLASS 'test DIM MyTableStyle As New TableStyle Private sub button1_click (byvale 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 (byvalgend, 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 (byvale as system.object, byval e as system.eventargs) Handles Button5.click MyTableStyle.DataGridtablesTyle.GridColumnStyles (2) .readonly = true End Sub Private sub Button6_Click (Byval e as system.EventArgs) Handles Button6.click MyTableStyle.AddColumn ("Name", "Hello") MyTableStyle.autoextend () End Sub Private sub button7_click (byvale) Handles Button7.click (Byval E AS System.Object MyTableStyle.TextColumnStyle ("Name") .width = 0 End Sub Private sub DataGridTextColumn_doubleClick (Byval e as system.eventargs) DIM MTEXTBOX As TextBox = CType (Sender, Textbox) MTEXTBOX.BACKCOLOR = system.drawing.color.blue MsgBox (mtextbox.text) End Sub