VB2010 DataGridView

索引サイト




Private Sub ReadData()
odbc_DataBase.Open()

Dim w_SQL As String = ""
w_SQL = w_SQL & "select * from tb_eeepc"
w_SQL = w_SQL & " order by code"
Dim w_Adapter As New System.Data.Odbc.OdbcDataAdapter(w_SQL, odbc_DataBase)
Dim w_DataTable As New DataTable
w_Adapter.Fill(w_DataTable)
Me.DataGridView1.DataSource = w_DataTable

odbc_DataBase.Close()

DataGridView1.Columns(1).HeaderText = "名称"
DataGridView1.Columns(2).HeaderText = "調整値"
DataGridView1.Columns(2).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
End Sub




Private Sub DataGridView1_CellLeave(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellLeave
Debug.WriteLine(DataGridView1.CurrentRow.ToString)
Debug.WriteLine(Me.DataGridView1.CurrentRow.Cells("name").Value.ToString)
End Sub




Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Me.TextBox_code.Text = Me.DataGridView1.CurrentRow.Cells("code").Value.ToString
Me.TextBox_name.Text = Me.DataGridView1.CurrentRow.Cells("name").Value.ToString
Me.TextBox_data.Text = Format(Me.DataGridView1.CurrentRow.Cells("data").Value, "###0.00")
End Sub




Private Sub Button_Update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Update.Click
odbc_DataBase.Open()
Try
Dim w_SQL = "update tb_eeepc set data = " & Val(Me.TextBox_data.Text) & " where code = '" & Me.TextBox_code.Text & "'"
Dim w_cmd As New System.Data.Odbc.OdbcCommand(w_SQL, odbc_DataBase)
w_cmd.ExecuteNonQuery()

MessageBox.Show("Updated")
Catch ex As Exception
MessageBox.Show("Update Error")
End Try
odbc_DataBase.Close()
Call ReadData()
End Sub




AX