📄 bminfo.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data ;
using System.Data .SqlClient ;
namespace RoomManage
{
/// <summary>
/// bminfo 的摘要说明。
/// </summary>
public class bminfo : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
DataConn dc=new DataConn ();
protected SqlDataAdapter adapter;
protected DataSet dataset;
protected DataTable table;
protected string selectString;
public bminfo()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
BindData();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <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.dataGrid1 = new System.Windows.Forms.DataGrid();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.dataGrid1);
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(352, 216);
this.groupBox1.TabIndex = 4;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "部门信息";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(3, 17);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(346, 196);
this.dataGrid1.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.button1);
this.groupBox2.Controls.Add(this.button2);
this.groupBox2.Controls.Add(this.button3);
this.groupBox2.Controls.Add(this.button4);
this.groupBox2.Location = new System.Drawing.Point(0, 216);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(352, 48);
this.groupBox2.TabIndex = 5;
this.groupBox2.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(48, 23);
this.button1.TabIndex = 2;
this.button1.Text = "添加";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(64, 16);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(48, 23);
this.button2.TabIndex = 2;
this.button2.Text = "删除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(232, 16);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(48, 23);
this.button3.TabIndex = 2;
this.button3.Text = "保存";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(288, 16);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(48, 23);
this.button4.TabIndex = 2;
this.button4.Text = "退出";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// bminfo
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(352, 269);
this.ControlBox = false;
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.groupBox2);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "bminfo";
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "部门信息维护";
this.TopMost = true;
this.Closing += new System.ComponentModel.CancelEventHandler(this.bminfo_Closing);
this.groupBox1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void BindData()
{
string connString=dc.connstr ;
SqlConnection conn=new SqlConnection(connString);
selectString="select * from tabbmdy order by 部门编号";
adapter=new SqlDataAdapter(selectString,conn);
SqlCommandBuilder builder=new SqlCommandBuilder(adapter);
adapter.DeleteCommand=builder.GetDeleteCommand();
adapter.InsertCommand=builder.GetInsertCommand();
adapter.UpdateCommand=builder.GetUpdateCommand();
dataset=new DataSet();
adapter.Fill(dataset,"tabbmdy");
table=dataset.Tables["tabbmdy"];
//创建惟一性约束
UniqueConstraint constraint = new UniqueConstraint
(new DataColumn[]{table.Columns[0]});
table.Constraints.Add(constraint);
this.dataGrid1.SetDataBinding(dataset,"tabbmdy");
}
public virtual void AddNewRow()
{
int num=table.Rows.Count+1;
DataRow newRow=table.NewRow();
while(true)
{
try
{
newRow["部门编号"]="BM"+num.ToString ("d5");
table.Rows.Add(newRow);
break;
}
catch
{
num++;
continue;
}
}
}
private void button4_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("真的要退出吗?","注意",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
this.Close ();
}
private void button3_Click(object sender, System.EventArgs e)
{
try
{
adapter.Update(dataset,"tabbmdy");
MessageBox.Show("保存成功。","恭喜");
}
catch(Exception err)
{
MessageBox.Show(err.Message,"保存信息出错!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
}
private void button1_Click(object sender, System.EventArgs e)
{
AddNewRow();
}
private void button2_Click(object sender, System.EventArgs e)
{
int num=table.Rows.Count-1;
if(num>0)
{
ArrayList ar=new ArrayList();
for(int i=num;i>=0;i--)
{
if(this.dataGrid1.IsSelected(i))
{
ar.Add(i);
}
}
if(ar.Count>0)
{
//捕获错误信息
if(MessageBox.Show("确实要删除选中的"+ar.Count.ToString()+"行吗? 注意:删除后就无法恢复了!","警告",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
foreach(int i in ar)
{
table.Rows[i].Delete();
}
}
}
else
{
MessageBox.Show("请先用鼠标点击左边灰色部分选择要删除的行(可以按住Ctrl键或者Shift键同时用鼠标选中多行),然后再删除。","提示");
}
}
}
private void bminfo_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(dataset.HasChanges())
{
if(MessageBox.Show("数据尚未保存,放弃所作的修改吗?","小心",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
{
this.Close();
e.Cancel=false; //退出
}
else
{
e.Cancel=true; //放弃退出
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -