You are on page 1of 5

Yu cu: lm Form frmLop tng t nh Form sinh vin, vi cc chc nng:

Hin th d liu t bng Lp ln DataGridView


Vit s kin RowEnter cho DataGridView
Thm, Sa, Xa

Code:
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;
using System.Data.Sql;

namespace QuanLySinhVien.Forms
{
public partial class frmSinhVien : Form
{
private int statusFunction = 0; //Bin Ghi nhn trng thi ca Form: Thm/Sa/None
private String strMasv = String.Empty; //Bin lu li M sinh vin dnh cho vic Sa

public frmSinhVien()
{
InitializeComponent();
}

private void frmSinhVien_Load(object sender, EventArgs e)
{
this.ListStudent();

setStatusButtons(true);
}

//Th tc hin th d liu t bng tblSinhvien ln DataGridView
private void ListStudent()
{
String connStr;
String strSQL;
SqlConnection connection;
SqlDataAdapter adp;

//Kt ni d liu
connStr = "Server = TRUONG-PC\\SQLEXPRESS; Initial Catalog = QLSV; User Id = truongxuan; Password =
123456";
connection = new SqlConnection(connStr);
connection.Open();

//Cu lnh Select chn d liu
strSQL = "Select masv, hoten, lop, gioitinh, ngaysinh, diemtb, ghichu from tblSinhVien Order By masv";

//Fill d liu vo Dataset
adp = new SqlDataAdapter(strSQL, connStr);

DataSet ds = new DataSet();
adp.Fill(ds);

//Gn table dt cho d liu ly c trong Dataset
DataTable dt = new DataTable();
dt = ds.Tables[0];

//Gn ngun d liu ca DataGridView
dgvSinhVien.DataSource = dt;

//ng kt ni
connection.Close();

//Mt s nh dng hin th trn DataGridView
dgvSinhVien.Columns[0].Width = 100;
dgvSinhVien.Columns[1].Width = 130;
dgvSinhVien.Columns[2].Width = 90;
dgvSinhVien.Columns[3].Width = 100;
dgvSinhVien.Columns[4].Width = 100;
dgvSinhVien.Columns[5].Width = 80;
dgvSinhVien.Columns[6].Width = 220;

dgvSinhVien.Columns[0].HeaderText = "M sinh vin";
dgvSinhVien.Columns[1].HeaderText = "H v tn";
dgvSinhVien.Columns[2].HeaderText = "Lp";
dgvSinhVien.Columns[3].HeaderText = "Gii tnh";
dgvSinhVien.Columns[4].HeaderText = "Ngy sinh";
dgvSinhVien.Columns[5].HeaderText = "im TB";
dgvSinhVien.Columns[6].HeaderText = "Ghi ch";

dgvSinhVien.Columns[0].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvSinhVien.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dgvSinhVien.Columns[4].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
dgvSinhVien.Columns[5].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
}

private void btnThoat_Click(object sender, EventArgs e)
{
this.Close();
}

//Thit lp trng thi cho cc Buttons
private void setStatusButtons(Boolean edit)
{
btnThem.Enabled = edit;
btnSua.Enabled = edit;
btnXoa.Enabled = edit;
btnGhi.Enabled = !edit;
}

//Thit lp trng thi cho cc TextBoxes
private void setStatus(Boolean edit)
{
txtMasv.ReadOnly = edit;
txtHoten.ReadOnly = edit;
txtLop.ReadOnly = edit;
txtDiemTB.ReadOnly = edit;
txtGhiChu.ReadOnly = edit;
dtpNgaySinh.Enabled = !edit;
}

//Xa d liu ti cc TextBoxes
private void clearText()
{
txtMasv.Clear();
txtHoten.Clear();
txtLop.Clear();
txtDiemTB.Clear();
txtGhiChu.Clear();

txtMasv.Focus();
}

private void btnThem_Click(object sender, EventArgs e)
{
//Ghi nhn trng thi Thm
statusFunction = 1;

setStatus(false);
setStatusButtons(false);
clearText();
}

private void btnSua_Click(object sender, EventArgs e)
{
//Ghi nhn trng thi Sa
statusFunction = 2;

setStatus(false);
setStatusButtons(false);
strMasv = txtMasv.Text.Trim();
}

private void btnGhi_Click(object sender, EventArgs e)
{
String connStr;
String strSQL;
SqlConnection connection;
SqlCommand cmd;

connStr = "Server = TRUONG-PC\\SQLEXPRESS; Initial Catalog = QLSV; User Id = truongxuan; Password =
123456";
connection = new SqlConnection(connStr);
connection.Open();

if (statusFunction == 1) //y l trng thi Ghi cho vic Thm d liu
{
int gt;
if (rbtNam.Checked) gt = 1;
else gt = 0;

strSQL = "Insert into tblSinhVien(masv, hoten, lop, gioitinh, ngaysinh, diemtb, ghichu) Values ('" +
txtMasv.Text.Trim();
strSQL += "', N'" + txtHoten.Text.Trim() + "', '" + txtLop.Text.Trim() + "', " + gt.ToString() + ",
Convert(varchar(10), '" + dtpNgaySinh.Value.ToString("MM/dd/yyyy") + "', 103), " + txtDiemTB.Text.ToString() + ", N'"
+ txtGhiChu.Text.Trim() + "')";

cmd = new SqlCommand(strSQL, connection);
cmd.ExecuteNonQuery();
connection.Close();
}
else //y l trng thi Ghi cho vic Sa d liu
{
int gt;
if (rbtNam.Checked) gt = 1;
else gt = 0;

strSQL = "Update tblSinhVien Set masv = '" + txtMasv.Text.Trim() + "', hoten = N'" +
txtHoten.Text.Trim() + "', lop = '" + txtLop .Text.Trim() + "', gioitinh = " + gt.ToString() + ", ngaysinh =
Convert(varchar(10), '" + dtpNgaySinh.Value.ToString("MM/dd/yyyy") + "', 103), diemtb = " + txtDiemTB.Text.Trim() +
", ghichu = N'" + txtGhiChu.Text.Trim() + "'";
strSQL += " Where masv = '" + strMasv + "'";

cmd = new SqlCommand(strSQL, connection);
cmd.ExecuteNonQuery();
connection.Close();
}

//Sau khi thay i d liu cn hin th li d liu trn DataGridView
this.ListStudent();

//t trng thi v li 0, khng Thm cng khng Sa
statusFunction = 0;

setStatus(true);
setStatusButtons(true);
}

private void dgvSinhVien_RowEnter(object sender, DataGridViewCellEventArgs e)
{
//ly row l dng hin ti ca DataGridView
int row = e.RowIndex;

if (dgvSinhVien.Rows[row].Cells[0].Value != null)
{
txtMasv.Text = dgvSinhVien.Rows[row].Cells[0].Value.ToString();
txtHoten.Text = dgvSinhVien.Rows[row].Cells[1].Value.ToString();
txtLop.Text = dgvSinhVien.Rows[row].Cells[2].Value.ToString();
if (int.Parse(dgvSinhVien.Rows[row].Cells[3].Value.ToString()) == 1)
{
rbtNam.Checked = true;
rbtNu.Checked = false;
}
else
{
rbtNam.Checked = false;
rbtNu.Checked = true;
}
dtpNgaySinh.Value = DateTime.Parse(dgvSinhVien.Rows[row].Cells[4].Value.ToString());
txtDiemTB.Text = dgvSinhVien.Rows[row].Cells[5].Value.ToString();
txtGhiChu.Text = dgvSinhVien.Rows[row].Cells[6].Value.ToString();
}
}

private void btnXoa_Click(object sender, EventArgs e)
{
//Trc ht cn Xc nhn vic Xa?
if (MessageBox.Show("Bn c mun xa sinh vin " + txtHoten.Text.Trim() + " khng?", "Qun l sinh vin",
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.OK)
{
String connStr;
String strSQL;
SqlConnection connection;
SqlCommand cmd;

connStr = "Server = TRUONG-PC\\SQLEXPRESS; Initial Catalog = QLSV; User Id = truongxuan; Password =
123456";
connection = new SqlConnection(connStr);
connection.Open();

strSQL = "Delete from tblSinhVien Where masv = '" + txtMasv.Text.Trim() + "'";

cmd = new SqlCommand(strSQL, connection);
cmd.ExecuteNonQuery();
connection.Close();

ListStudent();
}
}
}
}

You might also like