📄 peisong_form.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace test007
{
/// <summary>
/// peisong_Form 的摘要说明。
/// </summary>
public class peisong_Form : System.Windows.Forms.Form
{
private CurrencyManager cmAmend;
private DataSet ds = new DataSet();
private LinkDataBase link = new LinkDataBase();
private string sendTableName = "employee";
private string sendStrSQL = "SELECT * FROM employee";
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
private System.Windows.Forms.ToolBarButton toolBarButton6;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.TextBox textBox4;
private System.ComponentModel.IContainer components;
public peisong_Form()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.ds = link.SelectDataBase(sendStrSQL,sendTableName);//创建数据库连接
dataGrid1.DataSource = ds.Tables[0];
DataGridStateControl();
DataBingingsFunction();
cmAmend =( CurrencyManager )BindingContext[ds.Tables[0]];
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(manage_Form));
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label11 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label6 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton4,
this.toolBarButton6});
this.toolBar1.ButtonSize = new System.Drawing.Size(60, 35);
this.toolBar1.DropDownArrows = true;
this.toolBar1.ImageList = this.imageList1;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(672, 38);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.ImageIndex = 0;
this.toolBarButton1.Text = "新增";
this.toolBarButton1.ToolTipText = "新增";
//
// toolBarButton2
//
this.toolBarButton2.ImageIndex = 1;
this.toolBarButton2.Text = "修改";
this.toolBarButton2.ToolTipText = "修改";
//
// toolBarButton3
//
this.toolBarButton3.ImageIndex = 2;
this.toolBarButton3.Text = "删除";
this.toolBarButton3.ToolTipText = "删除";
//
// toolBarButton4
//
this.toolBarButton4.ImageIndex = 3;
this.toolBarButton4.Text = "提交";
this.toolBarButton4.ToolTipText = "提交";
//
// toolBarButton6
//
this.toolBarButton6.ImageIndex = 4;
this.toolBarButton6.Text = "退出";
this.toolBarButton6.ToolTipText = "退出";
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(8, 48);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(360, 360);
this.dataGrid1.TabIndex = 1;
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label11,
this.label10,
this.label8,
this.label7,
this.textBox4,
this.textBox3,
this.textBox2,
this.label6,
this.label4,
this.label2,
this.textBox1,
this.label1});
this.groupBox1.Location = new System.Drawing.Point(376, 40);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(296, 368);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
//
// label11
//
this.label11.Location = new System.Drawing.Point(264, 264);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(24, 24);
this.label11.TabIndex = 16;
this.label11.Text = "*";
//
// label10
//
this.label10.Location = new System.Drawing.Point(264, 192);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(24, 24);
this.label10.TabIndex = 15;
this.label10.Text = "*";
//
// label8
//
this.label8.Location = new System.Drawing.Point(264, 120);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(24, 24);
this.label8.TabIndex = 13;
this.label8.Text = "*";
//
// label7
//
this.label7.Location = new System.Drawing.Point(264, 48);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(24, 24);
this.label7.TabIndex = 12;
this.label7.Text = "*";
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(96, 264);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(160, 21);
this.textBox4.TabIndex = 10;
this.textBox4.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(96, 192);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(160, 21);
this.textBox3.TabIndex = 8;
this.textBox3.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(96, 120);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(160, 21);
this.textBox2.TabIndex = 7;
this.textBox2.Text = "";
//
// label6
//
this.label6.Location = new System.Drawing.Point(24, 264);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(64, 24);
this.label6.TabIndex = 6;
this.label6.Text = "配送城市";
//
// label4
//
this.label4.Location = new System.Drawing.Point(16, 192);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 24);
this.label4.TabIndex = 4;
this.label4.Text = "配送员密码";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 24);
this.label2.TabIndex = 2;
this.label2.Text = "配送员姓名";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 48);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(160, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 56);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(72, 24);
this.label1.TabIndex = 0;
this.label1.Text = "配送员帐号";
//
// manage_Form
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 421);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1,
this.dataGrid1,
this.toolBar1});
this.Name = "peisong_Form";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "配送员信息维护";
this.Load += new System.EventHandler(this.peisong_Form_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void DataGridStateControl()
{
DataGridTableStyle ts = new DataGridTableStyle();
ts.AlternatingBackColor = Color.LightGray;
ts.MappingName = this.ds.Tables[0].TableName;
ts.AllowSorting = false;
this.dataGrid1.TableStyles.Add(ts);
}
private void DataBingingsFunction()
{
this.textBox1.DataBindings.Add("Text",ds.Tables[0],"配送员帐号");
this.textBox2.DataBindings.Add("Text",ds.Tables[0],"配送员姓名");
this.textBox3.DataBindings.Add("Text",ds.Tables[0],"配送员密码");
this.textBox4.DataBindings.Add("Text",ds.Tables[0],"配送城市");
}
private void peisong_Form_Load(object sender, System.EventArgs e)
{
}
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button.ToolTipText=="退出")
{
this.Close();
}
if (e.Button.ToolTipText == "新增")
{
cmAmend.AddNew();
return;
}
if (e.Button.ToolTipText == "删除")
{
if (MessageBox.Show(" 确实要删除这条记录吗?","询问",MessageBoxButtons.YesNo) == DialogResult.Yes)
{
try
{
if (cmAmend.Count > 0)
cmAmend.RemoveAt(cmAmend.Position);
else
{
MessageBox.Show("没有可以删除的数据","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
return;
}
}
if (e.Button.ToolTipText == "修改")//保存对数据所作的修改
{
try
{
int row = this.dataGrid1.CurrentCell.RowNumber;
//将当前所在行往下移动一行,为便于实现同步保存
//(否则会出现修改的最后一行无法保存,因为它还处于编辑状态)
this.dataGrid1.CurrentCell = new DataGridCell(row+1,0);
if (this.ds.HasChanges())
{
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
MessageBox.Show("数据修改成功!","信息");
}
else
{
MessageBox.Show("没有需要修改的数据!");
return;
}
}
catch
{
MessageBox.Show("数据保存失败,请确认所有信息输入完整且正确!","提示");
return;
}
}
if (e.Button.ToolTipText == "提交")
{
if (this.textBox1.Text.Trim() == ""||this.textBox2.Text.Trim()==""||this.textBox3.Text.Trim()==""||this.textBox4.Text.Trim()=="")//检查不能为空的字段
{
MessageBox.Show("带*号的栏目不能为空!","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
cmAmend.EndCurrentEdit();//结束当前编辑操作并提交修改
if (ds.GetChanges() != null)
{
try
{
this.link.UpdateDataBase(this.ds.GetChanges(),sendTableName);
}
catch(Exception express)
{
MessageBox.Show(express.ToString(),"提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
this.ds.Tables [0].RejectChanges();
}
}
return;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -