Data binding to a programatically created dataset : DataBinding Label « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » DataBinding Label 
23.80.1.Data binding to a programatically created dataset
using System;
using System.Drawing;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Windows.Forms;

public class DataBindingDataSet : System.Windows.Forms.Form
{
    private Label lbl_first= new Label();
    private Label lbl_name= new Label();
    private Label lbl_title= new Label();
    private Label lbl_company= new Label();
    private Label lbl_phone = new Label();
    private TextBox FirstName = new TextBox();
    private TextBox SurName = new TextBox();
    private TextBox Title = new TextBox();
    private TextBox Company = new TextBox();
    private TextBox Phone = new TextBox();
    private Button btnNext = new Button();
    private Button btnPrev = new Button();
    private Button btnNew = new Button();
    private Button btnEnd = new Button();
    private DataSet dataset = new DataSet("ContactData");

    private void OnPrev(Object sender,EventArgs e)
    {
    this.BindingContext[dataset.Tables["Contacts"]].Position--;
    }

    private void OnNext(Object sender,EventArgs e)
    {
    this.BindingContext[dataset.Tables["Contacts"]].Position++;
    }

    private void OnNew(Object sender, EventArgs e)
    {
        NewEntry();
    }

    private void OnEnd(Object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void MoveToEnd()
    {
    this.BindingContext[dataset.Tables["Contacts"]].Position=dataset.Tables["Contacts"].Rows.Count-1;
    }

    private void NewEntry()
    {
        DataRow row = dataset.Tables["Contacts"].NewRow();
        row["First"]="Blank";
        row["Name"]="default name";
        row["Company"]="default company";
        row["Title"]="no title";
        row["Phone"]="999-999-9999";
        dataset.Tables[0].Rows.Add(row);
        dataset.AcceptChanges();
        MoveToEnd();
    }

    public DataBindingDataSet()
    {
    this.AutoScaleBaseSize = new System.Drawing.Size (513);
    this.ClientSize = new System.Drawing.Size (250200);
    this.FormBorderStyle = FormBorderStyle.Fixed3D;

        lbl_first.Text="First name";
        lbl_first.Location = new Point(5,5);
        lbl_first.Size = new Size(120,28);
        lbl_first.Anchor = AnchorStyles.Left | AnchorStyles.Right;
        Controls.Add(lbl_first);

        FirstName.Location = new Point(125,5);
        FirstName.Size = new Size(120,28);
        FirstName.Anchor = AnchorStyles.Left | AnchorStyles.Right;
        Controls.Add(FirstName);

        lbl_name.Text="Surname";
        lbl_name.Location = new Point(5,35);
        lbl_name.Size = new Size(120,28);
        lbl_name.Anchor = AnchorStyles.Left|AnchorStyles.Right;
        Controls.Add(lbl_name);

        SurName.Location = new Point(125,35);
        SurName.Size = new Size(120,28);
        SurName.Anchor = AnchorStyles.Left | AnchorStyles.Right;
        Controls.Add(SurName);

        lbl_company.Text="Company";
        lbl_company.Location = new Point(5,65);
        lbl_company.Size = new Size(120,28);
        Controls.Add(lbl_company);

        Company.Location = new Point(125,65);
        Company.Size = new Size(120,28);
        Controls.Add(Company);

        lbl_title.Text="Title";
        lbl_title.Location = new Point(5,95);
        lbl_title.Size = new Size(120,28);
        Controls.Add(lbl_title);

        Title.Location = new Point(125,95);
        Title.Size = new Size(120,28);
        Controls.Add(Title);

        lbl_phone.Text="Telephone";
        lbl_phone.Location = new Point(5,125);
        lbl_phone.Size = new Size(120,28);
        Controls.Add(lbl_phone);

        Phone.Location = new Point(125,125);
        Phone.Size = new Size(120,28);
        Controls.Add(Phone);

        btnNew.Location = new Point(5,155);
        btnNew.Size = new Size(70,28);
        btnNew.Text="New";
        btnNew.Click+=new EventHandler(OnNew);
        Controls.Add(btnNew);

        btnPrev.Location = new Point(80,155);
        btnPrev.Size = new Size(35,28);
        btnPrev.Text="<<";
        btnPrev.Click += new EventHandler(OnPrev);
        Controls.Add(btnPrev);

        btnEnd.Location = new Point(120,155);
        btnEnd.Size = new Size(70,28);
        btnEnd.Text="End";
        btnEnd.Click += new EventHandler(OnEnd);
        Controls.Add(btnEnd);

        btnNext.Location = new Point(200,155);
        btnNext.Size = new Size(35,28);
        btnNext.Text=">>";
        btnNext.Click += new EventHandler(OnNext);
        Controls.Add(btnNext);

        DataTable t=new DataTable("Contacts");
        t.Columns.Add("First",typeof(System.String));
        t.Columns.Add("Name",typeof(System.String));
        t.Columns.Add("Company",typeof(System.String));
        t.Columns.Add("Title",typeof(System.String));
        t.Columns.Add("Phone",typeof(System.String));
        t.MinimumCapacity=100;
        dataset.Tables.Add(t);

        NewEntry();

        FirstName.DataBindings.Add("Text",dataset.Tables["Contacts"],"First");
        SurName.DataBindings.Add("Text",dataset.Tables["Contacts"],"Name");
        Title.DataBindings.Add("Text",dataset.Tables["Contacts"],"Title");
        Company.DataBindings.Add("Text",dataset.Tables["Contacts"],"Company");
        Phone.DataBindings.Add("Text",dataset.Tables["Contacts"],"Phone");
    }

    static void Main()
    {
        Application.Run(new DataBindingDataSet());
    }
}
23.80.DataBinding Label
23.80.1.Data binding to a programatically created dataset
23.80.2.Bind Client Size to Label
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.