📄 usercontrol1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DataBaseManagerLibrary;
namespace SSWHManeger
{
public partial class UserControl1 : UserControl
{
private DataBaseManager DBManager;
private String userCategory;
private DataTable dataTable;
private Int64 dealID,insertDealID;
private int selectedIndex;
public UserControl1()
{
InitializeComponent();
String SqlConnectInfo = "server=localhost;database=test;uid=sa;pwd=''";
DBManager = new DataBaseManager(SqlConnectInfo);
reset();
}
public void visilize()
{
reset(); //将各个控件置为初始状态
this.Visible = true;
}
public void disVisilize()
{
this.Visible = false;
}
private void reset() //
{
dealID = 0;
dataGridView1.DataSource = null;
bt_WHdel.Enabled = false;
bt_WHxiug.Enabled = false;
tb_map.Text = "";
tb_SSname.Text = "";
tb_WHM.Text = "";
tb_WHmaneger.Text = "";
tb_WHperson.Text = "";
tb_WHshuom.Text = "";
tb_WHtel.Text = "";
tb_WHtime.Text = "";
}
public void setDataBaseManager(DataBaseManager DBManager)
{
this.DBManager = DBManager;
}
public void setUserCategory(String userCategory)
{
this.userCategory = userCategory;
}
private void bSearch_Click(object sender, EventArgs e)
{
String cmd;
if (checkInput("Select") == false) return; //用户输入不合法
cmd = generateSelectCommand(); //根据输入情况生成相应的命令
DataTable dataTable = DBManager.ExecuteSelectCommand(cmd);
if (dataTable == null)
{ //数据库出错
MessageBox.Show(DBManager.errorMessage);
return;
}
else if (dataTable.Rows.Count == 0)
{//没有搜索到对应信息,显示一个空的dataGridView1
dataGridView1.DataSource = dataTable;
return;
}
else
{//至少找到一条信息,将信息显示到dataGridView1,并将第一条信息详细列出
dataGridView1.DataSource = dataTable;
showDetailInfo(0);
}
}
private bool ErrorMessage(String msg)
{
MessageBox.Show(msg);
return false;
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex == -1 || e.RowIndex >= dataGridView1.Rows.Count - 1) return; //选择多行或选择列标签
showDetailInfo(e.RowIndex);
selectedIndex = e.RowIndex;
}
private void showDetailInfo(int idx)
{
if (idx < 0) return;
selectedIndex = idx;
dealID = (Int64)dataGridView1.Rows[idx].Cells["交易编号"].Value;//得到用户选择的行的主键
DataTable dt = DBManager.ExecuteSelectCommand(String.Format("select * from 交易信息 where 交易编号='{0}'", dealID));
if (dt == null)
{//数据库错误
MessageBox.Show(DBManager.errorMessage);
dealID = 0;
return;
}
else if (dt.Rows.Count == 0)
{ //未找到此记录
MessageBox.Show("此记录可能已被其他用户删除");
dealID = 0;
return;
}
tb_SSname.Text = (String)dt.Rows[0]["设施名称"];
tb_WHperson.Text = (String)dt.Rows[0]["维护人员"];
tb_WHtel.Text = (Int64)dt.Rows[0]["电话"]+"";
tb_WHtime.Text = (String)dt.Rows[0]["维护时间"];
tb_map.Text = (String)dt.Rows[0]["设施平面图"];
tb_WHM.Text = (float)dt.Rows[0]["维护费用"]+"";
tb_WHmaneger.Text = (String)dt.Rows[0]["管理人员"];
tb_WHshuom.Text = (String)dt.Rows[0]["维护说明"];
bt_WHxiug.Enabled = true;//此时可以操作修改、删除数据
bt_WHdel.Enabled = true;
}
private bool checkInput(String str)
{
//将多余的空格去掉
tb_SSname.Text = tb_SSname.Text.Trim();
tb_WHperson.Text = tb_WHperson.Text.Trim();
tb_WHtel.Text = tb_WHtel.Text.Trim();
tb_WHtime.Text = tb_WHtime.Text.Trim();
tb_map.Text = tb_map.Text.Trim();
tb_WHM.Text = tb_WHM.Text.Trim();
tb_WHmaneger.Text = tb_WHmaneger.Text.Trim();
tb_WHshuom.Text = tb_WHshuom.Text.Trim();
if (tb_SSname.Text == "") return ErrorMessage("设施名称为空");
if (tb_WHperson.Text == "") return ErrorMessage("维护人员为空");
if (tb_WHtel.Text == "") return ErrorMessage("电话为空");
if (tb_WHtime.Text == "") return ErrorMessage("维护时间为空");
if (tb_map.Text == "") return ErrorMessage("设施平面图为空");
if (tb_WHM.Text == "") return ErrorMessage("维护费用为空");
if (tb_WHmaneger.Text == "") return ErrorMessage("管理人员为空");
if (tb_WHshuom.Text == "") return ErrorMessage("维护说明为空");
if (tb_WHtel.Text != "")
{
try
{
Int64.Parse(tb_WHtel.Text);
}
catch {
MessageBox.Show("电话输入错误");
return false;
}
}
if (tb_WHM.Text != "")
{
try
{
double.Parse(tb_WHM.Text);
}
catch {
MessageBox.Show("金额输入错误");
return false;
}
}
return true;
}
private String generateCommand()
{
String cmd = "";
bool cmdChanged = false;
if (cmd == null) MessageBox.Show("ok");
if (tb_SSname.Text != "")
{
cmd = cmd + " 设施名称='" + tb_SSname.Text + "'";
cmdChanged = true;
}
if (tb_WHperson.Text != "")
{
if (cmdChanged) cmd = cmd + " and 维护人员 ='" + tb_WHperson.Text + "'";
else cmd = cmd + " 维护人员='" + tb_WHperson.Text + "'";
cmdChanged = true;
}
if (tb_WHtel.Text != "")
{
if (cmdChanged) cmd = cmd + " and 电话='" + tb_WHtel.Text + "'";
else cmd = cmd + "电话='" + tb_WHtel.Text + "'";
cmdChanged = true;
}
if (tb_WHtime.Text != "")
{
if (cmdChanged) cmd = cmd + " and 维护时间='" + tb_WHtime.Text + "'";
else cmd = cmd + "维护时间='" + tb_WHtime.Text + "'";
cmdChanged = true;
}
if (tb_map.Text != "")
{
if (cmdChanged) cmd = cmd + " and 设施平面图='" + tb_map.Text + "'";
else cmd = cmd + "设施平面图='" + tb_map.Text + "'";
cmdChanged = true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -