DataGridViewの参照と設定

ページ名:DataGridViewの参照と設定

''' <summary>
''' DataGridViewの参照と設定
''' </summary>
''' <remarks></remarks>
Public Class Form1

    Private Enum me_Col1
        c0
    End Enum

    Private Enum me_Col2
        c0
    End Enum

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Dim i As Integer = Me.DataGridView1.Rows.Count
        Me.DataGridView1.Rows.Add(1)
        Me.DataGridView1.CurrentCell = Me.DataGridView1.Rows(i).Cells(me_Col1.c0)
    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Call ms_Show()
    End Sub

    Private Sub DataGridView1_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
        If e.ColumnIndex < 0 AndAlso e.RowIndex >= 0 Then
            Call e.Paint(e.ClipBounds, DataGridViewPaintParts.All)
            Dim rect As Rectangle = e.CellBounds
            Call rect.Inflate(-2, -2)
            Call TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, rect, e.CellStyle.ForeColor, TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)
            e.Handled = True
        End If
    End Sub

    Private Sub DataGridView2_CellPainting(sender As Object, e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView2.CellPainting
        If e.ColumnIndex < 0 AndAlso e.RowIndex >= 0 Then
            Call e.Paint(e.ClipBounds, DataGridViewPaintParts.All)
            Dim rect As Rectangle = e.CellBounds
            Call rect.Inflate(-2, -2)
            Call TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), e.CellStyle.Font, rect, e.CellStyle.ForeColor, TextFormatFlags.Right Or TextFormatFlags.VerticalCenter)
            e.Handled = True
        End If
    End Sub

    Private Sub ms_Show()
        Dim w_List1 As List(Of String) = mf_List1()
        Me.DataGridView2.Rows.Clear()
        If w_List1.Count > 0 Then
            Me.DataGridView2.Rows.Add(w_List1.Count)
            For i As Integer = 0 To w_List1.Count - 1
                Me.DataGridView2.Rows(i).Cells(me_Col2.c0).Value = w_List1(i)
            Next
            Me.DataGridView2.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        End If
    End Sub

    Private Function mf_List1() As List(Of String)
        Dim ret As New List(Of String)
        For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
            ret.Add(mf_ObjectToString(Me.DataGridView1.Rows(i).Cells(me_Col1.c0).Value))
        Next
        Return ret
    End Function

    Private Function mf_ObjectToString(wp_Object As Object) As String
        Dim ret As String = ""
        If Not wp_Object Is Nothing Then
            ret = wp_Object.ToString
        End If
        Return ret
    End Function

End Class
 

シェアボタン: このページをSNSに投稿するのに便利です。

コメント

返信元返信をやめる

※ 悪質なユーザーの書き込みは制限します。

最新を表示する

NG表示方式

NGID一覧