|
分類:[C#]
■No5321に返信(まじんさんの記事)
なんとか 完成しました
Imports System.Windows.Forms Imports System.Drawing Public Class GridViewSP Inherits Windows.Forms.DataGridView Dim Pan As Panel Dim _HedSet() As String Dim FastSet As Boolean = False Dim _ColCoun As Integer = 30 Dim _Lab(_ColCoun) As Label Dim _Set(_ColCoun) As Integer Dim _HedColor As Color = System.Drawing.SystemColors.Control
Public Sub New()
Pan = New Panel Console.WriteLine("new") Dim c As Integer Do _Lab(c) = New Label Pan.Controls.Add(_Lab(c)) _Lab(c).Visible = False c += 1 Loop Until c >= _ColCoun If HedSet Is Nothing Then Exit Sub
End Sub Public Function HedLab() As ArrayList _colcoun = Me.columns.count - 1 Dim l As Integer = 0 Do _Set(l) = l l += 1 Loop Until l >= _ColCoun + 1 Dim Hed As New ArrayList Dim lab As New ArrayList For Each c As String In HedSet Dim labNo As Integer labNo = CInt(Mid(c, 1, 2)) lab.Add(labNo) Dim co As Integer = CInt(Mid(c, 3, 2)) If co > 1 Then For i As Integer = 0 To co - 2 lab.Add(labNo + i + 1) Next End If Next For Each cc As Integer In _Set Dim ari As Integer = 0 Dim ccc As Integer For Each ccc In lab If cc = ccc Then ari = 1 Exit For End If Next If ari = 0 Then Hed.Add(cc) End If Next Return Hed End Function Public Property HedColor() As Color Get HedColor = _HedColor End Get Set(ByVal value As Color) _HedColor = value End Set End Property Public Property HedSet() As String() Get HedSet = _HedSet End Get Set(ByVal value As String()) 'ここは結合したい列を 結合したいところだけ 例えば2列目から4列分結合したいなら 4桁の文字列で”0204”というふうに入力 _HedSet = value End Set End Property Public Sub HedSetting2() If HedSet Is Nothing Then Exit Sub If HedSet.Length < 1 Then Pan.Visible = False Exit Sub End If If FastSet = True Then Exit Sub
If Me.Columns.Count > 0 Then If Not HedSet Is Nothing Then Pan.Visible = True
Dim par As Control = Me.Parent par.Controls.Add(Pan) Pan.BringToFront()
Dim len As Integer = HedSet.Length If len < 1 Then Exit Sub Me.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing Dim fon As Font = Me.ColumnHeadersDefaultCellStyle.Font
Dim f As Integer = 0
With Pan .BorderStyle = Windows.Forms.BorderStyle.None .Top = Me.Top + 1 .Left = Me.Left + Me.RowHeadersWidth .Width = Me.Width - Me.RowHeadersWidth - 17 .Height = Me.ColumnHeadersHeight .Visible = True End With
Dim ii As Integer = 0
For Each c As String In HedSet
Dim st As String = c Dim setHed As Integer = CInt(Mid(st, 1, 2)) Dim setCou As Integer = CInt(Mid(st, 3, 2)) Dim lef As Integer = 0 Dim top As Integer = Me.Top Dim wid As Integer = 0 Dim hig As Integer = Me.ColumnHeadersHeight lef = Me.RowHeadersWidth Dim lef2 As Integer = 0 If setHed > 1 Then For i As Integer = 0 To setHed - 2 lef2 = lef2 + Me.Columns(i).Width Next End If lef = lef + Me.Left Dim l As Integer = 0 Do wid = wid + Me.Columns(setHed - 1 + l).Width l += 1 Loop Until l >= setCou If Not _Lab Is Nothing Then With _Lab(ii) .AutoSize = False .Top = 0 .Left = lef2 .Tag = lef2 .Width = wid .Height = hig .BorderStyle = Windows.Forms.BorderStyle.FixedSingle .BackColor = HedColor .Text = Me.Columns(setHed - 1).HeaderText .TextAlign = ContentAlignment.MiddleCenter .Font = fon .BringToFront() .Visible = True End With End If f += 1 ii += 1 Next For Each cc As Integer In HedLab() If cc > 0 Then Dim st As String = cc Dim setHed As Integer = cc Dim setCou As Integer = 1 Dim lef As Integer = 0 Dim top As Integer = Me.Top Dim wid As Integer = 0 Dim hig As Integer = Me.ColumnHeadersHeight lef = Me.RowHeadersWidth Dim lef2 As Integer = 0 If setHed > 1 Then For i As Integer = 0 To setHed - 2 lef2 = lef2 + Me.Columns(i).Width Next End If lef = lef + Me.Left
wid = Me.Columns(setHed).Width
With _Lab(cc) .AutoSize = False .Top = 0 .Left = lef2 .Tag = lef2 .Width = wid .Height = hig .BorderStyle = Windows.Forms.BorderStyle.FixedSingle .BackColor = HedColor .Text = Me.Columns(setHed - 1).HeaderText .TextAlign = ContentAlignment.MiddleCenter .Font = fon .BringToFront() .Visible = True End With End If Next End If End If End Sub
Private Sub GridViewSP_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed If Not Pan Is Nothing Then Pan.Dispose() For Each l As Label In _Lab If Not l Is Nothing Then l.Dispose() Next End Sub
Private Sub GridViewSP_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint If FastSet = False Then HedSetting2() FastSet = True Me.Refresh() End If End Sub
Private Sub GridViewSP_Scroll(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles Me.Scroll If _Lab Is Nothing Then Exit Sub Dim Shado As Object = Me.FirstDisplayedScrollingColumnHiddenWidth Dim NowInd As Object = Me.FirstDisplayedScrollingColumnIndex() Dim ShadWid As Integer If NowInd > 0 Then For i As Integer = 0 To NowInd - 1 ShadWid = ShadWid + Me.Columns(i).Width Next End If ShadWid = ShadWid + Shado
For Each c As Label In _Lab If Not c Is Nothing Then c.Left = CInt(c.Tag) - ShadWid End If Next End Sub End Class
|