なにやらかにやらメモ

DataGridViewSynchronizer


PageLastUpdate: 2007-11-13 /today: - /yesterday: -

DataGridViewSynchronizer

2007/11/13
上下に並べたふたつのDataGridViewの列表示を同期させるクラス。
Option Strict On
Public Class DataGridViewSynchronizer
    Private grd1 As DataGridView
    Private grd2 As DataGridView
    Public Sub Add(ByRef datagridview1 As DataGridView, ByRef datagridview2 As DataGridView)
        Try
            Me.grd1 = datagridview1
            Me.grd2 = datagridview2
            For Each column As DataGridViewColumn In grd1.Columns
                If grd2.Columns.Contains(column.Name) = False Then
                    MessageBox.Show("連動させるグリッドの列構成が違います。完全に一致したグリッドでなければ連動させることはできません。")
                End If
            Next
            For Each column As DataGridViewColumn In grd2.Columns
                If grd1.Columns.Contains(column.Name) = False Then
                    MessageBox.Show("連動させるグリッドの列構成が違います。完全に一致したグリッドでなければ連動させることはできません。")
                End If
            Next
            grd1.SuspendLayout()
            grd2.SuspendLayout()
            For Each column1 As DataGridViewColumn In grd1.Columns
                Dim column2 As DataGridViewColumn = grd2.Columns(column1.Name)
                column2.DisplayIndex = column1.DisplayIndex
                column2.Frozen = column1.Frozen
                column2.HeaderText = column1.HeaderText
                column2.Resizable = column1.Resizable
                column2.Visible = column1.Visible
                column2.Width = column1.Width
 
                column2.AutoSizeMode = DataGridViewAutoSizeColumnMode.None
            Next
 
            AddHandler grd1.ColumnWidthChanged, AddressOf ColumnWidthChanged1
            AddHandler grd1.ColumnDisplayIndexChanged, AddressOf ColumnDisplayIndexChanged1
            AddHandler grd1.Scroll, AddressOf Scroll1
            AddHandler grd1.ColumnStateChanged, AddressOf ColumnStateChanged1
 
 
            AddHandler grd2.ColumnWidthChanged, AddressOf ColumnWidthChanged2
            AddHandler grd2.ColumnDisplayIndexChanged, AddressOf ColumnDisplayIndexChanged2
            AddHandler grd2.Scroll, AddressOf Scroll2
            AddHandler grd2.ColumnStateChanged, AddressOf ColumnStateChanged2
 
            grd1.ResumeLayout()
            grd2.ResumeLayout()
 
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
 
    Public Sub ColumnWidthChanged1(ByVal sender As Object, ByVal e As DataGridViewColumnEventArgs)
        Try
            If grd2.Columns(e.Column.Name).Width <> e.Column.Width Then
                grd2.Columns(e.Column.Name).Width = e.Column.Width
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub ColumnDisplayIndexChanged1(ByVal sender As Object, ByVal e As DataGridViewColumnEventArgs)
        Try
            If grd2.Columns(e.Column.Name).DisplayIndex <> e.Column.DisplayIndex Then
                grd2.Columns(e.Column.Name).DisplayIndex = e.Column.DisplayIndex
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub Scroll1(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs)
        Try
            If grd2.HorizontalScrollingOffset <> grd1.HorizontalScrollingOffset Then
                grd2.HorizontalScrollingOffset = grd1.HorizontalScrollingOffset
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub ColumnStateChanged1(ByVal sender As Object, ByVal e As DataGridViewColumnStateChangedEventArgs)
        Try
            Select Case e.StateChanged
                Case DataGridViewElementStates.Frozen
                    If grd2.Columns(e.Column.Name).Frozen <> e.Column.Frozen Then
                        grd2.Columns(e.Column.Name).Frozen = e.Column.Frozen
                    End If
                Case DataGridViewElementStates.Resizable
                    If grd2.Columns(e.Column.Name).Resizable <> e.Column.Resizable Then
                        grd2.Columns(e.Column.Name).Resizable = e.Column.Resizable
                    End If
                Case DataGridViewElementStates.Selected
                    If grd2.Columns(e.Column.Name).Selected <> e.Column.Selected Then
                        grd2.Columns(e.Column.Name).Selected = e.Column.Selected
                    End If
                Case DataGridViewElementStates.Visible
                    If grd2.Columns(e.Column.Name).Visible <> e.Column.Visible Then
                        grd2.Columns(e.Column.Name).Visible = e.Column.Visible
                    End If
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
 
    Public Sub ColumnWidthChanged2(ByVal sender As Object, ByVal e As DataGridViewColumnEventArgs)
        Try
            If grd1.Columns(e.Column.Name).Width <> e.Column.Width Then
                grd1.Columns(e.Column.Name).Width = e.Column.Width
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub ColumnDisplayIndexChanged2(ByVal sender As Object, ByVal e As DataGridViewColumnEventArgs)
        Try
            If grd1.Columns(e.Column.Name).DisplayIndex <> e.Column.DisplayIndex Then
                grd1.Columns(e.Column.Name).DisplayIndex = e.Column.DisplayIndex
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub Scroll2(ByVal sender As Object, ByVal e As System.Windows.Forms.ScrollEventArgs)
        Try
            If grd1.HorizontalScrollingOffset <> grd2.HorizontalScrollingOffset Then
                grd1.HorizontalScrollingOffset = grd2.HorizontalScrollingOffset
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub ColumnStateChanged2(ByVal sender As Object, ByVal e As DataGridViewColumnStateChangedEventArgs)
        Try
            Select Case e.StateChanged
                Case DataGridViewElementStates.Frozen
                    If grd1.Columns(e.Column.Name).Frozen <> e.Column.Frozen Then
                        grd1.Columns(e.Column.Name).Frozen = e.Column.Frozen
                    End If
                Case DataGridViewElementStates.Resizable
                    If grd1.Columns(e.Column.Name).Resizable <> e.Column.Resizable Then
                        grd1.Columns(e.Column.Name).Resizable = e.Column.Resizable
                    End If
                Case DataGridViewElementStates.Selected
                    If grd1.Columns(e.Column.Name).Selected <> e.Column.Selected Then
                        grd1.Columns(e.Column.Name).Selected = e.Column.Selected
                    End If
                Case DataGridViewElementStates.Visible
                    If grd1.Columns(e.Column.Name).Visible <> e.Column.Visible Then
                        grd1.Columns(e.Column.Name).Visible = e.Column.Visible
                    End If
            End Select
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
    Public Sub Remove()
        Try
            RemoveHandler grd1.ColumnWidthChanged, AddressOf ColumnWidthChanged1
            RemoveHandler grd1.ColumnDisplayIndexChanged, AddressOf ColumnDisplayIndexChanged1
            RemoveHandler grd1.Scroll, AddressOf Scroll1
            RemoveHandler grd1.ColumnStateChanged, AddressOf ColumnStateChanged1
 
            RemoveHandler grd2.ColumnWidthChanged, AddressOf ColumnWidthChanged2
            RemoveHandler grd2.ColumnDisplayIndexChanged, AddressOf ColumnDisplayIndexChanged2
            RemoveHandler grd2.Scroll, AddressOf Scroll2
            RemoveHandler grd2.ColumnStateChanged, AddressOf ColumnStateChanged2
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
End Class
 

comment

このページの記述で聞きたいこととか間違ってることとかありましたらコメントを。
名前:
コメント:

すべてのコメントを見る


更新履歴

取得中です。