📄 studentmanage.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace StudentManager
{
public partial class StudentManage : Form
{
private SqlConnection conn1 = null;
private string selectstr = null;
private SqlCommand command1 = null;
private SqlDataAdapter DataAdapter = null;
private SqlDataReader DataReader = null;
private DataSet Dataset = new DataSet();
public StudentManage()
{
InitializeComponent();
conn1 = new SqlConnection(DataLevel.Connection.ConnString);
selectstr = "Select * from Student";
this.DataAdapter = new SqlDataAdapter(selectstr, conn1);
command1 = new SqlCommand();
this.command1.Connection=conn1;
CombDep_Fill();
}
public void CombDep_Fill()
{
selectstr="Select DepartmentName from Department";
command1.CommandText=selectstr;
if(this.conn1.State==ConnectionState.Closed)
conn1.Open();
DataReader=this.command1.ExecuteReader();
while(DataReader.Read())
{
comboBoxDepartment.Items.Add(DataReader["DepartmentName"].ToString());
}
DataReader.Close();
conn1.Close();
}
public void Data_Bings()
{
this.bindingSource1.DataSource = this.Dataset;
this.bindingSource1.DataMember = "Student";
this.dataGridView1.DataSource = this.bindingSource1;
// this.dataGridView1.DataSource=Dataset;
// this.dataGridView1.DataMember="Student";
if (this.textBoxDepartment.DataBindings.Count == 0)
{
this.textBoxDepartment.DataBindings.Add("Text", this.Dataset, "Student.Department");
}
if (this.textBoxClass.DataBindings.Count == 0)
{
this.textBoxClass.DataBindings.Add("Text", this.Dataset, "Student.Class");
}
if (this.textBoxID.DataBindings.Count == 0)
{
this.textBoxID.DataBindings.Add("Text", this.Dataset, "Student.StudentID");
}
if (this.textBoxIntime.DataBindings.Count == 0)
{
this.textBoxIntime.DataBindings.Add("Text", this.Dataset, "Student.InDate");
}
if (this.textBoxName.DataBindings.Count == 0)
{
this.textBoxName.DataBindings.Add("Text", this.Dataset, "Student.StudentName");
}
if (textBoxNation.DataBindings.Count == 0)
{
this.textBoxNation.DataBindings.Add("Text", this.Dataset, "Student.Nation");
}
if (this.textBoxNationplace.DataBindings.Count == 0)
{
this.textBoxNationplace.DataBindings.Add("Text", this.Dataset, "Student.NativePlace");
}
if (this.textBoxpower.DataBindings.Count == 0)
{
this.textBoxpower.DataBindings.Add("Text", this.Dataset, "Student.Status");
}
if (this.comboBoxSex.DataBindings.Count == 0)
{
this.comboBoxSex.DataBindings.Add("Text", this.Dataset, "Student.Sex");
}
if (this.numericUpDownAge.DataBindings.Count == 0)
{
this.numericUpDownAge.DataBindings.Add("Text", this.Dataset, "Student.Age");
}
if (this.pictureBox1.DataBindings.Count == 0)
{
if (this.BindingContext[this.Dataset, "Student"].Count == 0)
{
MessageBox.Show("此班级学生人数为零 ! 没有数据可以显示 !");
}
else
{
int position = this.BindingContext[this.Dataset,"Student"].Position;
this.pictureBox1.Image = Image.FromFile(this.Dataset.Tables["Student"].Rows[position]["Picture"].ToString());
}
}
conn1.Close();
}
public void Button_Control(bool control)
{
if(control)
{
this.textBoxDepartment.Enabled=true;
this.textBoxClass.Enabled=true;
this.textBoxID.Enabled=true;
this.textBoxIntime.Enabled=true;
this.textBoxName.Enabled=true;
this.textBoxNation.Enabled=true;
this.textBoxNationplace.Enabled=true;
this.textBoxpower.Enabled=true;
this.comboBoxSex.Enabled=true;
this.btnchoice.Enabled = true;
this.numericUpDownAge.Enabled=true;
}
else
{
this.textBoxDepartment.Enabled=false;
this.textBoxClass.Enabled=false;
this.textBoxID.Enabled=false;
this.textBoxIntime.Enabled=false;
this.textBoxName.Enabled=false;
this.textBoxNation.Enabled=false;
this.btnchoice.Enabled = false;
this.textBoxNationplace.Enabled=false;
this.textBoxpower.Enabled=false;
this.comboBoxSex.Enabled=false;
this.numericUpDownAge.Enabled=false;
this.btnchoice.Enabled=false;
}
}
private void comboBoxDepartment_SelectedIndexChanged(object sender, EventArgs e)
{
this.comboBoxClass.Items.Clear();
this.comboBoxClass.Text = "";
if (conn1.State == ConnectionState.Closed)
conn1.Open();
selectstr = "Select ClassName from Class where DepartmentName='" + comboBoxDepartment.Text + "'";
this.DataReader.Close();
this.command1.CommandText = selectstr;
this.DataReader= this.command1.ExecuteReader();
while (DataReader.Read())
{
comboBoxClass.Items.Add(DataReader["ClassName"]);
}
DataReader.Close();
conn1.Close();
}
private void textBoxRecord_Changed()
{
int position=this.BindingContext[this.Dataset,"Student"].Position+1;
int count=this.BindingContext[this.Dataset,"Student"].Count;
this.textBoxRecord.Text=position+"/"+count.ToString()+"记录";
this.Picture_Change(position - 1);
}
public void Picture_Change(int position)
{
if (this.Dataset.Tables["Student"].Rows[position]["Picture"].ToString() != "")
{
this.pictureBox1.Image = Image.FromFile(this.Dataset.Tables["Student"].Rows[position]["Picture"].ToString());
}
else
{
MessageBox.Show("该同学没有添加照片!");
return;
}
}
private void btnApply1_Click(object sender, EventArgs e)
{
this.selectstr="select * from Student where Department='"+this.comboBoxDepartment.Text+"' and Class='"+this.comboBoxClass.Text+"'";
this.DataAdapter=new SqlDataAdapter (selectstr,this.conn1);
try
{
if(conn1.State==ConnectionState.Closed)
conn1.Open();
this.Dataset.Clear();
this.DataAdapter.Fill(this.Dataset,"Student");
}
catch(Exception E)
{
MessageBox.Show(E.ToString());
}
finally
{
Data_Bings();
textBoxRecord_Changed( );
Button_Control(false);
conn1.Close();
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnbehind_Click(object sender, EventArgs e)
{
int position = this.BindingContext[this.Dataset, "Student"].Position;
int count = this.BindingContext[this.Dataset, "Student"].Count;
if (count > 0)
{
if (position == 0)
{
this.BindingContext[this.Dataset, "Student"].Position = count - 1;
}
else
{
this.BindingContext[this.Dataset, "Student"].Position -= 1;
}
this.textBoxRecord_Changed();
}
else
{
this.btnbehind.Enabled = false;
this.btnnext.Enabled = false;
this.btnlast.Enabled = false;
this.btnfrist.Enabled = false;
}
}
private void btnnext_Click(object sender, EventArgs e)
{
int position = this.BindingContext[this.Dataset, "Student"].Position;
int count = this.BindingContext[this.Dataset, "Student"].Count;
if (count > 0)
{
if (position == (count-1))
{
this.BindingContext[this.Dataset, "Student"].Position = 0;
}
else
{
this.BindingContext[this.Dataset, "Student"].Position += 1;
}
this.textBoxRecord_Changed();
}
else
{
this.btnbehind.Enabled = false;
this.btnnext.Enabled = false;
this.btnlast.Enabled = false;
this.btnfrist.Enabled = false;
}
}
private void btnfrist_Click(object sender, EventArgs e)
{
int position = this.BindingContext[this.Dataset, "Student"].Position;
int count = this.BindingContext[this.Dataset, "Student"].Count;
if (count > 0)
{
this.BindingContext[this.Dataset, "Student"].Position = 0;
this.textBoxRecord_Changed();
}
else
{
this.btnbehind.Enabled = false;
this.btnnext.Enabled = false;
this.btnlast.Enabled = false;
this.btnfrist.Enabled = false;
}
}
private void btnlast_Click(object sender, EventArgs e)
{
int count=this.BindingContext[this.Dataset,"Student"].Count;
this.BindingContext[this.Dataset, "Student"].Position = count - 1;
this.textBoxRecord_Changed();
}
private void btnAdd_Click(object sender, EventArgs e)
{
this.Button_Control(true);
this.BindingContext[this.Dataset, "Student"].AddNew();
this.textBoxDepartment.Text = this.comboBoxDepartment.Text;
this.textBoxClass.Text = this.comboBoxClass.Text;
this.textBoxClass.Enabled = false;
this.textBoxDepartment.Enabled = false;
}
private void btnApply2_Click(object sender, EventArgs e)
{
this.BindingContext[this.Dataset, "Student"].EndCurrentEdit();
if (conn1.State == ConnectionState.Closed)
conn1.Open();
SqlCommandBuilder sqlcommandbuider = new SqlCommandBuilder(this.DataAdapter);
this.DataAdapter.Update(this.Dataset, "Student");
this.Dataset.AcceptChanges();
this.dataGridView1.Refresh();
this.conn1.Close();
this.DataReader.Close();
this.textBoxRecord_Changed();
Button_Control(false);
}
private void btnchoice_Click(object sender, EventArgs e)
{
selectstr = "Select * from Student";
this.DataAdapter = new SqlDataAdapter(selectstr, conn1);
this.DataAdapter.Fill(this.Dataset, this.selectstr);
int position=this.BindingContext[this.Dataset,"Student"].Position;
this.openFileDialog1.InitialDirectory="c:\\Picture";
this.openFileDialog1.Filter="jpg files(*.jpg)|*.jpg|gif files(*.gif)|*.gif|bmp files(*.bmp)|*.bmp|All files (*.*)|*.*";
if(this.openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.pictureBox1.Image=Image.FromFile(this.openFileDialog1.FileName);
this.Dataset.Tables["Student"].Rows[position]["Picture"]=this.openFileDialog1.FileName;
}
}
private void btnModify_Click(object sender, EventArgs e)
{
this.Button_Control(true);
this.comboBoxClass.Enabled = false;
this.comboBoxDepartment.Enabled = false;
}
private void btnCancle_Click(object sender, EventArgs e)
{
this.BindingContext[this.Dataset, "Student"].CancelCurrentEdit();
this.Button_Control(false);
this.comboBoxClass.Enabled = false;
this.comboBoxDepartment.Enabled = false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -