Làm thế nào để giải quyết vấn đề sau : dùng BindingNavigator để thêm/xóa/sửa dữ liệu trên dataGridView khi bấm nút save thì dữ liệu có thay đổi trên dataGridView nhưng không thông báo số lượng records bị thay đổi và dữ liệu không cập nhật xuống database ????
Hướng giải quyết.
1. thiết kế giao diện
2. dùng bảng region trong northwind
3. coding
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
{
}
BindingSource bs= new BindingSource();
private void load_data()
{
SqlConnection con = new SqlConnection(“server=.;database=northwind;integrated security=true;”);
SqlDataAdapter da = new SqlDataAdapter(“select * from region”, con);
DataTable tb = new DataTable();
da.Fill(tb);
dataGridView1.DataSource = tb;
bs.DataSource = tb;
bindingNavigator1.BindingSource = bs;
}
private void Form1_Load(object sender, EventArgs e)
{
load_data();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“server=.;database=northwind;integrated security=true;”);
SqlCommand cmd = new SqlCommand(“insert into region values(‘” + dataGridView1.CurrentRow.Cells[0].Value.ToString() + “‘,'” +dataGridView1.CurrentRow.Cells[1].Value.ToString() + “‘)”, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
load_data();
}
private void button1_Click(object sender, EventArgs e)
{
load_data();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“server=.;database=northwind;integrated security=true;”);
SqlCommand cmd = new SqlCommand(“update region set regionDescription='” + dataGridView1.CurrentRow.Cells[1].Value.ToString() + “‘ where regionid='” + dataGridView1.CurrentRow.Cells[0].Value.ToString() + “‘”, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
load_data();
//String sql = “UPDATE [Tenbang] set [tentruong]='” + this.datagridView1.CurrentRow.Cells[0].Value.ToString()+”‘ where [tentruong]='” + this.datagridView1.CurrentRow.Cells[1].Value.ToString()+
}
}
II bài tương tự :
1. Thiết kế giao diện
2. database
USE master
go
drop database quanlyKH
go
create database quanlyKH
go
use quanlyKH
go
–create table bang
–(
–mabang char(2) primary key,
–tenbang varchar(50)
–)
–insert into bang values(‘AL’,’bang AL’)
–insert into bang values(‘CA’,’bang CA’)
–go
create table khachhang
(
maKH varchar(50) primary key,
tenKH varchar(50),
diachi varchar(50),
thanhpho varchar(50),
mabang char(2),
mavung varchar(50)
–constraint fk1 foreign key(mabang) references bang(mabang)
)
go
insert into khachhang values(‘001′,’teo’,’3 thang 2′,’hcm’,’AL’,’12345′)
insert into khachhang values(‘002′,’ti’,’hoa binh’,’hcm’,’CA’,’56789′)
go
–select * from bang
–go
select * from khachhang
go
3. coding form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
BindingSource bs = new BindingSource();
private void load_data()
{
SqlConnection con = new SqlConnection(“server=.;database=quanlyKH;integrated security=true;”);
SqlDataAdapter da = new SqlDataAdapter(“select * from khachhang”,con);
DataTable tb = new DataTable();
da.Fill(tb);
dataGridView1.DataSource = tb;
bs.DataSource = dataGridView1.DataSource;
bindingNavigator1.BindingSource = bs;
//binding
textBox1.DataBindings.Clear();
textBox3.DataBindings.Clear();
textBox1.DataBindings.Add(“Text”, dataGridView1.DataSource, “mabang”);
textBox3.DataBindings.Add(“Text”, dataGridView1.DataSource, “thanhpho”);
}
private void Form1_Load(object sender, EventArgs e)
{
load_data();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“server=.;database=quanlyKH;integrated security=true;”);
SqlDataAdapter da = new SqlDataAdapter(“select * from khachhang where thanhpho like ‘%”+textBox3.Text+”%'”, con);
DataTable tb = new DataTable();
da.Fill(tb);
dataGridView1.DataSource = tb;
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“server=.;database=quanlyKH;integrated security=true;”);
SqlCommand cmd = new SqlCommand(“insert into khachhang values(‘” + dataGridView1.CurrentRow.Cells[0].Value.ToString() + “‘,'” + dataGridView1.CurrentRow.Cells[1].Value.ToString() + “‘,'” + dataGridView1.CurrentRow.Cells[2].Value.ToString() + “‘,'” + dataGridView1.CurrentRow.Cells[3].Value.ToString() + “‘,'” + dataGridView1.CurrentRow.Cells[4].Value.ToString() + “‘,'” + dataGridView1.CurrentRow.Cells[5].Value.ToString() + “‘)”, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
load_data();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(“server=.;database=quanlyKH;integrated security=true;”);
SqlCommand cmd = new SqlCommand(“update khachhang set tenKH='” + dataGridView1.CurrentRow.Cells[1].Value.ToString() + “‘,diachi='” + dataGridView1.CurrentRow.Cells[2].Value.ToString() + “‘,thanhpho='” + dataGridView1.CurrentRow.Cells[3].Value.ToString() + “‘,mabang='” + dataGridView1.CurrentRow.Cells[4].Value.ToString() + “‘,mavung='” + dataGridView1.CurrentRow.Cells[5].Value.ToString() + “‘ where maKH='” + dataGridView1.CurrentRow.Cells[0].Value.ToString() + “‘”, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
load_data();
}
}