(1) Visual Studio Express 2012 for Windows Desktopを開いて、新規でwindowsフォームアプリケーションを作成。 (2) メニューバーから「ツール」→「ライブラリ パッケージ マネージャ」→「ソリューションのNuGetパッケージの管理」を開く。 (3) オンラインで「MySql」を検索して「MySql.Data」ドライバをインストールする。 (4) フォーム(Form1.vb)にDataGridViewを貼り付ける(コントロール名:DataGridView1)。
(5) フォーム(Form1.vb)のコードを表示して以下を追記。
Imports MySql.Data.MySqlClient Public Class Form1 Public Sub New() ' この呼び出しはデザイナーで必要です。 InitializeComponent()
' InitializeComponent() 呼び出しの後で初期化を追加します。 Construct() End Sub
Private Sub Construct() Dim ds As New DataSet
' sampleデータ取得 Using cmd As New MySqlCommand Try cmd.CommandType = CommandType.StoredProcedure ' ② cmd.CommandText = "stored_sample" cmd.Connection = New MySqlConnection("Database=sampledb;Data Source=localhost;User Id=vb;Password=vb") cmd.Parameters.Add("set_user_id", MySqlDbType.Int32).Value = 2 ' ③ user_id=2を抽出
Using da As New MySqlDataAdapter da.SelectCommand = cmd da.Fill(ds, "sample") End Using Catch ex As MySqlException Throw ex End Try End Using
DataGridView1.DataSource = ds.Tables("sample") End Sub End Class
Public Class DataGridViewCheckBoxCellEx Inherits DataGridViewCheckBoxCell
Protected Overrides Sub OnClick(ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) MyBase.OnClick(e) Dim bBool As Boolean = Me.Value Xor True Me.Value = bBool Me.DataGridView.CurrentCell = Me.DataGridView(e.ColumnIndex + 1, e.RowIndex) ' ① End Sub
' InitializeComponent() 呼び出しの後で初期化を追加します。 construct() End Sub
Private Sub Construct() ' 適当にセル作成 With DataGridView1 For iCol As Integer = 0 To 3 .Columns.Add(iCol.ToString, iCol.ToString) .Columns(iCol).Width = 120 Next .Rows.Add(5) .CurrentCell = DataGridView1(0, 1) End With ' CheckBoxCellを入れる Dim chkCell As New DataGridViewCheckBoxCellEx chkCell.Value = False DataGridView1(0, 0) = chkCell End Sub End Class
Public Class DataGridViewCheckBoxCellEx Inherits DataGridViewCheckBoxCell
Private _caption As String
Public Property Caption As String Set(value As String) _caption = value End Set Get Return _caption End Get End Property
Protected Overrides Sub Paint(graphics As Graphics, clipBounds As Rectangle, cellBounds As Rectangle, rowIndex As Integer, elementState As DataGridViewElementStates, value As Object, formattedValue As Object, errorText As String, cellStyle As DataGridViewCellStyle, advancedBorderStyle As DataGridViewAdvancedBorderStyle, paintParts As DataGridViewPaintParts)
MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts) ' ① Xを+20ずらしてキャプションを描画 Dim indexRect As Rectangle = cellBounds indexRect.X += +20 TextRenderer.DrawText(graphics, _caption, cellStyle.Font, indexRect, cellStyle.ForeColor, TextFormatFlags.Left Or TextFormatFlags.VerticalCenter)
' InitializeComponent() 呼び出しの後で初期化を追加します。 construct() End Sub
Private Sub Construct() ' 適当にセル作成 With DataGridView1 For iCol As Integer = 0 To 3 .Columns.Add(iCol.ToString, iCol.ToString) .Columns(iCol).Width = 120 Next .Rows.Add(5) .CurrentCell = DataGridView1(0, 1) End With ' キャプションを付ける Dim chkCell As New DataGridViewCheckBoxCellEx With chkCell .Caption = "サンプルキャプション" .Value = True End With DataGridView1(0, 0) = chkCell End Sub End Class