0% found this document useful (0 votes)
12 views

Source Thi

The document defines a form for managing student data in C# and SQL. It connects to a database, loads and displays student data in a datagrid, and allows adding, editing, deleting students. Functions handle database operations and updating the UI.

Uploaded by

Santos Tiến
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)
12 views

Source Thi

The document defines a form for managing student data in C# and SQL. It connects to a database, loads and displays student data in a datagrid, and allows adding, editing, deleting students. Functions handle database operations and updating the UI.

Uploaded by

Santos Tiến
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/ 3

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 CT291
{
public partial class Form1 : Form
{
public SqlConnection conn = new SqlConnection();
Ham func = new Ham();
public Form1()
{
InitializeComponent();
dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoSizeColumnsMode =
DataGridViewAutoSizeColumnsMode.Fill;
}

private void btnThoat_Click(object sender, EventArgs e)


{
Application.Exit();
}

private void Form1_Load(object sender, EventArgs e)


{
func.KetNoi(conn);
func.HienThiDuLieuDG(dataGridView1, "select sv.MSSV, sv.HoTen,
sv.NgaySinh, sv.GioiTinh, n.TenNhom from SINHVIEN sv, NHOM n where
sv.MaNhom=n.MaNhom", conn);
func.LoadComb(combNhom, "select MaNhom, TenNhom from NHOM", conn,
"TenNhom", "MaNhom");

private void dataGridView1_CellContentClick(object sender,


DataGridViewCellEventArgs e)
{
txtMSSV.Text =
dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
txtHoTen.Text =
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
dateTimeNgaySinh.Text =
dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
combNhom.Text =
dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();
if (dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString() ==
"Nam")
{
checkBoxNam.Checked = true;
checkBoxNu.Checked = false;
}
else
{
checkBoxNu.Checked = true;
checkBoxNam.Checked = false;
}
}

private void btnDatlai_Click(object sender, EventArgs e)


{
txtMSSV.Text = "";
txtHoTen.Text = "";
dateTimeNgaySinh.Text = "";
combNhom.Text = "Chọn nhóm";
checkBoxNam.Checked = false;
checkBoxNu.Checked = false;
}

private void btnThem_Click(object sender, EventArgs e)


{
DateTime ngaysinh = Convert.ToDateTime(dateTimeNgaySinh.Text);
string date = string.Format("{0:MM/dd/yyyy}", ngaysinh);
string manhom = combNhom.SelectedValue.ToString();
string gioitinh;
if(checkBoxNam.Checked == true)
{
gioitinh = "Nam";
} else
{
gioitinh = "Nữ";
}

string sql = "insert into SINHVIEN values('"+txtMSSV.Text+"',


N'"+txtHoTen.Text+"', '"+date+"', N'"+gioitinh+"', '"+manhom+"')";
func.CapNhat(sql, conn);
func.HienThiDuLieuDG(dataGridView1, "select sv.MSSV, sv.HoTen,
sv.NgaySinh, sv.GioiTinh, n.TenNhom from SINHVIEN sv, NHOM n where
sv.MaNhom=n.MaNhom", conn);
}

private void btnXoa_Click(object sender, EventArgs e)


{
MessageBox.Show("Bạn có chắc chắn muốn xóa sinh viên " +
txtMSSV.Text + " không?");
string sql = "delete from SINHVIEN where MSSV = '" + txtMSSV.Text
+ "'";
func.CapNhat(sql, conn);
func.HienThiDuLieuDG(dataGridView1, "select sv.MSSV, sv.HoTen,
sv.NgaySinh, sv.GioiTinh, n.TenNhom from SINHVIEN sv, NHOM n where
sv.MaNhom=n.MaNhom", conn);
}

private void btnSua_Click(object sender, EventArgs e)


{
DateTime ngaysinh = Convert.ToDateTime(dateTimeNgaySinh.Text);
string date = string.Format("{0:MM/dd/yyyy}", ngaysinh);
string manhom = combNhom.SelectedValue.ToString();
string gioitinh;
if (checkBoxNam.Checked == true)
{
gioitinh = "Nam";
checkBoxNu.Checked = false;
}
else
{
gioitinh = "Nữ";
checkBoxNam.Checked = false;
}
string sql = "update SINHVIEN set HoTen = N'"+txtHoTen.Text+"',
NgaySinh = '"+date+"', GioiTinh = N'"+gioitinh+"', MaNhom = '"+manhom+"' where
MSSV = '"+txtMSSV.Text+"'";
func.CapNhat(sql, conn);
func.HienThiDuLieuDG(dataGridView1, "select sv.MSSV, sv.HoTen,
sv.NgaySinh, sv.GioiTinh, n.TenNhom from SINHVIEN sv, NHOM n where
sv.MaNhom=n.MaNhom", conn);
}

private void checkBoxNam_CheckedChanged(object sender, EventArgs e)


{
checkBoxNu.Checked = false;
}

private void checkBoxNu_CheckedChanged(object sender, EventArgs e)


{
checkBoxNam.Checked = false;
}

private void textBox1_TextChanged(object sender, EventArgs e)


{
string sql = "select sv.MSSV, sv.HoTen, sv.NgaySinh, sv.GioiTinh,
n.TenNhom from SINHVIEN sv, NHOM n where sv.MaNhom=n.MaNhom and (sv.MSSV like
'%" + textBox1.Text + "%' or sv.Hoten like '%" + textBox1.Text + "%' or
n.TenNhom like '%" + textBox1.Text + "%')";
func.HienThiDuLieuDG(dataGridView1, sql, conn);
}

private void textBox1_Enter(object sender, EventArgs e)


{
string sql = "select sv.MSSV, sv.HoTen, sv.NgaySinh, sv.GioiTinh,
n.TenNhom from SINHVIEN sv, NHOM n where sv.MaNhom=n.MaNhom and (sv.MSSV like
'%" + textBox1.Text + "%' or sv.Hoten like '%" + textBox1.Text + "%' or
n.TenNhom like '%" + textBox1.Text + "%')";
func.HienThiDuLieuDG(dataGridView1, sql, conn);
}

}
}

You might also like