您的位置:首页 > 其它

DataGrid中添加删除操作

2007-05-10 16:17 375 查看
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace 练习
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
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;

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.dataGrid1 = new System.Windows.Forms.DataGrid();
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();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.dataGrid1.DataMember = "";
this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(0, 0);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(496, 168);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 184);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 24);
this.button1.TabIndex = 1;
this.button1.Text = "添加";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(136, 184);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(104, 24);
this.button2.TabIndex = 2;
this.button2.Text = "删除";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(256, 184);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 24);
this.button3.TabIndex = 3;
this.button3.Text = "修改";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(376, 184);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(104, 24);
this.button4.TabIndex = 4;
this.button4.Text = "退出";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 230);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGrid1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
System.Data.SqlClient.SqlDataAdapter da;
System.Data.SqlClient.SqlParameter par;
System.Data.DataTable dt;
data d=new data();
string sql;
public void load()
{
try
{
dt=d.select("select * from admin");
this.dataGrid1.DataSource=dt;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
load();
}

private void button1_Click(object sender, System.EventArgs e)//添加
{
try
{
sql="insert into admin values(@name,@pwd)";
da=new System.Data.SqlClient.SqlDataAdapter();
da.InsertCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);

par=da.InsertCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;

par=da.InsertCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;

da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button2_Click(object sender, System.EventArgs e)//删除
{
try
{
sql="delete from admin where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.DeleteCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);

par=da.DeleteCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;

dt.Rows[this.dataGrid1.CurrentRowIndex].Delete();

da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button3_Click(object sender, System.EventArgs e)//修改
{
try
{
sql="update admin set name=@name,pwd=@pwd where id=@id";
da=new System.Data.SqlClient.SqlDataAdapter();
da.UpdateCommand=new System.Data.SqlClient.SqlCommand(sql,data.conn);

par=da.UpdateCommand.Parameters.Add("@name",System.Data.SqlDbType.Int);
par.SourceColumn="name";
par.SourceVersion=System.Data.DataRowVersion.Current;

par=da.UpdateCommand.Parameters.Add("@pwd",System.Data.SqlDbType.Int);
par.SourceColumn="pwd";
par.SourceVersion=System.Data.DataRowVersion.Current;

par=da.UpdateCommand.Parameters.Add("@id",System.Data.SqlDbType.Int);
par.SourceColumn="id";
par.SourceVersion=System.Data.DataRowVersion.Original;

da.Update(dt);
dt.Clear();
load();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void button4_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐