frm-switchmodify.cs
来自「这是我个人写的一个实例用于登记公司的电脑信息」· CS 代码 · 共 316 行
CS
316 行
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//*************************************************
//程序设计:殷庆飞
//时间:2007-7-24
//地点:阳谷祥光铜业
//功能:交换机维护
//*************************************************
namespace 计算机及IP管理
{
public partial class frm_switchModify : Form
{
DataSet myds = new DataSet();
UserCls MyCls = new UserCls();
bool bl_Name=false;
bool bl_Model = false;
public frm_switchModify()
{
InitializeComponent();
}
//查询
private void btnFind_Click(object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder("select * from t_switch where ");
//判断是否输入了查询条件
if (txtName.Text.Trim() != "" || txtModel.Text.Trim() != "" || txtDepartment.Text.Trim() != "")
{
if (txtName.Text.Trim() != "")
{
strSql.Append("name ='");
strSql.Append(txtName.Text.Trim());
strSql.Append("'");
bl_Name = true;
}
if (txtModel.Text.Trim() != "")
{
if (bl_Name)
{
strSql.Append(" and model ='");
strSql.Append(txtModel.Text.Trim());
strSql.Append("'");
}
else
{
strSql.Append("model ='");
strSql.Append(txtModel.Text.Trim());
strSql.Append("'");
}
bl_Model = true;
}
if (txtDepartment.Text.Trim() != "")
{
if (bl_Name || bl_Model)
{
strSql.Append("and department ='");
strSql.Append(txtDepartment.Text.Trim());
strSql.Append("'");
}
else
{
strSql.Append("department ='");
strSql.Append(txtDepartment.Text.Trim());
strSql.Append("'");
}
}
}
else
{
MessageBox.Show("请输入查询条件!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
bl_Model = false;
bl_Name = false;
return;
}
bl_Model = false;
bl_Name = false;
MyCls.GetDataToGridView(DGViewResult, strSql.ToString(), "t_switch", out myds);
//设置DataGridView的列及列标题,isbind,remarks
DGViewResult.Columns["ID"].Visible = false;
DGViewResult.Columns["name"].HeaderText = "名称";
DGViewResult.Columns["name"].Width = 100;
DGViewResult.Columns["name"].ReadOnly = true;
DGViewResult.Columns["model"].HeaderText = "型号";
DGViewResult.Columns["model"].Width = 100;
DGViewResult.Columns["network"].HeaderText = "网段";
DGViewResult.Columns["network"].Width = 80;
DGViewResult.Columns["scope"].HeaderText = "区域";
DGViewResult.Columns["scope"].Width = 120;
DGViewResult.Columns["department"].HeaderText = "部门";
DGViewResult.Columns["department"].Width = 80;
DGViewResult.Columns["charge"].HeaderText = "负责人";
DGViewResult.Columns["charge"].Width = 100;
DGViewResult.Columns["remarks"].HeaderText = "备注";
DGViewResult.Columns["remarks"].Width = 160;
if (myds.Tables[0].Rows.Count <= 0)
{
MessageBox.Show("查无数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
btnDelete.Enabled = false;
btnModify.Enabled = false;
}
else
{
btnModify.Enabled = true;
btnDelete.Enabled = true;
}
}
//退出
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
//页面载入
private void frm_switchModify_Load(object sender, EventArgs e)
{
//设置名称字段
txtName.Items.Add("一层交换机");
txtName.Items.Add("二层交换机");
txtName.Items.Add("三层交换机");
txtName.Items.Add("路由器");
txtName.Items.Add("核心交换机");
txtName.SelectedItem = "二层交换机";
//设置部门
MyCls.GetDataToComboBox("select distinct name from t_department where name<>'' order by name", txtDepartment, "t_department", "name", "name");
txtDepartment.Text = "";
//设置交机型号
MyCls.GetDataToComboBox("select distinct model from t_switch where model<>''order by model", txtModel, "t_switch", "model", "model");
txtModel.Text = "";
//设置DataGridView的隔行变色
MyCls.SetDGVColor(DGViewResult);
}
//修改结束时
private void DGViewResult_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
//获得当前所在的行列
int i = DGViewResult.CurrentCell.RowIndex;
int j = DGViewResult.CurrentCell.ColumnIndex;
switch (j)
{
case 1://名称(二层或三层交换机)
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 20))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 2://交换机型号
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 20))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 3://网段
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 15))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 4://范围
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 20))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 5://负责人所在部门
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 25))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 6://部门
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 30))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 7://负责人
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 10))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
case 8://备注
{
if (!MyCls.CheckString(DGViewResult.CurrentCell.Value.ToString(), 100))
{
DGViewResult.CurrentCell = DGViewResult.Rows[i].Cells[j];
btnModify.Enabled = false;
return;
}
}
break;
}
DataRow DR = myds.Tables["t_switch"].Rows[i];
DR.BeginEdit();
DR[j] = DGViewResult.CurrentCell.Value.ToString();
DR.EndEdit();
btnModify.Enabled = true;
}
//修改
private void btnModify_Click(object sender, EventArgs e)
{
if (myds.GetChanges() != null)//对DataGridView进行了修改
{
bool blFlag = MyCls.DataSetUpdateByDataSet(myds, "t_switch");
if (blFlag)
{
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show("修改失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("没对数据进行任何修改!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//删除
private void btnDelete_Click(object sender, EventArgs e)
{
string strReturn = MessageBox.Show("确实要删除此记录吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information).ToString();
if (strReturn == "OK")
{
int i = DGViewResult.CurrentRow.Index;
DataRow DR = myds.Tables["t_switch"].Rows[i];
DR.BeginEdit();
DR.Delete();
DR.EndEdit();
bool blFlag = MyCls.DataSetUpdateByDataSet(myds, "t_switch");
if (blFlag)
{
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
if (myds.Tables[0].Rows.Count <= 0)
{
btnDelete.Enabled = false;
btnModify.Enabled = false;
}
}
else
{
MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?