c# datagridview 保存添加、修改、删除数据到数据库

发布时间:2010年04月12日      浏览次数:1112 次
Form1.cs 文档代码
---------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace test_datagridview_save
{
public partial class Form1 : Form
{
private SqlConnection cn;
private SqlCommandBuilder builder;
private SqlDataAdapter da;
private DataSet ds;
public Form1()
{
InitializeComponent();
//连接数据库
cn = new SqlConnection(@"server=SQL数据库服务器名或IP;uid=SQL帐号;pwd=SQL密码;database=Test;");
cn.Open();
//显示数据库表信息到Datagridview
ds = new DataSet();
SqlCommand cmd = new SqlCommand("select * from test", cn);
da = new SqlDataAdapter(cmd);
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
builder = new SqlCommandBuilder(da);
da.Fill(ds, "Test");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
//保存数据
private void btn_save_Click(object sender, EventArgs e)
{
int row = da.Update(ds, "Test");
MessageBox.Show("更新完成","操作完成");
}
}
}
---------------------------------------------------------------
Form1.Designer.cs 文档的代码
---------------------------------------------------------------
namespace test_datagridview_save
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.btn_save = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(0, 0);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.RowTemplate.Height = 23;
this.dataGridView1.Size = new System.Drawing.Size(795, 443);
this.dataGridView1.TabIndex = 0;
//
// btn_save
//
this.btn_save.Location = new System.Drawing.Point(374, 464);
this.btn_save.Name = "btn_save";
this.btn_save.Size = new System.Drawing.Size(70, 23);
this.btn_save.TabIndex = 1;
this.btn_save.Text = "保 存";
this.btn_save.UseVisualStyleBackColor = true;
this.btn_save.Click += new System.EventHandler(this.btn_save_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(795, 499);
this.Controls.Add(this.btn_save);
this.Controls.Add(this.dataGridView1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button btn_save;
}
}
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!