📄 weiji.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsApplication1.KaoChangManage
{
public partial class Weiji : Form
{
SqlConnection cn = new SqlConnection("SERVER=(local);UID=sa;PWD=sa;Trusted_Connection=True;DATABASE=test1");
baseclass.OperateAndValidate opAndvalidate = new baseclass.OperateAndValidate();
baseclass.BaseOperate boperate = new baseclass.BaseOperate();
protected string M_str_sql = "select Idweiji as 自动编号,Kname as 考场名称,zuoweiid as 座位号 ,s_number as 学号,addtion as 备注 from weiji ";
protected string M_str_table = "weiji";
public Weiji()
{
InitializeComponent();
}
private void Weiji_Load(object sender, EventArgs e)
{
//加载违纪信息表的内容
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;
opAndvalidate.cboxBind("select Kname from kaochang ", "kaochang", "kname", comBoxKname);
}
/// <summary>
/// 添加违纪信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (cn.State == ConnectionState.Closed)
cn.Open();
//如果座位号输入的不是正整数,提示错误
if (!opAndvalidate.validateNum(txtzuoweiid.Text.Trim()))
errorPnum.SetError(txtzuoweiid, "请输入正整数");
if (comBoxKname.Text.Trim() == "" || txtzuoweiid.Text.Trim() == "" || txtsnum.Text.Trim() == "" || txtaddtion.Text.Trim() == "")
MessageBox.Show("请填写完整的信息", "提示");
else
{
errorPnum.Clear();
//检查该考场中是否存在输入的座位号
SqlCommand cmd =new SqlCommand("select * from zuoci where kname ='"+ comBoxKname.Text.ToString().Trim() +"'and zuoweiid ='"+ txtzuoweiid.Text.Trim() +"'",cn);
if (cmd.ExecuteScalar() == null)
MessageBox.Show(comBoxKname.Text.Trim() + "考场中不存在座位号" + txtzuoweiid.Text.Trim(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//检查该考场中是否存在输入的学号
cmd = new SqlCommand("select * from zuoci where kname ='" + comBoxKname.Text.ToString().Trim() + "'and s_number = '" + txtsnum.Text.Trim() + "'", cn);
if (cmd.ExecuteScalar() == null)
MessageBox.Show(comBoxKname.Text.Trim() + "考场中不存在学号" + txtsnum.Text.ToString().Trim(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//生成自动编号
opAndvalidate.autoNum("select idweiji from weiji", "weiji", "idweiji", "W", "1000001", txtnum);
boperate.getcom("insert into weiji(idweiji,kname,zuoweiid,addtion,s_number) values('" + txtnum.Text.ToString().Trim() + "','" + comBoxKname.Text.Trim() + "','" + txtzuoweiid.Text.Trim() + "','" + txtaddtion.Text.Trim() + "','" + txtsnum.Text.Trim() + "')");
MessageBox.Show("信息添加成功", "提示");
Weiji_Load(sender, e);
}
}
}
if (cn.State == ConnectionState.Open)
cn.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// 修改
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnUpdata_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("确定要修改吗?", "确定", MessageBoxButtons.YesNo,MessageBoxIcon.Information) == DialogResult.Yes)
{
if (cn.State == ConnectionState.Closed)
cn.Open();
if (!opAndvalidate.validateNum(txtzuoweiid.Text.Trim()))
errorPnum.SetError(txtnum, "请输入正整数");
if (comBoxKname.Text.Trim() == "" || txtzuoweiid.Text.Trim() == "" || txtsnum.Text.Trim() == "" || txtaddtion.Text.Trim() == "")
MessageBox.Show("请填写完整的信息", "提示");
else
{
//清空错误提示
errorPnum.Clear();
//检查该考场中是否存在输入的座位号
SqlCommand cmd =new SqlCommand("select * from zuoci where kname ='"+ comBoxKname.Text.ToString().Trim() +"'and zuoweiid ='"+ txtzuoweiid.Text.Trim() +"'",cn);
if (cmd.ExecuteScalar() == null)
MessageBox.Show(comBoxKname.Text.Trim() + "考场中不存在座位号" + txtzuoweiid.Text.Trim(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//检查该考场中是否存在输入的学号
cmd = new SqlCommand("select * from zuoci where kname ='" + comBoxKname.Text.ToString().Trim() + "'and s_number = '" + txtsnum.Text.Trim() + "'", cn);
if (cmd.ExecuteScalar() == null)
MessageBox.Show(comBoxKname.Text.Trim() + "考场中不存在学号" + txtsnum.Text.ToString().Trim(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
boperate.getcom("update weiji set kname = '" + comBoxKname.Text.Trim()+ "',zuoweiid='" + txtzuoweiid.Text.Trim() + "',addtion = '" + txtaddtion.Text.Trim() + "',s_number ='" + txtsnum.Text.Trim() + "' where idweiji = '" + txtnum.Text.Trim() + "'");
Weiji_Load(sender, e);
MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
#region 双击单元格显示违纪信息
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
txtnum.Text = Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
comBoxKname.Text = Convert.ToString(dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value).Trim();
txtzuoweiid.Text = Convert.ToString(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value).Trim();
txtaddtion.Text = Convert.ToString(dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value).Trim();
txtsnum.Text = Convert.ToString(dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value).Trim();
}
#endregion
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (MessageBox.Show("确定要删除吗?", "删除确定", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
boperate.getcom("delete from weiji where idweiji='" + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim() + "'");
Weiji_Load(sender, e);
MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
/// <summary>
/// 返回
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnBack_Click(object sender, EventArgs e)
{
this.Close();
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
//更改选中行的背景色
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;
}
private void tSMItemDelete_Click(object sender, EventArgs e)
{
//删除选中的多条数据
boperate.delseldata(dataGridView1, "delete from weiji where idweiji=");
Weiji_Load(sender, e);
MessageBox.Show("删除数据成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#region 改变学号时,自动填充座位号
private void txtsnum_TextChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select zuoweiid from zuoci where s_number ='"+ txtsnum.Text.Trim() +"'",cn);
if(cn.State == ConnectionState.Closed)
cn.Open();
if(cmd.ExecuteScalar() != null)
txtzuoweiid.Text = cmd.ExecuteScalar().ToString();
if (cn.State == ConnectionState.Open)
cn.Close();
}
#endregion
#region 填写座位号时,自动填充学号
private void txtzuoweiid_TextChanged(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select s_number from zuoci where zuoweiid ='" + txtzuoweiid.Text.Trim() + "' and kname = '" + comBoxKname.Text.Trim() +"'", cn);
if (cn.State == ConnectionState.Closed)
cn.Open();
if (cmd.ExecuteScalar() != null)
txtsnum.Text = cmd.ExecuteScalar().ToString();
if (cn.State == ConnectionState.Open)
cn.Close();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -