// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Windows.Forms; namespace DefaultNamespace { public class MainForm : Form { private DataGrid dg; private TextBox tb1; private TextBox tb2; private Button save; private OleDbConnection con; private OleDbDataAdapter da; private OleDbCommandBuilder cb; private DataSet ds; public static void Main() { MainForm fMainForm = new MainForm(); fMainForm.ShowDialog(); } public MainForm() { InitializeComponent(); } private void InitializeComponent() { con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Databases\\MSAccess\\Test.mdb;"); con.Open(); da = new OleDbDataAdapter("SELECT * FROM T1", con); cb = new OleDbCommandBuilder(da); da.InsertCommand = cb.GetInsertCommand(); ds = new DataSet(); da.Fill(ds, "T1"); dg = new DataGrid(); tb1 = new TextBox(); tb2 = new TextBox(); save = new Button(); SuspendLayout(); dg.Location = new Point(50, 50); dg.Size = new Size(300, 200); dg.SetDataBinding(ds, "T1"); tb1.Location = new Point(50, 300); tb1.Size = new Size(75, 25); tb2.Location = new Point(150, 300); tb2.Size = new Size(75, 25); save.Location = new Point(250, 300); save.Size = new Size(75, 25); save.Text = "Save"; save.Click += new EventHandler(SaveClick); ClientSize = new Size(400, 400); Controls.Add(dg); Controls.Add(tb1); Controls.Add(tb2); Controls.Add(save); Text = "Main Form"; ResumeLayout(false); } void SaveClick(object sender, EventArgs e) { string[] val = new string[2]; val[0] = tb1.Text; val[1] = tb2.Text; ds.Tables["T1"].Rows.Add(val); da.Update(ds, "T1"); ds.AcceptChanges(); dg.Refresh(); } } }