📄 editzd.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace Eboer.MIS.MF.WinForm
{
/// <summary>
/// EditZD 的摘要说明。
/// </summary>
public class EditZD : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ListView listView;
private System.Windows.Forms.DataGrid dataGrid;
private System.Windows.Forms.DataGrid _dataGrid;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
/// <summary>
/// 数据项是否变动
/// </summary>
private bool change = false;
/// <summary>
/// 当前选中的类目名称
/// </summary>
private string currSign = "";
private DataSet _ds = null;
public EditZD()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this._dataGrid = this.dataGrid;
this.LoadDC();
this.LoadGardCol();
Public.SizeColumnsToContent(this.dataGrid,-1,this._ds.Tables[0],null);
DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)this.dataGrid.TableStyles[0].GridColumnStyles[0];
dgtb.Width = 80;
dgtb = (DataGridTextBoxColumn)this.dataGrid.TableStyles[0].GridColumnStyles[1];
dgtb.Width = 100;
dgtb = (DataGridTextBoxColumn)this.dataGrid.TableStyles[0].GridColumnStyles[2];
dgtb.Width = 120;
this.button1.Enabled = false;
this.button2.Enabled = false;
}
private void LoadDC(){
try{
string sql = "select * from DictionaryClass order by autoID desc";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
DataSet ds = new DataSet();
ad.Fill(ds,"list");
if(ds != null && ds.Tables[0] != null){
for(int i=0;i<ds.Tables[0].Rows.Count;i++){
DataRow row = ds.Tables[0].Rows[i];
ListViewItem lvi = new ListViewItem(new string[]{row["dcName"].ToString() + "("+ row["dcSign"].ToString() +")"});
this.listView.Items.Add(lvi);
}
}
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}
/// <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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(EditZD));
this.listView = new System.Windows.Forms.ListView();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.dataGrid = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).BeginInit();
this.SuspendLayout();
//
// listView
//
this.listView.Location = new System.Drawing.Point(8, 16);
this.listView.MultiSelect = false;
this.listView.Name = "listView";
this.listView.Size = new System.Drawing.Size(296, 432);
this.listView.TabIndex = 0;
this.listView.View = System.Windows.Forms.View.List;
this.listView.SelectedIndexChanged += new System.EventHandler(this.listView_SelectedIndexChanged);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.listView);
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(312, 456);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "字典类型";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.dataGrid);
this.groupBox2.Location = new System.Drawing.Point(328, 8);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(416, 400);
this.groupBox2.TabIndex = 2;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "条目列表";
//
// dataGrid
//
this.dataGrid.CaptionVisible = false;
this.dataGrid.DataMember = "";
this.dataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid.Location = new System.Drawing.Point(8, 16);
this.dataGrid.Name = "dataGrid";
this.dataGrid.ParentRowsVisible = false;
this.dataGrid.Size = new System.Drawing.Size(400, 376);
this.dataGrid.TabIndex = 0;
this.dataGrid.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGrid_KeyDown);
this.dataGrid.CurrentCellChanged += new System.EventHandler(this.dataGrid_CurrentCellChanged);
//
// button1
//
this.button1.Location = new System.Drawing.Point(608, 416);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 23);
this.button1.TabIndex = 3;
this.button1.Text = "保存变动";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(680, 416);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 4;
this.button2.Text = "取 消";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// EditZD
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(760, 469);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "EditZD";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "设置字典";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 返回初始状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e) {
this.LoadItem(this.currSign);
}
/// <summary>
/// 保存变动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e) {
if(this.currSign != ""){
this.SaveItem(this.currSign);
this.button1.Enabled = false;
this.button2.Enabled = false;
}
}
/// <summary>
/// 点中相应条目
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listView_SelectedIndexChanged(object sender, System.EventArgs e) {
//先检查是否有数据变动
if(this.button1.Enabled && MessageBox.Show("数据有变动,是否保存?","是否保存?",MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK){
this.button1_Click(sender,e);
}
if(this.listView.Items.Count > 0 && this.listView.SelectedItems.Count > 0){
string val = this.listView.SelectedItems[0].SubItems[0].Text;
int endC = val.LastIndexOf(")",2);
string sig = val.Substring(endC + 1,val.Length - 1);
this.currSign = sig;
this.LoadItem(sig);
this.button1.Enabled = false;
this.button2.Enabled = false;
}
}
/// <summary>
/// 载入数据
/// </summary>
private void LoadItem(string dcSign){
try{
this.dataGrid = this._dataGrid;
string sql = "select diSign as 编号,diName as 值,content as 备注 from DictionaryItem where dcSign = '"+ dcSign +"'";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
DataSet ds = new DataSet();
ad.Fill(ds,"list");
this.dataGrid.SetDataBinding(ds.Tables[0],"");
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}
/// <summary>
/// 载入条目空表
/// </summary>
private void LoadGardCol(){
try{
string sql = "select diSign as 编号,diName as 值,content as 备注 from DictionaryItem where autoID = 0";
OleDbDataAdapter ad = new OleDbDataAdapter(sql,Public.conn);
this._ds = new DataSet();
ad.Fill(_ds,"list");
this.dataGrid.SetDataBinding(_ds.Tables[0],"");
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}
/// <summary>
/// 在网格区按下某键,说明有数据变动,此时需要保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
this.change = true;
}
/// <summary>
/// 删除指定dcSign的全部项
/// </summary>
/// <param name="dcSign"></param>
private void DeleteAllItem(string dcSign){
try{
string sql = "delete from DictionaryItem where dcSign='"+ dcSign +"'";
Public.conn.Open();
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
cmd.ExecuteNonQuery();
Public.conn.Close();
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}
/// <summary>
/// 保存更新后的所有项
/// 先将数据库中当前条件下的全部删除
/// </summary>
/// <param name="dcSign"></param>
private void SaveItem(string dcSign){
try{
DataTable table = (DataTable)this.dataGrid.DataSource;
if(table != null){
this.DeleteAllItem(dcSign);
for(int i=0;i<table.Rows.Count;i++){
DataRow row = table.Rows[i];
string sql = "insert into DictionaryItem (dcSign,diName,diSign,content,adder,addDate) values ('"+ dcSign +"','"+ row["值"].ToString() +"','"+ row["编号"].ToString() +"','"+ row["备注"].ToString() +"','"+ Public.userID +"','"+ DateTime.Now.ToString() +"')";
Public.conn.Open();
OleDbCommand cmd = new OleDbCommand(sql,Public.conn);
cmd.ExecuteNonQuery();
Public.conn.Close();
}
}
}catch(Exception ex){
MessageBox.Show(ex.Message.ToString());
return;
}
}
/// <summary>
/// 说明有变动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid_CurrentCellChanged(object sender, System.EventArgs e) {
this.change = true;
this.button1.Enabled = true;
this.button2.Enabled = true;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -