0% found this document useful (0 votes)
28 views46 pages

1

The document describes a Windows Forms application for managing student statistics. It allows the user to view, add, edit and delete records from a database table. Functions include loading data from the table, validating and saving form input, and searching the table by name, birthplace or other fields.

Uploaded by

tiendatpham1504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views46 pages

1

The document describes a Windows Forms application for managing student statistics. It allows the user to view, add, edit and delete records from a database table. Functions include loading data from the table, validating and saving form input, and searching the table by name, birthplace or other fields.

Uploaded by

tiendatpham1504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

De 1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace De_1
{
public partial class Form1 : Form
{
string ketnoi = @"Data Source=LAPTOP-H0KQF1IJ\SQLEXPRESS;Initial Catalog=QLNS;Integrated
Security=True";
SqlConnection con = null;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con = new SqlConnection(ketnoi);
con.Open();
string sql = "select * from ThongKe ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;

}
private void dgvthongke_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

private void btnthem_Click(object sender, EventArgs e)


{
string hovaten = txbhovaten.Text;
string gioitinh = "";
if (rbnam.Checked)
{
gioitinh = "Nam";
}
else if (rbnu.Checked)
{
gioitinh = "Nữ";
}
string quequan = txbquequan.Text;
string donvi = txbdonvi.Text;
string hamhoc = txbhamhoc.Text;
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "INSERT INTO ThongKe (HoVaTen, GioiTinh, QueQuan, DonVi, HamHoc) VALUES (@HoVaTen,
@GioiTinh, @QueQuan, @DonVi, @HamHoc)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@HoVaTen", hovaten);
command.Parameters.AddWithValue("@GioiTinh", gioitinh);
command.Parameters.AddWithValue("@QueQuan", quequan);
command.Parameters.AddWithValue("@DonVi", donvi);
command.Parameters.AddWithValue("@HamHoc", hamhoc);
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
string sql = "select * from ThongKe ";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Thêm dữ liệu không thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}

private void btnsua_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
string hovaten = txbhovaten.Text;
string gioitinh = "";
if (rbnam.Checked)
{
gioitinh = "Nam";
}
else if (rbnu.Checked)
{
gioitinh = "Nữ";
}
string quequan = txbquequan.Text;
string donvi = txbdonvi.Text;
string hamhoc = txbhamhoc.Text;
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "UPDATE ThongKe SET GioiTinh = @GioiTinh, QueQuan = @QueQuan, DonVi = @DonVi,
HamHoc = @HamHoc WHERE HoVaTen = @HoVaTen";

using (SqlCommand command = new SqlCommand(query, connection))


{
command.Parameters.AddWithValue("@HoVaTen", hovaten);
command.Parameters.AddWithValue("@GioiTinh", gioitinh);
command.Parameters.AddWithValue("@QueQuan", quequan);
command.Parameters.AddWithValue("@DonVi", donvi);
command.Parameters.AddWithValue("@HamHoc", hamhoc);
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Sửa dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
string sql = "SELECT * FROM ThongKe";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Sửa dữ liệu không thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một dòng để sửa!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}

private void btnxoa_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
string hovaten = txbhovaten.Text;
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "DELETE FROM ThongKe WHERE HoVaTen = @HoVaTen";

using (SqlCommand command = new SqlCommand(query, connection))


{
command.Parameters.AddWithValue("@HoVaTen", hovaten);
connection.Open();
int rowsAffected = command.ExecuteNonQuery();
if (rowsAffected > 0)
{
MessageBox.Show("Xóa dữ liệu thành công!","Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
string sql = "SELECT * FROM ThongKe";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Xóa dữ liệu không thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một hàng để xóa.", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}

private void dgvthongke_CellClick(object sender, DataGridViewCellEventArgs e)


{
txbhovaten.Text = dgvthongke[0, dgvthongke.CurrentRow.Index].Value.ToString();
txbquequan.Text = dgvthongke[2, dgvthongke.CurrentRow.Index].Value.ToString();
txbdonvi.Text = dgvthongke[3, dgvthongke.CurrentRow.Index].Value.ToString();
txbhamhoc.Text = dgvthongke[4, dgvthongke.CurrentRow.Index].Value.ToString();
if (rbnam.Checked)
{
dgvthongke[1, dgvthongke.CurrentRow.Index].Value = "Nam";
}
else if (rbnu.Checked)
{
dgvthongke[1, dgvthongke.CurrentRow.Index].Value = "Nữ";
}
}

private void btntimkiem_Click(object sender, EventArgs e)


{
TimKiem();
}
private void TimKiem()
{
string hovaten = txbhovaten.Text;
string quequan = txbquequan.Text;
string donvi = txbdonvi.Text;
string hamhoc = txbhamhoc.Text;
string query = "select * from ThongKe where hovaten LIKE @HoVaTen and quequan LIKE @QueQuan and
donvi like @DonVi and hamhoc like @HamHoc";
using (SqlConnection connection = new SqlConnection(ketnoi))
{
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@HoVaTen", "%" + hovaten + "%");
command.Parameters.AddWithValue("@QueQuan", "%" + quequan + "%");
command.Parameters.AddWithValue("@DonVi", "%" + donvi + "%");
command.Parameters.AddWithValue("@HamHoc", "%" + hamhoc + "%");
connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count > 0)
{
MessageBox.Show("Tìm thấy thông tin.", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Không tìm thấy thông tin.", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
dgvthongke.DataSource = dt;
}
}
}

private void button1_Click(object sender, EventArgs e)


{
ClearInputFields();

}
private void ClearInputFields()
{
txbhovaten.Text = "";
txbquequan.Text = "";
txbdonvi.Text = "";
txbhamhoc.Text = "";
rbnam.Checked = false;
rbnu.Checked = false;
}

private void btnthoat_Click(object sender, EventArgs e)


{
this.Close();
}
}
}
De 3

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace de_3
{
public partial class Form1 : Form
{
string ketnoi = @"Data Source=LAPTOP-H0KQF1IJ\SQLEXPRESS;Initial
Catalog=QLSV;Integrated Security=True";
SqlConnection con = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
con = new SqlConnection(ketnoi);
con.Open();
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}

private void dgvthongke_CellClick(object sender, DataGridViewCellEventArgs e)


{
txbhovaten.Text = dgvthongke[1, dgvthongke.CurrentRow.Index].Value.ToString();
txbmasv.Text = dgvthongke[0, dgvthongke.CurrentRow.Index].Value.ToString();
dtpngaysinh.Text = dgvthongke[2, dgvthongke.CurrentRow.Index].Value.ToString();
txbnoisinh.Text = dgvthongke[3, dgvthongke.CurrentRow.Index].Value.ToString();
if (rbnam.Checked)
{
dgvthongke[4, dgvthongke.CurrentRow.Index].Value = "Nam";
}
else if (rbnu.Checked)
{
dgvthongke[4, dgvthongke.CurrentRow.Index].Value = "Nữ";
}

}
private void btnthem_Click(object sender, EventArgs e)
{
string masinhvien = txbmasv.Text;
string hovaten = txbhovaten.Text;
string ngaysinh = dtpngaysinh.Text;
string noisinh = txbnoisinh.Text;
string gioitinh = "";
if (rbnam.Checked)
{
gioitinh = "Nam";
}
else if (rbnu.Checked)
{
gioitinh = "Nữ";
}
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "insert into thongke (masv,tensv,ngaysinh,noisinh,gioitinh) values
(@masv,@tensv,@ngaysinh,@noisinh,@gioitinh)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@masv", masinhvien);
command.Parameters.AddWithValue("@tensv", hovaten);
command.Parameters.AddWithValue("@ngaysinh", ngaysinh);
command.Parameters.AddWithValue("@noisinh", noisinh);
command.Parameters.AddWithValue("@gioitinh", gioitinh);
connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Thêm dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

private void btnsua_Click(object sender, EventArgs e)


{
if(dgvthongke.SelectedRows.Count > 0)
{
string hovaten = txbhovaten.Text;
string masinhvien = txbmasv.Text;
string ngaysinh = dtpngaysinh.Text;
string noisinh = txbnoisinh.Text;
string gioitinh = "";
if (rbnam.Checked)
{
gioitinh = "Nam";
}
else if (rbnu.Checked)
{
gioitinh = "Nữ";
}
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "update thongke set tensv = @tensv, ngaysinh = @ngaysinh, noisinh
= @noisinh, gioitinh = @gioitinh where masv = @masv";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@masv", masinhvien);
command.Parameters.AddWithValue("@tensv", hovaten);
command.Parameters.AddWithValue("@ngaysinh", ngaysinh);
command.Parameters.AddWithValue("@noisinh", noisinh);
command.Parameters.AddWithValue("@gioitinh", gioitinh);
connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Sửa dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Sửa dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

}
}
else
{
MessageBox.Show("Vui lòng chọn một dòng để sửa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btnxoa_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
string masinhvien = txbmasv.Text;
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "Delete from thongke where masv = @masv";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@masv", masinhvien);
connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Xóa dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Xóa dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một hàng để xóa.", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btntimkiem_Click(object sender, EventArgs e)


{
TimKiem();
}
private void TimKiem()
{
string masv = txbmasv.Text;
string hovaten = txbhovaten.Text;
string noisinh = txbnoisinh.Text;
string gioitinh = "";

if (rbnam.Checked)
{
gioitinh = "Nam";
}
else if (rbnu.Checked)
{
gioitinh = "Nữ";
}

// Lấy ngày sinh từ DateTimePicker


string ngaysinh = dtpngaysinh.Value.ToString("yyyy-MM-dd");

// Tạo câu truy vấn SQL tương ứng với thông tin tìm kiếm
string query = "SELECT * FROM thongke WHERE masv LIKE @masv AND tensv
LIKE @tensv AND noisinh LIKE @noisinh";

// Thêm điều kiện tìm kiếm theo giới tính nếu giới tính được chọn
if (!string.IsNullOrEmpty(gioitinh))
{
query += " AND gioitinh = @gioitinh";
}
// Thêm điều kiện tìm kiếm theo ngày sinh nếu có
if (!string.IsNullOrEmpty(ngaysinh))
{
query += " AND ngaysinh = @ngaysinh";
}

using (SqlConnection connection = new SqlConnection(ketnoi))


{
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@masv", "%" + masv + "%");
command.Parameters.AddWithValue("@tensv", "%" + hovaten + "%");
command.Parameters.AddWithValue("@noisinh", "%" + noisinh + "%");

// Thêm tham số cho giới tính nếu có


if (!string.IsNullOrEmpty(gioitinh))
{
command.Parameters.AddWithValue("@gioitinh", gioitinh);
}

// Thêm tham số cho ngày sinh nếu có


if (!string.IsNullOrEmpty(ngaysinh))
{
command.Parameters.AddWithValue("@ngaysinh", ngaysinh);
}

connection.Open();
SqlDataAdapter da = new SqlDataAdapter(command);
DataTable dt = new DataTable();
da.Fill(dt);

if (dt.Rows.Count == 0)
{
MessageBox.Show("Không tìm thấy thông tin.", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}

dgvthongke.DataSource = dt;
}
}
}

private void btnthoat_Click(object sender, EventArgs e)


{
Application.Exit();
}
}
}

De 4
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace de_4_quan_ly_diem_thi
{
public partial class Form1 : Form
{
string ketnoi = @"Data Source=LAPTOP-H0KQF1IJ\SQLEXPRESS;Initial
Catalog=QLDT;Integrated Security=True";
SqlConnection con = null;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con = new SqlConnection(ketnoi);
con.Open();
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
// Gán dữ liệu vào ComboBox
cbphanloai.Items.Add("Giỏi");
cbphanloai.Items.Add("Khá");
cbphanloai.Items.Add("Trung bình");

// Đặt giá trị mặc định cho ComboBox


cbphanloai.SelectedIndex = 0;

private void dgvthongke_CellClick(object sender, DataGridViewCellEventArgs e)


{
txbmasv.Text = dgvthongke[0, dgvthongke.CurrentRow.Index].Value.ToString();
txbtensv.Text = dgvthongke[1, dgvthongke.CurrentRow.Index].Value.ToString();
txbdiemtoan.Text = dgvthongke[2,
dgvthongke.CurrentRow.Index].Value.ToString();
txbdiemvan.Text = dgvthongke[3,
dgvthongke.CurrentRow.Index].Value.ToString();
txbdiemngoaingu.Text = dgvthongke[4,
dgvthongke.CurrentRow.Index].Value.ToString();
txbdiemtb.Text = dgvthongke[5, dgvthongke.CurrentRow.Index].Value.ToString();
}

private void btnthem_Click(object sender, EventArgs e)


{
// Lấy dữ liệu từ các ô nhập trên giao diện
string masv = txbmasv.Text;
string tensv = txbtensv.Text;
float diemtoan, diemvan, diemngoaingu, diemtrungbinh;

// Kiểm tra và chuyển đổi điểm thành kiểu float


if (!float.TryParse(txbdiemtoan.Text, out diemtoan) || diemtoan < 0 || diemtoan >
10)
{
MessageBox.Show("Nhập sai điểm Toán. Điểm phải nằm trong khoảng từ 0 đến
10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

if (!float.TryParse(txbdiemvan.Text, out diemvan) || diemvan < 0 || diemvan > 10)


{
MessageBox.Show("Nhập sai điểm Văn. Điểm phải nằm trong khoảng từ 0 đến
10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

if (!float.TryParse(txbdiemngoaingu.Text, out diemngoaingu) || diemngoaingu < 0 ||


diemngoaingu > 10)
{
MessageBox.Show("Nhập sai điểm Ngoại ngữ. Điểm phải nằm trong khoảng từ
0 đến 10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Tính điểm trung bình


diemtrungbinh = (diemtoan + diemvan + diemngoaingu) / 3;
// Nếu điểm trung bình không hợp lệ, hiển thị thông báo lỗi
if (diemtrungbinh < 0 || diemtrungbinh > 10)
{
MessageBox.Show("Nhập sai điểm trung bình. Điểm phải nằm trong khoảng từ
0 đến 10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Thêm dữ liệu vào cơ sở dữ liệu


using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "insert into thongke
(masv,tensv,diemtoan,diemvan,diemngoaingu,diemtrungbinh) values
(@masv,@tensv,@diemtoan,@diemvan,@diemngoaingu,@diemtrungbinh)";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@masv", masv);
command.Parameters.AddWithValue("@tensv", tensv);
command.Parameters.AddWithValue("@diemtoan", diemtoan);
command.Parameters.AddWithValue("@diemvan", diemvan);
command.Parameters.AddWithValue("@diemngoaingu", diemngoaingu);
command.Parameters.AddWithValue("@diemtrungbinh", diemtrungbinh);
connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Load lại dữ liệu trên DataGridView
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Thêm dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}

private void btnsua_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dgvthongke.SelectedRows[0];

string masv = txbmasv.Text;


string tensv = txbtensv.Text;
float diemtoan, diemvan, diemngoaingu;

if (!float.TryParse(txbdiemtoan.Text, out diemtoan) || diemtoan < 0 || diemtoan >


10)
{
MessageBox.Show("Nhập sai điểm Toán. Điểm phải nằm trong khoảng từ 0
đến 10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

if (!float.TryParse(txbdiemvan.Text, out diemvan) || diemvan < 0 || diemvan >


10)
{
MessageBox.Show("Nhập sai điểm Văn. Điểm phải nằm trong khoảng từ 0
đến 10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

if (!float.TryParse(txbdiemngoaingu.Text, out diemngoaingu) || diemngoaingu <


0 || diemngoaingu > 10)
{
MessageBox.Show("Nhập sai điểm Ngoại ngữ. Điểm phải nằm trong khoảng
từ 0 đến 10.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

// Kiểm tra trùng masv


using (SqlConnection connection = new SqlConnection(ketnoi))
{
string queryCheckDuplicate = "SELECT COUNT(*) FROM thongke WHERE
masv = @masv AND masv != @old_masv";
using (SqlCommand commandCheckDuplicate = new
SqlCommand(queryCheckDuplicate, connection))
{
commandCheckDuplicate.Parameters.AddWithValue("@masv", masv);
commandCheckDuplicate.Parameters.AddWithValue("@old_masv",
selectedRow.Cells[0].Value.ToString());

connection.Open();
int count = (int)commandCheckDuplicate.ExecuteScalar();
if (count > 0)
{
MessageBox.Show("Mã sinh viên đã tồn tại!", "Lỗi",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}

// Tính điểm trung bình


float diemtrungbinh = (diemtoan + diemvan + diemngoaingu) / 3;

// Tiến hành cập nhật dữ liệu


using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query;
if (masv != selectedRow.Cells[0].Value.ToString())
{
query = "UPDATE thongke SET masv = @masv, tensv = @tensv, diemtoan
= @diemtoan, diemvan = @diemvan, diemngoaingu = @diemngoaingu, diemtrungbinh =
@diemtrungbinh WHERE masv = @old_masv";
}
else
{
query = "UPDATE thongke SET tensv = @tensv, diemtoan = @diemtoan,
diemvan = @diemvan, diemngoaingu = @diemngoaingu, diemtrungbinh = @diemtrungbinh
WHERE masv = @masv";
}

using (SqlCommand command = new SqlCommand(query, connection))


{
command.Parameters.AddWithValue("@masv", masv);
command.Parameters.AddWithValue("@tensv", tensv);
command.Parameters.AddWithValue("@diemtoan", diemtoan);
command.Parameters.AddWithValue("@diemvan", diemvan);
command.Parameters.AddWithValue("@diemngoaingu", diemngoaingu);
command.Parameters.AddWithValue("@diemtrungbinh", diemtrungbinh);
command.Parameters.AddWithValue("@old_masv",
selectedRow.Cells[0].Value.ToString());

connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Cập nhật dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Load lại dữ liệu trên DataGridView
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Cập nhật dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một dòng để sửa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btnxoa_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa dữ liệu
này?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "DELETE FROM thongke WHERE masv = @masv";
using (SqlCommand command = new SqlCommand(query, connection))
{
DataGridViewRow selectedRow = dgvthongke.SelectedRows[0];
string masv = selectedRow.Cells[0].Value.ToString();
command.Parameters.AddWithValue("@masv", masv);

connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Xóa dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Load lại dữ liệu trên DataGridView
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Xóa dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một dòng để xóa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btnthongke_Click(object sender, EventArgs e)


{
// Xác định loại được chọn từ ComboBox cbphanloai
string loai = cbphanloai.SelectedItem.ToString();

// Tạo điều kiện để lọc dữ liệu từ DataGridView


string filterExpression = "";
switch (loai)
{
case "Giỏi":
filterExpression = "diemtrungbinh >= 8";
break;
case "Khá":
filterExpression = "diemtrungbinh < 8 AND diemtrungbinh >= 6.5";
break;
case "Trung bình":
filterExpression = "diemtrungbinh < 6.5";
break;
}

// Áp dụng bộ lọc vào DataView của DataTable hiện tại của DataGridView
((DataTable)dgvthongke.DataSource).DefaultView.RowFilter = filterExpression;
}
}
}
De chon ve xem phim

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace quan_ly_ban_ve_xem_phim
{
public partial class Form1 : Form
{
Dictionary<Button, int> seatPrices = new Dictionary<Button, int>();
int totalPayment = 0;
public Form1()
{
InitializeComponent();
InitializeSeatPrices();
InitializeSeatButtons();
cbChonPhim.SelectedIndexChanged += cbChonPhim_SelectedIndexChanged;
}
private void InitializeSeatPrices()
{
// Thiết lập giá vé cho từng ghế
seatPrices.Add(button1, 25000);
seatPrices.Add(button2, 25000);
seatPrices.Add(button3, 25000);
seatPrices.Add(button4, 25000);
seatPrices.Add(button5, 30000);
seatPrices.Add(button6, 30000);
seatPrices.Add(button7, 30000);
seatPrices.Add(button8, 30000);
seatPrices.Add(button9, 35000);
seatPrices.Add(button10, 35000);
seatPrices.Add(button11, 35000);
seatPrices.Add(button12, 35000);
seatPrices.Add(button13, 40000);
seatPrices.Add(button14, 40000);
seatPrices.Add(button15, 40000);
seatPrices.Add(button16, 40000);
}
private void InitializeSeatButtons()
{
// Gắn sự kiện click cho từng ghế
foreach (Button button in seatPrices.Keys)
{
button.Click += SeatButton_Click;
}
}
private void SeatButton_Click(object sender, EventArgs e)
{
Button seatButton = (Button)sender;

// Kiểm tra xem ghế đã được chọn hay chưa


if (seatButton.BackColor == Color.Green)
{
// Nếu đã chọn rồi thì hủy chọn ghế
seatButton.BackColor = DefaultBackColor;
totalPayment -= seatPrices[seatButton];
}
else
{
// Nếu chưa chọn thì chọn ghế
seatButton.BackColor = Color.Green;
totalPayment += seatPrices[seatButton];
}

// Cập nhật tổng tiền


lblthanhtien.Text = "Thành tiền: " + totalPayment.ToString() + " đồng";
}

private void Form1_Load(object sender, EventArgs e)


{
// Gán dữ liệu cho combobox cbChonPhim
cbChonPhim.Items.Add("Phim A");
cbChonPhim.Items.Add("Phim B");
cbChonPhim.Items.Add("Phim C");

// Chọn phim mặc định là phim đầu tiên


cbChonPhim.SelectedIndex = 0;
// Gọi sự kiện để hiển thị thông điệp mặc định khi form được load
cbChonPhim_SelectedIndexChanged(null, null);
}

private void btnthanhtoan_Click(object sender, EventArgs e)


{
DialogResult result = MessageBox.Show("Bạn có muốn thanh toán không?", "Xác nhận thanh toán",
MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{
// Đồng ý thanh toán
foreach (Button button in seatPrices.Keys)
{
if (button.BackColor == Color.Green)
{
// Đổi màu thành đỏ và không cho phép chọn nữa
button.BackColor = Color.Red;
button.Enabled = false;
}
}
// Reset tổng tiền
totalPayment = 0;
lblthanhtien.Text = "Thành tiền: " + totalPayment.ToString() + " đồng";
MessageBox.Show("Thanh toán thành công!");
}

}
private void cbChonPhim_SelectedIndexChanged(object sender, EventArgs e)
{
// Lấy tên phim từ combobox cbChonPhim
string selectedMovie = cbChonPhim.SelectedItem.ToString();

// Hiển thị thông điệp dưới ô txbketqua


txbketqua.Text = "Mời bạn chọn số ghế của phim: " + selectedMovie;
}

}
}

QlMayTinh

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ban_may_tinh
{
public partial class Form1 : Form
{
string ketnoi = @"Data Source=LAPTOP-H0KQF1IJ\SQLEXPRESS;Initial
Catalog=QLMAYTINH;Integrated Security=True";
SqlConnection con = null;
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con = new SqlConnection(ketnoi);
con.Open();
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
foreach (DataGridViewColumn column in dgvthongke.Columns)
{
column.ReadOnly = true;
}

txbdongia.TextChanged += txbdongia_TextChanged;
nudsoluong.ValueChanged += nudsoluong_ValueChanged;
}

private void dgvthongke_CellClick(object sender, DataGridViewCellEventArgs e)


{
txbmahd.Text = dgvthongke[0, dgvthongke.CurrentRow.Index].Value.ToString();
txbtenkh.Text = dgvthongke[1, dgvthongke.CurrentRow.Index].Value.ToString();
dtpngayban.Text = dgvthongke[2,
dgvthongke.CurrentRow.Index].Value.ToString();
if (rbdeban.Checked)
{
dgvthongke[3, dgvthongke.CurrentRow.Index].Value = "De ban";
}
else if (rbxachtay.Checked)
{
dgvthongke[3, dgvthongke.CurrentRow.Index].Value = "Xach tay";
}
txbtenmay.Text = dgvthongke[4, dgvthongke.CurrentRow.Index].Value.ToString();
txbdongia.Text = dgvthongke[5, dgvthongke.CurrentRow.Index].Value.ToString();
nudsoluong.Text = dgvthongke[6,
dgvthongke.CurrentRow.Index].Value.ToString();
txbthanhtien.Text = dgvthongke[7,
dgvthongke.CurrentRow.Index].Value.ToString();
}

private bool KiemTraTrungMaHD(string mahd)


{
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "SELECT COUNT(*) FROM thongke WHERE mahd = @mahd";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@mahd", mahd);
connection.Open();
int count = (int)command.ExecuteScalar();
return count > 0; // Trả về true nếu có ít nhất một bản ghi có mã HD tương tự
}
}
}
private void btnthem_Click(object sender, EventArgs e)
{

string mahd = txbmahd.Text;// Kiểm tra nếu mã HD đã tồn tại


if (KiemTraTrungMaHD(mahd))
{
MessageBox.Show("Mã hóa đơn đã tồn tại!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; // Thoát khỏi phương thức để ngăn việc thêm bản ghi trùng lặp
}
string tenkh = txbtenkh.Text;
string ngayban = dtpngayban.Text;
string loaimay = "";
if (rbdeban.Checked)
{
dgvthongke[3, dgvthongke.CurrentRow.Index].Value = "De ban";
}
else if (rbxachtay.Checked)
{
dgvthongke[3, dgvthongke.CurrentRow.Index].Value = "Xach tay";
}
string tenmay = txbtenmay.Text;
string dongia = txbdongia.Text;
string soluong = nudsoluong.Text;
string thanhtien = txbthanhtien.Text;
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "insert into
thongke(mahd,tenkh,ngayban,loaimay,tenmay,dongia,soluong,thanhtien)
values(@mahd,@tenkh,@ngayban,@loaimay,@tenmay,@dongia,@soluong,@thanhtien)";
using(SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@mahd", mahd);
command.Parameters.AddWithValue("@tenkh", tenkh);
command.Parameters.AddWithValue("@ngayban", ngayban);
command.Parameters.AddWithValue("@loaimay", loaimay);
command.Parameters.AddWithValue("@tenmay", tenmay);
command.Parameters.AddWithValue("@dongia", dongia);
command.Parameters.AddWithValue("@soluong", soluong);
command.Parameters.AddWithValue("@thanhtien", thanhtien);
connection.Open();
int sodong = command.ExecuteNonQuery();
if(sodong > 0)
{
MessageBox.Show("Thêm dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Thêm dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
private void TinhThanhTien()
{
if (!string.IsNullOrEmpty(txbdongia.Text) && !
string.IsNullOrEmpty(nudsoluong.Text))
{
double dongia = double.Parse(txbdongia.Text);
int soluong = (int)nudsoluong.Value;
double thanhtien = dongia * soluong;
txbthanhtien.Text = thanhtien.ToString();
}
}
private void txbdongia_TextChanged(object sender, EventArgs e)
{
TinhThanhTien();
}

private void nudsoluong_ValueChanged(object sender, EventArgs e)


{
TinhThanhTien();
}

private void btnsua_Click(object sender, EventArgs e)


{
// Lấy dữ liệu từ các ô nhập liệu
string mahd = txbmahd.Text;
string tenkh = txbtenkh.Text;
string loaimay = rbdeban.Checked ? "De ban" : "Xach tay";
string tenmay = txbtenmay.Text;
string dongia = txbdongia.Text;
string soluong = nudsoluong.Text;
string thanhtien = txbthanhtien.Text;

// Kiểm tra xem người dùng đã chọn hàng nào trong DataGridView chưa
if (dgvthongke.SelectedRows.Count > 0)
{
// Lấy chỉ số của hàng được chọn
int rowIndex = dgvthongke.SelectedRows[0].Index;

// Lấy mã hóa đơn của hàng được chọn trong DataGridView


string selectedMaHD = dgvthongke.Rows[rowIndex].Cells[0].Value.ToString();

// Kiểm tra xem mã hóa đơn đã được chọn có trùng với mã hóa đơn nhập vào
không
if (mahd != selectedMaHD)
{
MessageBox.Show("Không thể thay đổi mã hóa đơn!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}

// Tiến hành cập nhật dữ liệu vào cơ sở dữ liệu


using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "UPDATE thongke SET tenkh = @tenkh, loaimay = @loaimay,
tenmay = @tenmay, dongia = @dongia, soluong = @soluong, thanhtien = @thanhtien WHERE
mahd = @mahd";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@mahd", mahd);
command.Parameters.AddWithValue("@tenkh", tenkh);
command.Parameters.AddWithValue("@loaimay", loaimay);
command.Parameters.AddWithValue("@tenmay", tenmay);
command.Parameters.AddWithValue("@dongia", dongia);
command.Parameters.AddWithValue("@soluong", soluong);
command.Parameters.AddWithValue("@thanhtien", thanhtien);
connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Cập nhật dữ liệu thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Cập nhật lại DataGridView sau khi cập nhật dữ liệu thành công
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Cập nhật dữ liệu không thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một hàng để sửa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void btnxoa_Click(object sender, EventArgs e)


{
// Kiểm tra xem người dùng đã chọn hàng nào trong DataGridView chưa
if (dgvthongke.SelectedRows.Count > 0)
{
// Lấy chỉ số của hàng được chọn
int rowIndex = dgvthongke.SelectedRows[0].Index;

// Lấy mã hóa đơn của hàng được chọn trong DataGridView


string mahd = dgvthongke.Rows[rowIndex].Cells[0].Value.ToString();

// Xác nhận xóa bản ghi


DialogResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa bản ghi
có mã hóa đơn " + mahd + " không?", "Xác nhận xóa", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
// Tiến hành xóa bản ghi khỏi cơ sở dữ liệu
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "DELETE FROM thongke WHERE mahd = @mahd";
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Parameters.AddWithValue("@mahd", mahd);
connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Xóa bản ghi thành công!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
// Cập nhật lại DataGridView sau khi xóa bản ghi thành công
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Không có bản ghi nào được xóa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một hàng để xóa!", "Thông báo",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}

private void btnthoat_Click(object sender, EventArgs e)


{
this.Close();
}
}
}

Muontruyen

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace cho_thue_truyen
{
public partial class Form1 : Form
{
string ketnoi = @"Data Source=LAPTOP-H0KQF1IJ\SQLEXPRESS;Initial Catalog=thuetruyen1;Integrated
Security=True";
SqlConnection con = null;
Dictionary<string, decimal> giaTriTruyen = new Dictionary<string, decimal>()
{
{ "manga", 50000 },
{ "truyen hai", 100000 },
{ "truyen ma", 150000 }
};
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
con = new SqlConnection(ketnoi);
con.Open();
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataGridViewColumn column in dgvthongke.Columns)
{
column.ReadOnly = true;
}

dgvthongke.DataSource = dt;

cbtentruyen.DataSource = new BindingSource(giaTriTruyen, null);


cbtentruyen.DisplayMember = "Key";
cbtentruyen.ValueMember = "Value";
cbtentruyen.SelectedIndexChanged += cbtentruyen_SelectedIndexChanged;
}

private void dgvthongke_CellClick(object sender, DataGridViewCellEventArgs e)


{
txbtenkh.Text = dgvthongke[1, dgvthongke.CurrentRow.Index].Value.ToString();
txbsdt.Text = dgvthongke[2, dgvthongke.CurrentRow.Index].Value.ToString();
cbtentruyen.Text = dgvthongke[3, dgvthongke.CurrentRow.Index].Value.ToString();
dtpngaymuon.Text = dgvthongke[4, dgvthongke.CurrentRow.Index].Value.ToString();
dtpngaytra.Text = dgvthongke[5, dgvthongke.CurrentRow.Index].Value.ToString();
txbthanhtien.Text = dgvthongke[6, dgvthongke.CurrentRow.Index].Value.ToString();

}
private void cbtentruyen_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbtentruyen.SelectedItem != null)
{
KeyValuePair<string, decimal> selectedItem = (KeyValuePair<string, decimal>)cbtentruyen.SelectedItem;
txbdongia.Text = selectedItem.Value.ToString();
}
}

private void btnmuontruyen_Click(object sender, EventArgs e)


{
string tenkhachhang = txbtenkh.Text;
string sdt = txbsdt.Text;
string tentruyen = cbtentruyen.Text;
DateTime ngaymuon = dtpngaymuon.Value;
string thanhtien = txbthanhtien.Text;

// 1. Kết nối cơ sở dữ liệu


using (SqlConnection connection = new SqlConnection(ketnoi))
{
connection.Open();

// 2. Thực hiện chèn dữ liệu vào cơ sở dữ liệu


string insertQuery = "INSERT INTO thongke (tenkh, sdt, tentruyen, ngaymuon, thanhtien, ghichu) VALUES
(@tenkh, @sdt, @tentruyen, @ngaymuon, @thanhtien, @ghichu)";
using (SqlCommand command = new SqlCommand(insertQuery, connection))
{
command.Parameters.AddWithValue("@tenkh", tenkhachhang);
command.Parameters.AddWithValue("@sdt", sdt);
command.Parameters.AddWithValue("@tentruyen", tentruyen);
command.Parameters.AddWithValue("@ngaymuon", ngaymuon);
command.Parameters.AddWithValue("@thanhtien", thanhtien);
command.Parameters.AddWithValue("@ghichu", "chưa trả");
command.ExecuteNonQuery();
}

// 3. Lấy dữ liệu từ cơ sở dữ liệu và hiển thị trên DataGridView


string selectQuery = "SELECT tenkh, sdt, tentruyen, ngaymuon, '', thanhtien, ghichu FROM thongke";
SqlDataAdapter dataAdapter = new SqlDataAdapter(selectQuery, connection);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);

dgvthongke.DataSource = dataTable;
Form1_Load(sender, e);
}
}

private void btnxoa_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
DialogResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa dữ liệu này?", "Xác nhận xóa",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
using (SqlConnection connection = new SqlConnection(ketnoi))
{
string query = "DELETE FROM thongke WHERE tenkh = @tenkh";
using (SqlCommand command = new SqlCommand(query, connection))
{
DataGridViewRow selectedRow = dgvthongke.SelectedRows[0];
string tenkh = selectedRow.Cells[1].Value.ToString();
command.Parameters.AddWithValue("@tenkh", tenkh);

connection.Open();
int sodong = command.ExecuteNonQuery();
if (sodong > 0)
{
MessageBox.Show("Xóa dữ liệu thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
// Load lại dữ liệu trên DataGridView
string sql = "SELECT * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(sql, connection);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
else
{
MessageBox.Show("Xóa dữ liệu không thành công!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một dòng để xóa!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}

private void btntratruyen_Click(object sender, EventArgs e)


{
if (dgvthongke.SelectedRows.Count > 0)
{
DataGridViewRow selectedRow = dgvthongke.SelectedRows[0];

// Lấy dữ liệu từ DataGridView


string tenkhachhang = selectedRow.Cells[1].Value.ToString();
string sdt = selectedRow.Cells[2].Value.ToString();
string tentruyen = selectedRow.Cells[3].Value.ToString();
DateTime ngaymuon = Convert.ToDateTime(selectedRow.Cells[4].Value);
DateTime ngaytra = dtpngaytra.Value;

// Tính số ngày mượn


int soNgayMuon = (int)(ngaytra - ngaymuon).TotalDays;

// Tính thành tiền


decimal dongia = giaTriTruyen[tentruyen];
decimal thanhtien = soNgayMuon * dongia;

// Cập nhật giá trị vào DataGridView


selectedRow.Cells[5].Value = ngaytra;
selectedRow.Cells[6].Value = thanhtien;
selectedRow.Cells[7].Value = "đã trả"; // Cột ghi chú

// Cập nhật vào cơ sở dữ liệu


string updateQuery = "UPDATE thongke SET ngaytra = @ngaytra, thanhtien = @thanhtien, ghichu = @ghichu
WHERE tenkh = @tenkh AND tentruyen = @tentruyen";
using (SqlConnection connection = new SqlConnection(ketnoi))
{
connection.Open();
using (SqlCommand command = new SqlCommand(updateQuery, connection))
{
command.Parameters.AddWithValue("@ngaytra", ngaytra);
command.Parameters.AddWithValue("@thanhtien", thanhtien);
command.Parameters.AddWithValue("@ghichu", "đã trả");
command.Parameters.AddWithValue("@tenkh", tenkhachhang);
command.Parameters.AddWithValue("@tentruyen", tentruyen);
command.ExecuteNonQuery();
}
}
}
else
{
MessageBox.Show("Vui lòng chọn một dòng để trả truyện!", "Thông báo", MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
Form1_Load(sender, e);
}
}
}

QL hoa qua
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Quan_ly_ban_hoa_qua
{
public partial class Form1 : Form
{
string ketnoi = @"Data Source=LAPTOP-H0KQF1IJ\SQLEXPRESS;Initial Catalog=QLHOAQUA;Integrated
Security=True";
SqlConnection con = null;

public Form1()
{
InitializeComponent();
cbloaihoaqua.SelectedIndexChanged += cbloaihoaqua_SelectedIndexChanged;
nudsoluong.ValueChanged += nudsoluong_ValueChanged;
txbsotienkhachdua.TextChanged += txbsotienkhachdua_TextChanged;
btnthem.Click += btnthem_Click;
}

private void Form1_Load(object sender, EventArgs e)


{
con = new SqlConnection(ketnoi);
con.Open();
string sql = "select * from thongke ";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}
private void dgvthongke_CellClick(object sender, DataGridViewCellEventArgs e)
{
cbloaihoaqua.Text = dgvthongke[1, dgvthongke.CurrentRow.Index].Value.ToString();
txbdongia.Text = dgvthongke[2, dgvthongke.CurrentRow.Index].Value.ToString();
nudsoluong.Text = dgvthongke[3, dgvthongke.CurrentRow.Index].Value.ToString();
txbtongtienphaitra.Text = dgvthongke[4, dgvthongke.CurrentRow.Index].Value.ToString();
}

private void btnthem_Click(object sender, EventArgs e)


{
// Lấy thông tin từ các trường dữ liệu
string loaiHoaQua = cbloaihoaqua.Text;
float donGia = float.Parse(txbdongia.Text);
int soLuong = (int)nudsoluong.Value;
float thanhTien = float.Parse(txbtongtienphaitra.Text);

// Thêm thông tin vào cơ sở dữ liệu


string query = "INSERT INTO thongke (tensp, dongia, soluong, thanhtien) VALUES (@TenSP, @DonGia,
@SoLuong, @ThanhTien); SELECT SCOPE_IDENTITY()";
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.Parameters.AddWithValue("@TenSP", loaiHoaQua);
cmd.Parameters.AddWithValue("@DonGia", donGia);
cmd.Parameters.AddWithValue("@SoLuong", soLuong);
cmd.Parameters.AddWithValue("@ThanhTien", thanhTien);

// Lấy số thứ tự (STT) của hóa đơn vừa được thêm


int stt = Convert.ToInt32(cmd.ExecuteScalar());
txbstt.Text = stt.ToString(); // Hiển thị số thứ tự trong TextBox
}

// Cập nhật lại DataGridView


UpdateDataGridView();

// Thông báo thành công


MessageBox.Show("Thêm thông tin thành công!");
}
private void UpdateDataGridView()
{
string selectQuery = "SELECT ROW_NUMBER() OVER(ORDER BY Stt) AS STT, * FROM thongke";
SqlDataAdapter da = new SqlDataAdapter(selectQuery, con);
DataTable dt = new DataTable();
da.Fill(dt);
dgvthongke.DataSource = dt;
}

private void cbloaihoaqua_SelectedIndexChanged(object sender, EventArgs e)


{
// Lấy tên loại hoa quả được chọn từ combobox
string loaiHoaQua = cbloaihoaqua.SelectedItem.ToString();

// Tạo một dictionary để ánh xạ giữa tên hoa quả và giá


Dictionary<string, int> giaHoaQua = new Dictionary<string, int>()
{
{ "xoài", 10000 },
{ "chuối", 20000 },
{ "nho", 30000 },
{ "thanh long", 40000 },
{ "ổi", 50000 }
};

// Nếu loại hoa quả tồn tại trong danh sách giá, hiển thị giá tương ứng
if (giaHoaQua.ContainsKey(loaiHoaQua))
{
txbdongia.Text = giaHoaQua[loaiHoaQua].ToString();
}
else
{
txbdongia.Text = "0"; // Hoặc một giá trị mặc định khác bạn chọn
}
}
private void nudsoluong_ValueChanged(object sender, EventArgs e)
{
// Kiểm tra xem đã chọn loại hoa quả chưa
if (cbloaihoaqua.SelectedItem != null)
{
// Lấy đơn giá từ txbdongia và chuyển đổi thành kiểu số
if (int.TryParse(txbdongia.Text, out int donGia))
{
// Lấy số lượng từ NumericUpDown
int soLuong = (int)nudsoluong.Value;

// Tính tổng số tiền phải trả và gán vào txbtongtienphaitra


int tongTien = donGia * soLuong;
txbtongtienphaitra.Text = tongTien.ToString();
}
else
{
// Xử lý trường hợp không thể chuyển đổi đơn giá sang kiểu số
MessageBox.Show("Đơn giá không hợp lệ.");
}
}
else
{
// Xử lý trường hợp chưa chọn loại hoa quả
MessageBox.Show("Vui lòng chọn loại hoa quả trước.");
}
}
private void txbsotienkhachdua_TextChanged(object sender, EventArgs e)
{
// Kiểm tra xem đã nhập số tiền khách đưa chưa
if (!string.IsNullOrWhiteSpace(txbsotienkhachdua.Text))
{
// Lấy số tiền khách đưa và chuyển đổi thành kiểu số
if (int.TryParse(txbsotienkhachdua.Text, out int soTienKhachDua))
{
// Kiểm tra xem đã tính được tổng số tiền phải trả chưa
if (int.TryParse(txbtongtienphaitra.Text, out int tongTienPhaiTra))
{
// Tính số tiền trả lại và gán vào txbsotientralai
int soTienTraLai = soTienKhachDua - tongTienPhaiTra;
txbsotientralai.Text = soTienTraLai.ToString();
}
else
{
// Xử lý trường hợp không thể chuyển đổi tổng số tiền phải trả sang kiểu số
MessageBox.Show("Tổng số tiền phải trả không hợp lệ.");
}
}
else
{
// Xử lý trường hợp không thể chuyển đổi số tiền khách đưa sang kiểu số
MessageBox.Show("Số tiền khách đưa không hợp lệ.");
}
}
else
{
// Xử lý trường hợp chưa nhập số tiền khách đưa
txbsotientralai.Clear(); // Xóa kết quả trả lại nếu không có số tiền khách đưa
}
}
}
}

You might also like