using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace databind
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.DataGrid dataGrid1;
private SqlConnection con;
private SqlDataAdapter ada;
private DataSet ds;
private System.Windows.Forms.Button btnEdit;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnNex;
private System.Windows.Forms.Button btnPre;
private System.Windows.Forms.Button btnDel;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
con = new SqlConnection("server=it003;database=pubs;uid=sa;pwd=");
con.Open();
ada = new SqlDataAdapter("select * from authors",con);
ds=new DataSet();
ada.Fill(ds,"authors");
this.dataGrid1.DataSource=ds.Tables["authors"].DefaultView;
this.textBox1.DataBindings.Add("Text",ds,"authors.au_id");
this.textBox2.DataBindings.Add("Text",ds,"authors.city");
this.checkBox1.DataBindings.Add("Checked",ds,"authors.contract");
//
// 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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.btnEdit = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnNex = new System.Windows.Forms.Button();
this.btnPre = new System.Windows.Forms.Button();
this.btnDel = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 72);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(32, 104);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(264, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(32, 168);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(352, 144);
this.dataGrid1.TabIndex = 2;
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
//
// btnEdit
//
this.btnEdit.Location = new System.Drawing.Point(192, 24);
this.btnEdit.Name = "btnEdit";
this.btnEdit.Size = new System.Drawing.Size(72, 23);
this.btnEdit.TabIndex = 3;
this.btnEdit.Text = "修改";
this.btnEdit.Click += new System.EventHandler(this.btnEdit_Click);
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(32, 136);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 5;
this.checkBox1.Text = "是否";
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnDel);
this.groupBox1.Controls.Add(this.btnPre);
this.groupBox1.Controls.Add(this.btnNex);
this.groupBox1.Controls.Add(this.btnEdit);
this.groupBox1.Location = new System.Drawing.Point(24, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(368, 64);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "控制面版";
//
// btnNex
//
this.btnNex.Location = new System.Drawing.Point(16, 24);
this.btnNex.Name = "btnNex";
this.btnNex.TabIndex = 4;
this.btnNex.Text = "下一条";
this.btnNex.Click += new System.EventHandler(this.btnNex_Click);
//
// btnPre
//
this.btnPre.Location = new System.Drawing.Point(104, 24);
this.btnPre.Name = "btnPre";
this.btnPre.TabIndex = 5;
this.btnPre.Text = "上一条";
this.btnPre.Click += new System.EventHandler(this.btnPre_Click);
//
// btnDel
//
this.btnDel.Location = new System.Drawing.Point(272, 24);
this.btnDel.Name = "btnDel";
this.btnDel.TabIndex = 6;
this.btnDel.Text = "删除";
this.btnDel.Click += new System.EventHandler(this.btnDel_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(408, 333);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "数据演示";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{
this.BindingContext[ds,"authors"].Position=this.dataGrid1.CurrentCell.RowNumber;
}
private void btnEdit_Click(object sender, System.EventArgs e)
{
try
{
this.BindingContext[ds,"authors"].EndCurrentEdit();
SqlCommandBuilder build = new SqlCommandBuilder(ada);
ada.Update(ds,"authors");
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnNex_Click(object sender, System.EventArgs e)
{
this.BindingContext[ds,"authors"].Position--;
}
private void btnPre_Click(object sender, System.EventArgs e)
{
this.BindingContext[ds,"authors"].Position++;
}
private void btnDel_Click(object sender, System.EventArgs e)
{
ds.Tables["authors"].Rows[this.BindingContext[ds,"authors"].Position].Delete();
}
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode==Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
}
}