日期:2014-05-18 浏览次数:21047 次
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace PMMIS.TEST { public partial class frmLx : Form { public frmLx() { InitializeComponent(); } SqlConnection cn; BindingSource bs; private void frmLx_Load(object sender, EventArgs e) { cn = new SqlConnection("server=10.0.0.1;database=kerno;uid=sa;pwd=sa"); cn.Open(); SqlDataAdapter da = new SqlDataAdapter("select 組編號,組名稱 from grou", cn); DataSet ds = new DataSet(); da.Fill(ds); bs = new BindingSource(); bs.DataSource = ds.Tables[0]; txtId.DataBindings.Add("text", bs, "組編號"); txtName.DataBindings.Add("text", bs, "組名稱"); bs.AddingNew += new AddingNewEventHandler(bs_AddingNew); //注册委托方法 } void bs_AddingNew(object sender, AddingNewEventArgs e) { BindingSource bstemp = (BindingSource)sender; DataView view = (DataView)bstemp.List; DataRowView row = view.AddNew(); row["組編號"] = txtId.Text; row["組名稱"] = txtName.Text; e.NewObject = row; } private void btnAdd_Click(object sender, EventArgs e) { bs.AddNew(); //自动触发上面的bs_AddingNew委托方法 } //清空文本框内容 private void btnClear_Click(object sender, EventArgs e) { txtId.Text = ""; txtName.Text = ""; } } }