using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
database db = new database();
string query = "SELECT * FROM product";
SqlCommand cmd = new SqlCommand(query, db.connect());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add(dr["product_id"], dr["product_name"], dr["product_amount"]);
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
dataGridView2.Rows.Add(row.Cells[0].Value.ToString() ,row.Cells[1].Value.ToString() ,row.Cells[2].Value.ToString() , label2.Text );
}
private void search_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
database db = new database();
string query = "SELECT * FROM employee where emp_id = '" + search.Text + "'";
SqlCommand cmd = new SqlCommand(query, db.connect());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dataGridView3.Rows.Add(dr["emp_id"], dr["emp_name"]);
}
}
private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = this.dataGridView3.Rows[e.RowIndex];
label2.Text = row.Cells[1].Value.ToString();
}
private void save_Click(object sender, EventArgs e)
{
string query = "";
for (int i = 0; i < (dataGridView3.Rows.Count - 1); i++ )
{
query += "INSERT INTO sending (sending_product_id,sending_product_name,sending_product_amount,sending_id)";
query += " VALUES ('" + dataGridView3.Rows.Cells[0].Value.ToString() + "','" + dataGridView3.Rows.Cells[1].Value.ToString() + "','" + dataGridView3.Rows.Cells[2].Value.ToString() + "','" + dataGridView3.Rows.Cells[3].Value.ToString() + "');";
}
database db = new database();
SqlCommand cmd = new SqlCommand( query ,db.connect() );
if(cmd.ExecuteNonQuery() > 0){
MessageBox.Show("save");
}
}
}
}
การบันทึกข้อมูล จาก DataGridView ลงในฐานข้อมูล อ่ะค่ะว่าทำยังไง
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
database db = new database();
string query = "SELECT * FROM product";
SqlCommand cmd = new SqlCommand(query, db.connect());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dataGridView1.Rows.Add(dr["product_id"], dr["product_name"], dr["product_amount"]);
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
dataGridView2.Rows.Add(row.Cells[0].Value.ToString() ,row.Cells[1].Value.ToString() ,row.Cells[2].Value.ToString() , label2.Text );
}
private void search_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
database db = new database();
string query = "SELECT * FROM employee where emp_id = '" + search.Text + "'";
SqlCommand cmd = new SqlCommand(query, db.connect());
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dataGridView3.Rows.Add(dr["emp_id"], dr["emp_name"]);
}
}
private void dataGridView3_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = this.dataGridView3.Rows[e.RowIndex];
label2.Text = row.Cells[1].Value.ToString();
}
private void save_Click(object sender, EventArgs e)
{
string query = "";
for (int i = 0; i < (dataGridView3.Rows.Count - 1); i++ )
{
query += "INSERT INTO sending (sending_product_id,sending_product_name,sending_product_amount,sending_id)";
query += " VALUES ('" + dataGridView3.Rows.Cells[0].Value.ToString() + "','" + dataGridView3.Rows.Cells[1].Value.ToString() + "','" + dataGridView3.Rows.Cells[2].Value.ToString() + "','" + dataGridView3.Rows.Cells[3].Value.ToString() + "');";
}
database db = new database();
SqlCommand cmd = new SqlCommand( query ,db.connect() );
if(cmd.ExecuteNonQuery() > 0){
MessageBox.Show("save");
}
}
}
}