BaseDataGridView

ColumnHeadersHeightSizeModeをEnableResizingにしないと

ColumnHeaderの高さを変更しても反映されないが、

なぜかここで設定した値が使用先で反映されない。

使用先で手動にてColumnHeadersHeightSizeModeをEnableResizing

に変更する必要がある。

 

Imports System.ComponentModel
Imports System.Web.UI.WebControls
<System.ComponentModel.ToolboxItemAttribute(GetType(System.Drawing.Design.ToolboxItem))>
Public Class BaseDataGridView
Implements IBaseControl
<DefaultValue(0)>
<Browsable(True)>
<Description("当Controlを使用可能となるRoleId")>
Public Property RoleId As Integer = 0 Implements IBaseControl.RoleId
Private _columnHeadersHeightSizeMode As DataGridViewColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
<DefaultValue(DataGridViewColumnHeadersHeightSizeMode.EnableResizing)>
<Browsable(True)>
<Description("EnableResizing")>
Public Shadows Property ColumnHeadersHeightSizeMode As DataGridViewColumnHeadersHeightSizeMode
Get
Return MyBase.ColumnHeadersHeightSizeMode
End Get
Set(value As DataGridViewColumnHeadersHeightSizeMode)
_columnHeadersHeightSizeMode = value
MyBase.ColumnHeadersHeightSizeMode = _columnHeadersHeightSizeMode
End Set
End Property
Public Sub New()
InitializeComponent()
Me.EnableHeadersVisualStyles = False
Me.AlternatingRowsDefaultCellStyle = DefaultAlternatingRowsStyle()
Me.DefaultCellStyle = DefaultCellsStyle()
Me.CellBorderStyle = DataGridViewCellBorderStyle.SingleHorizontal
Me.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.MultiSelect = False
Me.ReadOnly = True
Me.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Me.AllowUserToAddRows = False
Me.AllowUserToDeleteRows = False
Me.AllowUserToOrderColumns = True
Me.RowHeadersVisible = False
Me.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
Me.ColumnHeadersDefaultCellStyle = DefaultColumnHeaderStyle()
Me.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
Me.ColumnHeadersHeight = 25

End Sub
Public Sub ResizeDGV()
Me.DataGridViewLastColumnWidthSetting()
End Sub

Public Sub BaseDataGridView_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles Me.ColumnWidthChanged
Me.ResizeDGV()
End Sub
Private Sub BaseDataGridView_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles Me.DataBindingComplete
Me.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
End Sub
''' <summary>
''' DadaGridViewの全列幅がDataGridViewの幅未満の場合に、最終列を調整することでDataGridViewの幅に合わせる
''' (*)全列幅がDataGridViewの幅以上の場合は何も行わない
''' </summary>
Private Sub DataGridViewLastColumnWidthSetting()
Dim allWidth As Integer = Me.Columns.GetColumnsWidth(DataGridViewElementStates.Visible)
Dim vScrollWidth As Integer = If(Me.Controls.OfType(Of VScrollBar)().First().Visible, SystemInformation.VerticalScrollBarWidth, 0)
Dim lastColmWidth As Integer = Me.Columns.GetLastColumn(DataGridViewElementStates.Visible, Nothing).Width
Dim rowSelectWidth As Integer = If(Me.RowHeadersVisible, Me.RowHeadersWidth, 0)
Dim bothSideBorderWidth As Integer = If(Me.BorderStyle = System.Windows.Forms.BorderStyle.None, 0, 2)
If allWidth <= Me.Width - SystemInformation.VerticalScrollBarWidth Then
Me.Columns.GetLastColumn(DataGridViewElementStates.Visible, Nothing).Width =
Me.Width - (allWidth - lastColmWidth + vScrollWidth + rowSelectWidth + bothSideBorderWidth)
End If
End Sub
Private Function DefaultAlternatingRowsStyle() As DataGridViewCellStyle
Dim dgvStyle As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle With {
.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft,
.BackColor = System.Drawing.Color.FromArgb(CType(CType(230, Byte), Integer), CType(CType(240, Byte), Integer), CType(CType(250, Byte), Integer)),
.Font = New System.Drawing.Font("Meiryo UI", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
}
Return dgvStyle
End Function

Private Function DefaultColumnHeaderStyle() As DataGridViewCellStyle
Dim dgvStyle As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle With {
.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft,
.BackColor = System.Drawing.Color.FromArgb(CType(CType(245, Byte), Integer), CType(CType(245, Byte), Integer), CType(CType(245, Byte), Integer)),
.Font = New System.Drawing.Font("Meiryo UI", 9.0!),
.ForeColor = System.Drawing.SystemColors.WindowText,
.SelectionBackColor = System.Drawing.SystemColors.Highlight,
.SelectionForeColor = System.Drawing.SystemColors.HighlightText,
.WrapMode = System.Windows.Forms.DataGridViewTriState.[True]
}
Return dgvStyle
End Function
Private Function DefaultCellsStyle() As DataGridViewCellStyle
Dim dgvStyle As System.Windows.Forms.DataGridViewCellStyle = New System.Windows.Forms.DataGridViewCellStyle With {
.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft,
.BackColor = System.Drawing.SystemColors.Window,
.Font = New System.Drawing.Font("Meiryo UI", 9.0!),
.ForeColor = System.Drawing.SystemColors.ControlText,
.SelectionBackColor = System.Drawing.SystemColors.Highlight,
.SelectionForeColor = System.Drawing.SystemColors.HighlightText,
.WrapMode = System.Windows.Forms.DataGridViewTriState.[False]
}
Return dgvStyle
End Function
End Class