📄 proctstuinfo.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using WindowsApplication1.baseclass;
namespace WindowsApplication1
{
public partial class ProctStuInfo : Form
{
OperateAndValidate opvalidate = new OperateAndValidate();
SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
BaseOperate boperate = new BaseOperate();
protected string M_str_sql = "select s_number as 学号,s_name as 姓名,department as 院系,"
+ "profession as 专业,class as 班级,Sphoto as 照片路径 from student ";
protected string M_str_table = "student";
//dataGridView1表中的数据是否有变化
bool ifnew = false;
public ProctStuInfo()
{
InitializeComponent();
}
/// <显示照片>
/// 点击picturebox,浏览、修改考生照片
/// </显示照片>
/// <param name="sender"></param>
/// <param name="e"></param>
private void pictureBox2_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "(*.*)|*.*|(*.gif)|*.gif|(*.jpg)|*.jpg|(*.jpeg)|*.jpeg|(*.ico)|*.ico|(*.bmp)|*.bmp";
openFileDialog1.ShowDialog();
//如果文件名不为空,加载图片
if (openFileDialog1.FileName != "")
this.pictureBox2.Image = Image.FromFile(openFileDialog1.FileName);
}
//加载数据库中的数据
public void load()
{
DataSet myds = boperate.getds(M_str_sql, M_str_table);
dataGridView1.DataSource = myds.Tables[0];
if (myds.Tables[0].Rows.Count <= 0)
{
dataGridView1.AllowUserToAddRows = true;
}
if (myds.Tables[0].Rows.Count > 0)
btnDelete.Enabled = true;
else
btnDelete.Enabled = false;
//显示所有学生的数量
int num = dataGridView1.RowCount;
labelnum.Text = (num - 1).ToString().Trim();
}
private void ProctStuInfo_Load(object sender, EventArgs e)
{
load();
//院系下拉框绑定院系表中的数据
opvalidate.cboxBind("select department from department","department","department",comBDeparture);
}
/// <选择院系>
/// 改变院系下拉列表中的索引,将导致对专业班级的进一步筛选
/// </选择院系>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comBDeparture_SelectedIndexChanged(object sender, EventArgs e)
{
if (comBDeparture.Text.Trim() != "")
{
opvalidate.cboxBind("select department,Profession from Profession where department = '" + comBDeparture.Text.Trim() + "'", "Profession", "Profession", comBoxProfession);
}
}
/// <添加>
/// 向数据库中添加考生信息
/// </添加>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
using (addstu add = new addstu("Add"))
{
opvalidate.cboxBind("select department from department", "department", "department", add.comBDeparture);
inheritant(add);
add.ShowDialog();
}
ProctStuInfo_Load(sender, e);
if (cn.State == ConnectionState.Open)
cn.Close();
}
#region 初始化添加、更新窗口
public void inheritant(addstu add)
{
add.txtStuName.Text = this.txtStuName.Text;
add.txtStuNum.Text = this.txtStuNum.Text;
add.comBDeparture.Text = this.comBDeparture.Text;
add.comBprofession.Text = this.comBoxProfession.Text;
add.comBClass.Text = this.comBClass.Text;
add.pictureBox1.Image = this.pictureBox2.Image;
}
#endregion
/// <修改>
/// 更新数据库中考生信息
/// </修改>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdata_Click(object sender, EventArgs e)
{
if (dataGridView1.DataSource != null && dataGridView1[0, dataGridView1.CurrentCell.RowIndex] != null)
{
using (addstu add = new addstu("Modification"))
{
opvalidate.cboxBind("select department from department", "department", "department", add.comBDeparture);
inheritant(add);
//设置学号属性为只读
add.txtStuNum.ReadOnly = true;
string path = Convert.ToString(dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value).Trim();
add.ShowDialog();
}
}
ProctStuInfo_Load(sender, e);
}
private void comBClass_SelectedIndexChanged(object sender, EventArgs e)
{
if (comBClass.Text.Trim() != "")
{
SqlDataAdapter adp = new SqlDataAdapter("select * from student where class = '" + comBClass.Text.Trim() + "'", cn);
DataSet ds = new DataSet();
adp.Fill(ds, "class");
if (cn.State == ConnectionState.Open)
cn.Close();
}
}
/// <summary>
/// 鼠标单击GridView中的单元格,左侧将进行相应的显示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
try
{
//改变选中列的背景色
for (int i = 0; i < dataGridView1.Rows.Count ; i++)
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.White;
dataGridView1.CurrentRow.DefaultCellStyle.BackColor = Color.LightSlateGray;
//设置鼠标点击行为选择状态
dataGridView1.CurrentRow.Selected = true;
txtStuNum.Text = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
txtStuName.Text = Convert.ToString(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
comBDeparture.Text = Convert.ToString(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value).Trim();
comBoxProfession.Text = Convert.ToString(dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value).Trim();
comBClass.Text = Convert.ToString(dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value).Trim();
//显示学生照片
string path = Convert.ToString(dataGridView1[5, dataGridView1.CurrentCell.RowIndex].Value).Trim();
if (path != "")
pictureBox2.Image = Image.FromFile(Application.StartupPath.Trim() + path);
else
pictureBox2.Image =null;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <取消>
/// 取消,并关闭对话框
/// </取消>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBack_Click(object sender, EventArgs e)
{
this.Close();
}
/// <删除>
/// 删除学生信息
/// </删除>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("确定要删除吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -