📄 cdlgmaintain.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OracleClient;
using GlobleUtility;
namespace PlatF
{
/// <summary>
/// CdlgMaintainForCharge 的摘要说明。
/// </summary>
public class CdlgMaintainForCharge : System.Windows.Forms.Form
{
private ErrorCode E = new ErrorCode();
private DataSet myDataSet;//取得收费点信息,填充DataGrid
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.DataGrid dgSiteInfo;//DataGrid控件用来显示收费点信息
private System.Windows.Forms.Button btnAdd;//“添加”按钮
private System.Windows.Forms.Button btnModify;//“修改”按钮
private System.Windows.Forms.Button btnDel;//“删除”按钮
private System.Windows.Forms.Button btnExit;//“退出”按钮
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public CdlgMaintainForCharge()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
InitSiteInfoGrid();
RefreshAllSiteInfo();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.dgSiteInfo = new System.Windows.Forms.DataGrid();
this.btnAdd = new System.Windows.Forms.Button();
this.btnModify = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dgSiteInfo)).BeginInit();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.dgSiteInfo);
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(568, 320);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "所有收费点信息";
//
// dgSiteInfo
//
this.dgSiteInfo.CaptionVisible = false;
this.dgSiteInfo.DataMember = "";
this.dgSiteInfo.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dgSiteInfo.Location = new System.Drawing.Point(0, 24);
this.dgSiteInfo.Name = "dgSiteInfo";
this.dgSiteInfo.ReadOnly = true;
this.dgSiteInfo.Size = new System.Drawing.Size(568, 296);
this.dgSiteInfo.TabIndex = 0;
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(8, 328);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(72, 32);
this.btnAdd.TabIndex = 1;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// btnModify
//
this.btnModify.Location = new System.Drawing.Point(112, 328);
this.btnModify.Name = "btnModify";
this.btnModify.Size = new System.Drawing.Size(72, 32);
this.btnModify.TabIndex = 2;
this.btnModify.Text = "修改(&M)";
this.btnModify.Click += new System.EventHandler(this.btnModify_Click);
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(248, 328);
this.btnDel.Name = "btnDel";
this.btnDel.Size = new System.Drawing.Size(72, 32);
this.btnDel.TabIndex = 3;
this.btnDel.Text = "删除(&D)";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(376, 328);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(72, 32);
this.btnExit.TabIndex = 4;
this.btnExit.Text = "退出(&X)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// CdlgMaintainForCharge
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(562, 378);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnDel);
this.Controls.Add(this.btnModify);
this.Controls.Add(this.btnAdd);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "CdlgMaintainForCharge";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "收费点维护";
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dgSiteInfo)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 初始化收费点信息列表
/// </summary>
private void InitSiteInfoGrid()
{
// 初始化收费点信息列表
string[] FieldInfo = {"SiteID", "SiteName","descrip","MaxMoney","State"};//与DataGrid各列标头邦定的纪录字段
string[] HeadTextInfo = {"收费点编号", "名称", "描述","最大收费金额(单位:元)","当前状态"};//DataGrid控件列标头
int[] width = {70, 100, 100, 100, 150};
DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = "SiteInfo";
//对DataGrid进行绑定
for (int i = 0; i < FieldInfo.Length; i++)
{
DataGridColumnStyle col = new DataGridTextBoxColumn();
col.Alignment = System.Windows.Forms.HorizontalAlignment.Center;
col.HeaderText = HeadTextInfo[i];
col.MappingName = FieldInfo[i];
col.NullText = "";
col.Width = width[i];
ts1.GridColumnStyles.Add(col);
}
dgSiteInfo.TableStyles.Add(ts1);
ts1.DataGrid = dgSiteInfo;
}
/// <summary>
/// 刷新收费点信息列表内容
/// </summary>
//刷新收费点信息列表内容
private void RefreshAllSiteInfo()
{
OracleConnection myConn = Connection.DBConnection;
if (myConn == null)
{
MessageBox.Show("连接数据库失败!");
this.Close();
return;
}
string mySql = "Select SiteID, SiteName,descrip,MaxMoney/100 MaxMoney,decode(State,0,'禁用',1,'在用') state From Charge_Site_Info Order By SiteID";
OracleDataAdapter myDataAdapter = new OracleDataAdapter(mySql, myConn);
myDataSet = new DataSet();
try
{
myDataAdapter.Fill(myDataSet);//填充数据集
myDataSet.Tables[0].TableName = "SiteInfo";//定义数据集中表的表名
dgSiteInfo.DataSource = myDataSet.Tables[0].DefaultView;//定义DataGrid数据源,从而绑定DataGrid与数据集中表SiteInfo
}
catch
{
MessageBox.Show("数据库错误!");
this.Close();
return;
}
}
/// <summary>
/// 显示添加/修改窗口
/// </summary>
/// <param name="SiteID">收费点编号</param>
private void ShowDetail(int SiteID)
{
CdlgSiteInfoForCharge dlg = new CdlgSiteInfoForCharge();
if (dlg.RefreshSiteInfo(SiteID) == 0)
{
if (dlg.ShowDialog(this) == DialogResult.OK)//添加或修改成功
{
int nSiteID = dlg.SiteID;//取得新增的收费点编号或被修改的收费点的编号
RefreshAllSiteInfo();//刷新收费点列表
//在DataGrid中找到新增的收费点项或被修改的收费点项,将其设为选中状态
for (int i = dgSiteInfo.VisibleRowCount - 1; i >= 0; i--)
{
if (nSiteID == Convert.ToInt32(dgSiteInfo[i, 0]))
{
dgSiteInfo.Select(i);
dgSiteInfo.CurrentRowIndex = i;
break;
}
}
}
}
}
//添加
private void btnAdd_Click(object sender, System.EventArgs e)
{
ShowDetail(-1);
}
//修改
private void btnModify_Click(object sender, System.EventArgs e)
{
int rowNum = dgSiteInfo.CurrentRowIndex;
if (rowNum == -1)//未选中收费点
{
MessageBox.Show(this, "请选中要修改的收费点!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
int siteID = Convert.ToInt32(dgSiteInfo[rowNum, 0]);//取得收费点编号
ShowDetail(siteID);
}
}
//删除
private void btnDel_Click(object sender, System.EventArgs e)
{
int index = dgSiteInfo.CurrentRowIndex;//取得DataGrid选定行的索引
if (index >= 0)
{
if (MessageBox.Show(this, "确定要删除该收费点吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
int siteID = Convert.ToInt32(dgSiteInfo[index, 0]);//DataGrid首列为收费点编号
OracleParameter[] inParam = {new OracleParameter()};
inParam[0].ParameterName = "p_SiteID";
inParam[0].OracleType = OracleType.Int32;
inParam[0].Value = siteID;
OracleParameter[] outParam = {new OracleParameter()};
outParam[0].ParameterName = "p_Result";
outParam[0].OracleType = OracleType.Int32;
outParam[0].Direction = ParameterDirection.Output;
GlobleHelper.ExecStoredProc("SP_ChargeSite_Del", inParam, ref outParam);
int error = Convert.ToInt32(outParam[0].Value);
if (error != 0)
{
E.ShowMessage(error);
MessageBox.Show("删除失败!");
return;
}
//数据库中删除成功,在DataSet中移除选中的收费点对应的纪录
for (int i = 0; i < myDataSet.Tables[0].Rows.Count; i++)
{
if (Convert.ToInt32(myDataSet.Tables[0].Rows[i][0]) == siteID)
{
myDataSet.Tables[0].Rows.RemoveAt(i);
break;
}
}
}
else //未选中收费点
{
MessageBox.Show(this, "请选中要删除的收费点!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//退出
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -