จากนั้น
จะได้
คลิกที่พื้นที่ว่างของ Form แล้วเขียน Code ข้อมูลดังต่อไปนี้
cmd.Connection = conn;
conn.Open();
String sql = "select * from Customer ";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
String num;
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.Oledb.12.0;Data Source=D:\\C4160\\C4160.accdb;");
OleDbCommand cmd = new OleDbCommand();
if (textBox1.Text != "" & textBox2.Text != "" & textBox3.Text != "" & textBox4.Text != "" & textBox5.Text != "" & textBox6.Text != "" & textBox7.Text != "" & textBox8.Text != "" & textBox9.Text != "")
{
conn.Open();
cmd.CommandText = "insert into Customer ([cusid], [title], [cusname], [birthday], [bloodgroup], [address], [phone], [congenital], [drugallergy]) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "')";
cmd.ExecuteNonQuery();
cmd.Clone();
String sql = "select * from Customer ";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
conn.Close();
}
conn.Open();
cmd.CommandText = "update Customer set [cusid] = '" + textBox1.Text + "' , [title] = '" + textBox2.Text + "', [cusname] ='" + textBox3.Text + "' , [birthday] ='" + textBox4.Text + "' , [bloodgroup] ='" + textBox5.Text + "' , [address]='" + textBox6.Text + "', [phone]='" + textBox7.Text + "', [congenital]='" + textBox8.Text + "', [drugallergy]='" + textBox9.Text + "' where id = " + num;
cmd.ExecuteNonQuery();
String sql = "select * from Customer ";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
cmd.Clone();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
conn.Close();
conn.Open();
cmd.CommandText = "delete from Customer where [cusid] ='" + textBox1.Text + "'";
cmd.ExecuteNonQuery();
String sql = "select * from Customer ";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
cmd.Clone();
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
textBox7.Text = "";
textBox8.Text = "";
textBox9.Text = "";
conn.Close();
cmd.Connection = conn;
conn.Open();
String sql = "select * from Customer ";
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;
conn.Close();
textBox1.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
textBox3.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox8.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
textBox9.Text = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
num = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();