Simple DataBinding to TextBox : Data Bind TextBox « Database ADO.net « C# / C Sharp






Simple DataBinding to TextBox



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form {
    public Form1() {
        InitializeComponent();
        DataSet ds = CreateDataSet();
        textBox.DataBindings.Add("Text", ds.Tables["Products"], "ProductName");
    }

    private DataSet CreateDataSet() {
        string customers = "SELECT * FROM Products";
        DataSet ds = new DataSet();

        using (SqlConnection con = new SqlConnection(ConfigurationSettings.ConnectionStrings["northwind"].ConnectionString)) {
            SqlDataAdapter da = new SqlDataAdapter(customers, con);

            da.Fill(ds, "Products");
        }

        return ds;
    }

    private void InitializeComponent() {
        this.textBox = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox
        // 
        this.textBox.Location = new System.Drawing.Point(13, 13);
        this.textBox.Name = "textBox";
        this.textBox.Size = new System.Drawing.Size(477, 20);
        this.textBox.TabIndex = 0;
        // 
        // Form1
        this.Controls.Add(this.textBox);
        this.ResumeLayout(false);
        this.PerformLayout();

    }



    private System.Windows.Forms.TextBox textBox;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.EnableRTLMirroring();
        Application.Run(new Form1());
    }
}


           
       








Related examples in the same category

1.Bind DataSet to TextBox
2.Bind String array to a TextBox
3.Bind Database table column to TextBox
4.DataView Binding to TextBox