Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
DataGridView1(0, e.RowIndex).Value = IIf(Convert.ToInt32(DataGridView1(1, e.RowIndex).Value) Mod 2 = 0, "แบบคู่", "แบบคี่")
End Sub
โดยข้อมูลที่โชว์ในdatagridview มีคราวๆดังนี้
ID Product Type NUM
1 ส้ม 1
2 แตงโม 1
3 ผักชี 2
4 คะน้า 2
5 กะหล่ำ 2
โค้ด
Code (VB.NET)
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Dim con As New OleDbConnection
Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
Label1.Enabled = True
TextBox1.Enabled = True
Label2.Enabled = False
TextBox2.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
Label1.Enabled = False
TextBox1.Enabled = False
Label2.Enabled = True
TextBox2.Enabled = True
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\admim\Documents\ฐานข้อมูล31.accdb"
con.Open()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * FROM tb_Products", con)
da.Fill(dt)
Me.DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Enabled = True
TextBox1.Enabled = True
Label2.Enabled = True
TextBox2.Enabled = True
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\admim\Documents\ฐานข้อมูล31.accdb"
con.Open()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * FROM tb_DD", con)
da.Fill(dt)
Me.DataGridView1.DataSource = dt.DefaultView
con.Close()
End Sub
End Class
Imports System.Data.OleDb
Imports System.Data
Public Class Form1
Dim con As New OleDbConnection
Private Sub SetDisplay(chk As Boolean)
Label1.Enabled = chk
TextBox1.Enabled = chk
Label2.Enabled = Not chk
TextBox2.Enabled = Not chk
End Sub
Public Sub DisplayDatagrid(sql As String, dgv As DataGridView)
con.Open()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter(sql, con)
da.Fill(dt)
dgv.DataSource = dt.DefaultView
con.Close()
End Sub
Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
SetDisplay(True)
End Sub
Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged
SetDisplay(False)
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
DisplayDatagrid("SELECT * FROM tb_Products", DataGridView1)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\admim\Documents\ฐานข้อมูล31.accdb"
Label1.Enabled = True
TextBox1.Enabled = True
Label2.Enabled = True
TextBox2.Enabled = True
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
DisplayDatagrid("SELECT * FROM tb_DD", DataGridView1)
End Sub
End Class