📄 department.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace StudentManager
{
public partial class Department : Form
{
private SqlConnection conn1 = null;
private SqlDataAdapter sqlDataAdapter1 = null;
private DataSet DataSetDep = new DataSet("Department");
private string selectStr;
public Department()
{
InitializeComponent();
this.conn1 = new SqlConnection(DataLevel.Connection.ConnString);
this.selectStr = "select * from Department";
this.sqlDataAdapter1 = new SqlDataAdapter(this.selectStr, this.conn1);
this.conn1.Open();
this.sqlDataAdapter1.Fill(this.DataSetDep, "Department");
this.DataSet_Bingding();
this.Buttons_Control(false);
this.conn1.Close();
}
private void Department_Load(object sender, System.EventArgs e)
{
try
{
this.conn1.Open();
this.sqlDataAdapter1.Fill(this.DataSetDep, "Department");
this.DataSet_Bingding();
this.Buttons_Control(false);
}
catch (Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
this.conn1.Close();
}
}
private void DataSet_Bingding()
{
this.dataGridView1.DataSource = this.DataSetDep;
this.dataGridView1.DataMember="Department";
this.textDepID.DataBindings.Add("Text", this.DataSetDep, "Department.DepartmentID");
this.textDepName.DataBindings.Add("Text", this.DataSetDep, "Department.DepartmentName");
this.textTelNo.DataBindings.Add("Text", this.DataSetDep, "Department.PhoneNO");
}
private void Buttons_Control(bool IsValid)
{
if (IsValid)
{
this.btnCancel.Enabled = true;
this.btnApply.Enabled = true;
this.textDepID.Enabled = true;
this.textDepName.Enabled = true;
this.textTelNo.Enabled = true;
}
else
{
this.btnCancel.Enabled = false;
this.btnApply.Enabled = false;
this.textDepID.Enabled = false;
this.textDepName.Enabled = false;
this.textTelNo.Enabled = false;
}
}
private void butAdd_Click(object sender, EventArgs e)
{
MessageBox.Show("请注意学院代码不准为空 !否则添加失败 !");
this.Buttons_Control(true);
this.BindingContext[this.DataSetDep, "Department"].AddNew();
}
private void btnApply_Click(object sender, EventArgs e)
{
if (textDepID.Text=="")
{
MessageBox.Show("请注意学院代码不准为空 !否则添加失败 !");
return;
}
this.BindingContext[this.DataSetDep, "Department"].EndCurrentEdit();
if (this.conn1.State == ConnectionState.Closed)
this.conn1.Open();
SqlCommandBuilder commandbuilder1 = new SqlCommandBuilder(this.sqlDataAdapter1);
this.sqlDataAdapter1.Update(this.DataSetDep, "Department");
this.DataSetDep.AcceptChanges();
this.dataGridView1.Refresh();
Buttons_Control(false);
}
private void butModify_Click(object sender, EventArgs e)
{
this.Buttons_Control(true);
}
private void butDel_Click(object sender, EventArgs e)
{
if (this.BindingContext[this.DataSetDep, "Department"].Count > 0 &&
(MessageBox.Show("真的要删除此记录吗 ?", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question).Equals(DialogResult.OK)))
{
int position = this.BindingContext[this.DataSetDep, "Department"].Position;
this.BindingContext[this.DataSetDep, "Department"].RemoveAt(position);
if (conn1.State == ConnectionState.Closed)
conn1.Open();
SqlCommandBuilder commandbuilder1 = new SqlCommandBuilder(this.sqlDataAdapter1);
this.sqlDataAdapter1.Update(this.DataSetDep, "Department");
this.DataSetDep.AcceptChanges();
this.dataGridView1.Refresh();
Buttons_Control(false);
}
else
return;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.BindingContext[this.DataSetDep, "Department"].CancelCurrentEdit();
this.Buttons_Control(false);
}
private void butcanall_Click(object sender, EventArgs e)
{
this.DataSetDep.RejectChanges();
this.Buttons_Control(false);
}
private void butExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -