📄 formghsbiao.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 formGhsBiao : Form
{
int a = 0;
SqlConnection cn = new SqlConnection();
SqlCommand cm = new SqlCommand();
SqlDataAdapter da;
SqlDataReader aaa;
DataSet aa = new DataSet();
public formGhsBiao()
{
InitializeComponent();
}
private void ghs_Load(object sender, EventArgs e)
{
if (cn.State == ConnectionState.Closed)
{
cn.ConnectionString = "Data Source=.;Initial Catalog=110901DB;Integrated Security=True";
cn.Open();
}
da = new SqlDataAdapter("select gysID as '供应商编号',gysName as '供应商名称',lxrName as '联系人姓名',lxDh as '联系电话',lxDzSheng as '所在省份',lxDzshi as '所在城市',lxDzJdmph as '详细地址街道门牌号',yfJe as '应付金额',beizhu as '备注' from ghsBiao", cn);
aa.Clear();
da.Fill(aa);
this.dataGridView1.DataSource = aa.Tables[0];
this.txtghsID.Text = "";
this.txtghsName.Text ="";
this.txtlxrName.Text = "";
this.txtlxDh.Text = "";
this.txtlxDzSheng.Text = "";
this.txtlxDzshi.Text = "";
this.txtlxDzJdmph.Text = "";
this.txtyfJe.Text = "";
this.txtBeiZhu.Text = "";
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
this.txtghsID.Text = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
this.txtghsName.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
this.txtlxrName.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
this.txtlxDh.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
this.txtlxDzSheng.Text = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
this.txtlxDzshi.Text = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
this.txtlxDzJdmph.Text = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
this.txtyfJe.Text = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();
this.txtBeiZhu.Text = this.dataGridView1.CurrentRow.Cells[8].Value.ToString();
}
private void 添加ToolStripMenuItem2_Click(object sender, EventArgs e)
{
if (this.txtghsID.Text == "")
{
MessageBox.Show("您尚未添加供应商编号!");
this.txtghsID.Focus();
return;
}
cm = new SqlCommand();
cm.Connection = cn;
cm.CommandText = "select gysID from ghsBiao where gysID='" + this.txtghsID.Text + "' ";
aaa = cm.ExecuteReader();
if (aaa.Read())
{
aaa.Close();
MessageBox.Show("此供应商编号已存在,请重新输入!");
this.txtghsID.Text = "";
this.txtghsID.Focus();
}
aaa.Close();
cm.Connection = cn;
cm.CommandText = "insert ghsBiao values('" + this.txtghsID.Text + "','" + this.txtghsName.Text + "','" + this.txtlxrName.Text + "','" + this.txtlxDh.Text + "','" + this.txtlxDzSheng.Text + "','" + this.txtlxDzshi.Text + "','" + this.txtlxDzJdmph.Text + "','"+this.txtyfJe.Text+"','" + this.txtBeiZhu.Text + "')";
cm.ExecuteNonQuery();
MessageBox.Show("添加成功!");
ghs_Load(sender, e);
}
private void 修改ToolStripMenuItem3_Click(object sender, EventArgs e)
{
cm.Connection = cn;
cm.CommandText = "update ghsBiao set gysName='" + this.txtghsName.Text + "',lxrName='" + this.txtlxrName.Text + "',lxDh='" + this.txtlxDh.Text + "',lxDzSheng='" + this.txtlxDzSheng.Text + "',lxDzshi='" + this.txtlxDzshi.Text + "',lxDzJdmph='" + this.txtlxDzJdmph.Text + "',beiZhu='" + this.txtBeiZhu.Text + "'where gysID='" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
cm.ExecuteNonQuery();
MessageBox.Show("修改成功!");
ghs_Load(sender, e);
}
private void 删除ToolStripMenuItem4_Click(object sender, EventArgs e)
{
if (a == 0)
{
cm.Connection = cn;
cm.CommandText = "select * from jhxqBIAO where gysID='" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
aaa = cm.ExecuteReader();
if (aaa.Read())
{
MessageBox.Show("已跟此供应商建立合作关系,此供应商的信息不能被删除!");
aaa.Close();
return;
}
aaa.Close();
DialogResult c = MessageBox.Show("真的要删除吗?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (c == DialogResult.Yes)
{
cm.Connection = cn;
cm.CommandText = "delete ghsBiao where gysID='" + this.dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'";
cm.ExecuteNonQuery();
MessageBox.Show("删除成功!");
ghs_Load(sender, e);
}
}
else
{
MessageBox.Show("您的操作未完成,请点击 取消 后进行此操作!");
}
}
private void 刷新ToolStripMenuItem6_Click(object sender, EventArgs e)
{
ghs_Load(sender, e);
}
private void 查询_Click(object sender, EventArgs e)
{
if (txtghsID.Text == "" && txtghsName.Text == "" && txtlxrName.Text == "" && txtlxDh.Text == "" && txtlxDzSheng.Text == "" && txtlxDzshi.Text == "" && txtlxDzJdmph.Text == "" && txtyfJe.Text == "")
{
MessageBox.Show("请输入查询信息");
return;
}
else
{
cm.Connection = cn;
cm.CommandText = "select * from ghsBiao where gysID like '%" + this.txtghsID.Text + "%' and gysName like '%" + txtghsName.Text + "%' and lxrName like '%" + txtlxrName.Text + "%' ";
cm.CommandText +="and lxDh like '%" + txtlxDh.Text + "%' and lxDzSheng like '%" + txtlxDzSheng.Text + "%' and lxDzshi like '%" + txtlxDzshi.Text + "%' and lxDzJdmph like '%" + txtlxDzJdmph.Text + "%' and yfJe like '%" + txtyfJe.Text + "%'";
aaa = cm.ExecuteReader();
if (aaa.Read())
{
aa.Clear();
aaa.Close();
cm.CommandText = "select gysID as '供应商编号', gysName as '供应商名称', lxrName as '联系人姓名', lxDh as '联系电话', lxDzSheng as '所在省份', lxDzshi as '所在城市', lxDzJdmph as '详细地址街道门牌号', yfJe as '应付金额', beizhu as '备注' from ghsBiao where gysID like '%" + this.txtghsID.Text + "%' ";
cm.CommandText += "and gysName like '%" + txtghsName.Text + "%' and lxrName like '%" + txtlxrName.Text + "%' ";
cm.CommandText += "and lxDh like '%" + txtlxDh.Text + "%' and lxDzSheng like '%" + txtlxDzSheng.Text + "%' and lxDzshi like '%" + txtlxDzshi.Text + "%' and lxDzJdmph like '%" + txtlxDzJdmph.Text + "%' and yfJe like '%" + txtyfJe.Text + "%'";
da.SelectCommand = cm;
da.Fill(aa);
this.dataGridView1.DataSource = aa.Tables[0];
MessageBox.Show("查询成功");
}
else
{
aaa.Close();
MessageBox.Show("系统提示:你查询的信息不存在!");
}
}
}
private void txtghsID_KeyPress(object sender, KeyPressEventArgs e)
{
//只能输入数字和退格
if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && (int)e.KeyChar != 8)
{
e.Handled = true;
}
}
private void txtlxDh_KeyPress(object sender, KeyPressEventArgs e)
{
//只能输入数字和退格
if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && (int)e.KeyChar != 8)
{
e.Handled = true;
}
}
private void txtlxDzSheng_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;
}
}
private void txtlxDzshi_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;
}
}
private void txtlxDzJdmph_KeyPress(object sender, KeyPressEventArgs e)
{
//只能输入数字和退格
if (!(e.KeyChar >= '0' && e.KeyChar <= '9') && ((int)e.KeyChar >= 0 && (int)e.KeyChar <= 255)&&(int)e.KeyChar != 8)
{
e.Handled = true;
}
}
private void txtyfJe_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.txtyfJe.Text.IndexOf('.') >= 0) //如果已经有小数点存在,则不能再输入小数点
e.Handled = true;
//如果小数点不存在,但是文本框没有任何字符,则也禁用'.'
//禁止文本框的第一个字符为'.'
else if (this.txtyfJe.Text.Length == 0)
e.Handled = true;
}
if (this.txtyfJe.Text.Contains("."))
{
if (this.txtyfJe.Text.Substring(this.txtyfJe.Text.IndexOf('.')).Length > 2 && e.KeyChar != 8)
{
e.Handled = true;
}
}
}
private void txtghsID_Leave(object sender, EventArgs e)
{
if (this.txtghsID.Text.Length != 4 && this.txtghsID.Text != "")
{
MessageBox.Show("您输入的编号不正确,请重新输入!");
this.txtghsID.Text = "";
this.txtghsID.Focus();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -