📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.Button button6;
private System.Windows.Forms.Button button7;
private System.Windows.Forms.Button button8;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// 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.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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button5 = new System.Windows.Forms.Button();
this.button6 = new System.Windows.Forms.Button();
this.button7 = new System.Windows.Forms.Button();
this.button8 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 216);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "添加";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(168, 216);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "修改";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(80, 256);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
this.button3.Text = "删除";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(168, 256);
this.button4.Name = "button4";
this.button4.TabIndex = 3;
this.button4.Text = "关闭";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.TabIndex = 4;
this.label1.Text = "姓名";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 96);
this.label2.Name = "label2";
this.label2.TabIndex = 5;
this.label2.Text = "年龄";
//
// label3
//
this.label3.Location = new System.Drawing.Point(16, 160);
this.label3.Name = "label3";
this.label3.TabIndex = 6;
this.label3.Text = "单位";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(160, 32);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 7;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(160, 96);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 8;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(160, 152);
this.textBox3.Name = "textBox3";
this.textBox3.TabIndex = 9;
this.textBox3.Text = "";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(296, 32);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(352, 208);
this.dataGrid1.TabIndex = 10;
//
// button5
//
this.button5.Location = new System.Drawing.Point(296, 248);
this.button5.Name = "button5";
this.button5.TabIndex = 11;
this.button5.Text = "加载数据";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// button6
//
this.button6.Location = new System.Drawing.Point(376, 248);
this.button6.Name = "button6";
this.button6.TabIndex = 12;
this.button6.Text = "插入";
this.button6.Click += new System.EventHandler(this.button6_Click);
//
// button7
//
this.button7.Location = new System.Drawing.Point(464, 248);
this.button7.Name = "button7";
this.button7.TabIndex = 13;
this.button7.Text = "修改";
this.button7.Click += new System.EventHandler(this.button7_Click);
//
// button8
//
this.button8.Location = new System.Drawing.Point(544, 248);
this.button8.Name = "button8";
this.button8.TabIndex = 14;
this.button8.Text = "删除";
this.button8.Click += new System.EventHandler(this.button8_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 302);
this.Controls.Add(this.button8);
this.Controls.Add(this.button7);
this.Controls.Add(this.button6);
this.Controls.Add(this.button5);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
Application.Exit();
}
public void Fullsh()
{
this.textBox1.Text="";
this.textBox2.Text="";
this.textBox3.Text="";
}
private void button1_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
DataRow row=ds.Tables["t_user"].NewRow();
row["uname"]=this.textBox1.Text;
row["uage"]=int.Parse(this.textBox2.Text);
row["ustat"]=this.textBox3.Text;
ds.Tables[0].Rows.Add(row);
d.InsertUser(ds);
this.Fullsh();
}
private void button2_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
string uname=this.textBox1.Text;
ds.Tables[0].DefaultView.RowFilter="uname='"+uname+"'";
if(ds.Tables[0].DefaultView.Count>0)
{
ds.Tables[0].DefaultView[0]["uage"]=int.Parse(this.textBox2.Text);
ds.Tables[0].DefaultView[0]["ustat"]=this.textBox3.Text;
}
d.UpdateUser(ds);
this.Fullsh();
}
private void button3_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
string uname=this.textBox1.Text;
ds.Tables[0].DefaultView.RowFilter="uname='"+uname+"'";
if(ds.Tables[0].DefaultView.Count>0)
{
ds.Tables[0].DefaultView[0].Delete();
}
d.DeleteUser(ds);
this.Fullsh();
}
public void DataLoad()
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
ds=(DataSet)this.dataGrid1.DataSource;
this.dataGrid1.DataMember=ds.Tables[0].TableName;
}
private void button5_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
this.dataGrid1.DataSource=ds;
this.DataLoad();
}
private void button6_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
d.InsertUser((DataSet)this.dataGrid1.DataSource);
this.DataLoad();
}
private void button7_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
d.UpdateUser((DataSet)this.dataGrid1.DataSource);
this.DataLoad();
}
private void button8_Click(object sender, System.EventArgs e)
{
DataUser d=new DataUser();
DataSet ds=d.GetAllUser();
ds=(DataSet)this.dataGrid1.DataSource;
string uname=ds.Tables[0].Rows[this.dataGrid1.CurrentRowIndex][0].ToString();
ds.Tables[0].Rows[this.dataGrid1.CurrentRowIndex].Delete();
d.DeleteUser((DataSet)this.dataGrid1.DataSource);
this.DataLoad();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -