📄 formyggl.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 _10901BS
{
public partial class formYggl : Form
{
int b = 0;
SqlConnection cn = new SqlConnection();
SqlCommand cm= new SqlCommand();
SqlDataAdapter da;
SqlDataReader bbb;
DataSet bb = new DataSet();
public formYggl()
{
InitializeComponent();
}
private void ygBiao_Load(object sender, EventArgs e)
{
this.txtygID.Text = "";
this.txtygName.Text = "";
this.txtygYuexin.Text = "";
this.txtbeiZhu.Text = "";
if (cn.State == ConnectionState.Closed)
{
cn.ConnectionString = "Data Source=.;Initial Catalog=110901DB;Integrated Security=True";
cn.Open();
}
da = new SqlDataAdapter("select ygID as '员工编号' , ygName as '员工姓名' , ygSex as '员工性别' , buMen as '所在部门' , zhiWu as '员工职务' , ygYuexin as '员工月薪' , beizhu as '备注' from ygBiao", cn);
bb.Clear();
da.Fill(bb);
this.dataGridView1.DataSource = bb.Tables[0];
}
private void 查询_Click(object sender, EventArgs e)
{
if (txtygID.Text == "" && txtygName.Text == "" && cb_Sex.Text == "" && cb_Bm.Text == "" && cb_Zw.Text == "" && txtygYuexin.Text == "")
{
MessageBox.Show("请输入您所需要的查询信息!");
return;
}
else
{
cm.Connection = cn;
cm.CommandText = "select * from ygBiao where ygID like '%" + this.txtygID.Text + "%' and ygName like '%" + this.txtygName.Text + "%' and ygSex like '%" + this.cb_Sex.Text + "%' and buMen like '%" + this.cb_Bm.Text + "%' and zhiWu like '%" + this.cb_Zw.Text + "%' and ygYuexin like '%" + this.txtygYuexin.Text + "%' ";
bbb = cm.ExecuteReader();
if (bbb.Read())
{
bb.Clear();
bbb.Close();
cm.CommandText = "select ygID as '员工编号' , ygName as '员工姓名' , ygSex as '员工性别' , buMen as '所在部门' , zhiWu as '员工职务' , ygYuexin as '员工月薪' , beizhu as '备注' from ygBiao where ygID like '%" + this.txtygID.Text + "%' and ygName like '%" + this.txtygName.Text + "%' and ygSex like '%" + this.cb_Sex.Text + "%' and buMen like '%" + this.cb_Bm.Text + "%' and zhiWu like '%" + this.cb_Zw.Text + "%' and ygYuexin like '%" + this.txtygYuexin.Text + "%' ";
da.SelectCommand = cm;
da.Fill(bb);
this.dataGridView1.DataSource = bb.Tables[0];
MessageBox.Show("查询成功!");
}
else
{
bbb.Close();
MessageBox.Show("系统提示:您的查询的信息不存在!");
}
}
}
private void txtygID_KeyPress(object sender, KeyPressEventArgs e)
{
//只能输入数字和退格
if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && (int)e.KeyChar != 8)
{
e.Handled = true;
}
}
private void txtygName_KeyPress(object sender, KeyPressEventArgs e)
{
//用循环的方式禁用全角字符
char[] cc ={ '~', '!', '·', '#', '¥', '%', '…', '—', '*', '(', ')', '《', '》', '?', '。', ',', '“', ':', ';', '‘' };
foreach (char c in cc)
{
if (e.KeyChar == c)
e.Handled = true;
}
//禁用ASCII表中的所有字符,但是允许删除
if ((int)e.KeyChar >= 0 && (int)e.KeyChar <= 255 && (int)e.KeyChar != 8)
{
e.Handled = true;
}
}
//combox的keypresss禁用
private void txtygSex_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && e.KeyChar != 13 )
{
e.Handled = true;
}
////用循环的方式禁用全角字符
//char[] cc ={ '~', '!', '·', '#', '¥', '%', '…', '—', '*', '(', ')', '《', '》', '?', '。', ',', '“', ':', ';', '‘' };
//foreach (char c in cc)
//{
// if (e.KeyChar == c)
// e.Handled = true;
//}
////禁用ASCII表中的所有字符,但是允许删除
//if ((int)e.KeyChar >= 0 && (int)e.KeyChar <= 255 && (int)e.KeyChar != 8)
//{
// e.Handled = true;
//}
}
private void txtygYuexin_KeyPress(object sender, KeyPressEventArgs e)
{
if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && (int)e.KeyChar != 8 && e.KeyChar != '.')
{
e.Handled = true;
}
else if (e.KeyChar == '.')
{
if (this.txtygYuexin.Text.IndexOf('.') >= 0) //如果已经有小数点存在,则不能再输入小数点
e.Handled = true;
//如果小数点不存在,但是文本框没有任何字符,则也禁用'.'
//禁止文本框的第一个字符为'.'
else if (this.txtygYuexin.Text.Length == 0)
e.Handled = true;
}
if (this.txtygYuexin.Text.Contains("."))
{
if (this.txtygYuexin.Text.Substring(this.txtygYuexin.Text.IndexOf('.')).Length > 2 && e.KeyChar!=8)
{
e.Handled = true;
}
}
}
//添加功能
private void tsl_Tj_Click(object sender, EventArgs e)
{
if (this.txtygID.Text.Length != 4)
{
MessageBox.Show("编号输入有误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtygID.Text = "";
txtygID.Focus();
return;
}
if(this.txtygName.Text=="" )
{
MessageBox.Show("您尚未输入姓名,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtygName.Focus();
return;
}
if (this.cb_Sex.Text == "")
{
this.cb_Sex.Text = "男";
}
if (this.cb_Bm.Text == "")
{
this.cb_Bm.Text = "销售部";
}
if (this.cb_Zw.Text == "")
{
this.cb_Zw.Text = "营业员";
}
if (this.txtygYuexin.Text == "")
{
this.txtygYuexin.Text = "0.00";
}
if (this.txtygID.Text == "")
{
cm.Connection = cn;
cm.CommandText = "select ygID from ygBiao ORDER by YGID DESC ";
this.txtygID.Text =Convert.ToString(Convert.ToInt32(cm.ExecuteScalar().ToString())+1).PadLeft(4,'0');
}
cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "select * from ygBiao where ygID = '" + this.txtygID.Text + "' ";
bbb = cm.ExecuteReader();
if (bbb.Read())
{
MessageBox.Show("您输入的编号已存在,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtygID.Text = "";
txtygID.Focus();
bbb.Close();
}
else
{
bbb.Close();
cm.Connection = cn;
cm.CommandText = "insert ygBiao values('" + this.txtygID.Text + "','" + this.txtygName.Text + "','" + this.cb_Sex.Text + "','" + this.cb_Bm.Text + "','" + this.cb_Zw.Text + "','" + this.txtygYuexin.Text + "','" + this.txtbeiZhu.Text + "')";
cm.ExecuteNonQuery();
MessageBox.Show("添加成功!");
ygBiao_Load(sender, e);
}
}
//修改功能
private void tsl_xg_Click(object sender, EventArgs e)
{
if (this.txtygID.Text.Length != 4)
{
MessageBox.Show("编号输入有误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtygID.Text = "";
txtygID.Focus();
return;
}
if (this.txtygName.Text == "")
{
MessageBox.Show("您尚未输入姓名,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtygName.Focus();
return;
}
if (this.cb_Sex.Text == "")
{
this.cb_Sex.Text = "男";
}
if (this.cb_Bm.Text == "")
{
this.cb_Bm.Text = "销售部";
}
if (this.cb_Zw.Text == "")
{
this.cb_Zw.Text = "营业员";
}
if (this.txtygYuexin.Text == "")
{
this.txtygYuexin.Text = "0.00";
}
if (this.txtygID.Text == "")
{
cm.Connection = cn;
cm.CommandText = "select ygID from ygBiao ORDER by YGID DESC ";
this.txtygID.Text = Convert.ToString(Convert.ToInt32(cm.ExecuteScalar().ToString()) + 1).PadLeft(4, '0');
}
cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "update ygBiao set ygName='" + this.txtygName.Text + "',ygSex='" + this.cb_Sex.Text + "',buMen='" + this.cb_Bm.Text + "',zhiWu='" + this.cb_Zw.Text + "',ygYuexin='" + this.txtygYuexin.Text + "',beiZhu='" + this.txtbeiZhu.Text + "'where ygID='" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
cm.ExecuteNonQuery();
MessageBox.Show("修改成功!");
ygBiao_Load(sender, e);
}
//删除功能
private void tsl_Sc_Click(object sender, EventArgs e)
{
if (b == 0)
{
cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "select * from yhxxBiao where yhName = '" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "' ";
bbb = cm.ExecuteReader();
if (bbb.Read())
{
MessageBox.Show("对不起此员工信息已经启用不能删除!");
bbb.Close();
return;
}
//--------------------------------------------------------------------------------------------------
DialogResult c = MessageBox.Show("真的要删除吗?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (c == DialogResult.Yes)
{
bbb.Close();
cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "delete ygBiao where ygID='" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
cm.ExecuteNonQuery();
MessageBox.Show("删除成功!");
ygBiao_Load(sender, e);
}
}
else
{
MessageBox.Show("您的操作未完成,请点击 取消 后进行此操作!");
}
}
//刷新功能
private void bt_Xx_Click(object sender, EventArgs e)
{
ygBiao_Load(sender, e);
}
//text文本根据网格选择显示
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (this.dataGridView1.Focused == true)
{
if (this.dataGridView1.SelectedCells.Count>0)
{
this.txtygID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
this.txtygName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
this.cb_Sex.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
this.cb_Bm.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
this.cb_Zw.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
this.txtygYuexin.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
this.txtbeiZhu.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
}
}
}
//判断员工编号的输入位数
private void txtygID_Leave(object sender, EventArgs e)
{
if (this.txtygID.Text.Length != 4 && this.txtygID.Text!="")
{
MessageBox.Show("编号输入有误,请重新输入!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtygID.Text = "";
txtygID.Focus();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -