dataGrid更新数据
来源:互联网 发布:淘宝运营总监招聘 编辑:程序博客网 时间:2024/04/29 03:03
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private DataSet myDs;
private SqlDataAdapter myDa;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button updt;
private System.Windows.Forms.Button conndb;
/// <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.updt = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.conndb = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// updt
//
this.updt.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.updt.Location = new System.Drawing.Point(344, 328);
this.updt.Name = "updt";
this.updt.Size = new System.Drawing.Size(200, 23);
this.updt.TabIndex = 0;
this.updt.Text = "Update to DataBase";
this.updt.Click += new System.EventHandler(this.updt_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
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(590, 312);
this.dataGrid1.TabIndex = 1;
//
// conndb
//
this.conndb.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
this.conndb.Location = new System.Drawing.Point(88, 328);
this.conndb.Name = "conndb";
this.conndb.Size = new System.Drawing.Size(168, 23);
this.conndb.TabIndex = 2;
this.conndb.Text = "Connection DataBase";
this.conndb.Click += new System.EventHandler(this.conndb_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(592, 366);
this.Controls.Add(this.conndb);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.updt);
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 updt_Click(object sender, System.EventArgs e)
{
try
{
SqlCommandBuilder cb = new SqlCommandBuilder();
cb.DataAdapter = myDa;
myDa.UpdateCommand = cb.GetUpdateCommand();
myDa.DeleteCommand = cb.GetDeleteCommand();
myDa.InsertCommand = cb.GetInsertCommand();
myDa.Update(myDs.Tables["tab"]);
MessageBox.Show("Database updated!");
}
catch (Exception eupdt)
{
MessageBox.Show("没有更新成功,具体原因是:/n"+eupdt.Message);
}
}
private void conndb_Click(object sender, System.EventArgs e)
{
SqlConnection conn= new SqlConnection();
string connstr = "initial catalog = Northwind; data source = 127.0.0.1; uid= sa; pwd=";
conn.ConnectionString = connstr;
try
{
conn.Open();
SqlCommand cmd= new SqlCommand();
cmd.CommandText = "select * from customers";
cmd.Connection = conn;
myDa = new SqlDataAdapter();
myDa.SelectCommand = cmd;
myDs = new DataSet();
myDa.Fill(myDs,"tab");
dataGrid1.DataSource = myDs;
dataGrid1.DataMember="tab";
//dataGrid1.DataBind(); //去掉即可,
}
catch ( Exception econn)
{
MessageBox.Show("数据库连接错误(0)",econn.Message);
}
finally
{
conn.Close();
}
}
}
}
- dataGrid更新数据
- DataGrid更新数据
- 一次更新Datagrid 中多列数据
- 一次更新Datagrid 中多列数据
- 遍历dataGrid行更新数据
- 一次更新Datagrid 中多列数据
- datagrid更新数据。取得textbox中的数据
- DataGrid更新不了数据的问题!
- vb.net2003中,利用datagrid更新数据(原创)
- 使用单击和双击更新DataGrid中的数据的例子
- 使用单击和双击更新DataGrid中的数据的例子
- 使用单击和双击更新DataGrid中的数据的例子
- DataGrid动态模板列更新数据并且分页的例子
- 批量更新数据引起 DataGrid 的绘制错误及解决方法
- DataGrid更新数据时的一系列问题的解决
- Silverlight 解决 更新数据后 datagrid同步问题
- jQuery Ajax EasyUI周期性更新datagrid的数据
- WPF DataGrid 绑定数据及时更新的处理
- 我是一个新手,想向各位请教面向对象和面向过程的区别
- 唱歌好啊唱歌妙
- KOL+DUNIT+WANT+PASDOC进行TDD开发
- Linux 学习基础
- SOA FAQ
- dataGrid更新数据
- 有力的十种情绪 -- 安东尼.罗宾斯
- 用JSP+Javabean+Servlet实现一个投票系统
- 很好的虚拟机软件
- Classworking 工具箱: 注释(Annotation)与 ASM
- javascript技巧大全
- 畫圖保存核心代碼
- CVS完全手册
- 如何创建数据库Web Services