Using a BindingSource to bind a list to a DataGridView control. : DataBinding DataGrid « 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 DataGrid 
23.78.5.Using a BindingSource to bind a list to a DataGridView control.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Drawing;
using System.Data.SqlClient;
using System.Windows.Forms;

 
public class Form1 : System.Windows.Forms.Form
{
    private Button changeItemBtn = new Button();
    private DataGridView customersDataGridView = new DataGridView();

    private BindingSource customersBindingSource = new BindingSource();

    public Form1()
    {
        this.changeItemBtn.Text = "Change Item";
        this.changeItemBtn.Dock = DockStyle.Bottom;
        this.changeItemBtn.Click += new EventHandler(changeItemBtn_Click);
        this.Controls.Add(this.changeItemBtn);

        customersDataGridView.Dock = DockStyle.Top;
        this.Controls.Add(customersDataGridView);
        this.Size = new Size(800200);
        this.Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(System.Object sender, System.EventArgs e)
    {
        List<Employee> customerList = new List<Employee>();
        customerList.Add(Employee.CreateNewEmployee());
        customerList.Add(Employee.CreateNewEmployee());
        customerList.Add(Employee.CreateNewEmployee());

        this.customersBindingSource.DataSource = customerList;
        this.customersDataGridView.DataSource = this.customersBindingSource;
    }
    void changeItemBtn_Click(object sender, EventArgs e)
    {
        List<Employee> customerList = this.customersBindingSource.DataSource as List<Employee>;
        customerList[0].CompanyName = "Abc";
        this.customersBindingSource.ResetItem(0);
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

public class Employee
{
    private Guid idValue = Guid.NewGuid();
    private string employeeName = String.Empty;
    private string companyNameValue = String.Empty;
    private string phoneNumberValue = String.Empty;

    private Employee()
    {
        employeeName = "no data";
        companyNameValue = "no data";
        phoneNumberValue = "no data";
    }

    public static Employee CreateNewEmployee()
    {
        return new Employee();
    }

    public Guid ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string CompanyName
    {
        get
        {
            return this.companyNameValue;
        }

        set
        {
            this.companyNameValue = value;
        }
    }

    public string PhoneNumber
    {
        get
        {
            return this.phoneNumberValue;
        }

        set
        {
            this.phoneNumberValue = value;
        }
    }
}
23.78.DataBinding DataGrid
23.78.1.Data binding to a programatically created dataset to a DataGridData binding to a programatically created dataset to a DataGrid
23.78.2.Table Create/Row DeleteTable Create/Row Delete
23.78.3.Binding to DataGrid
23.78.4.DataReader Binding
23.78.5.Using a BindingSource to bind a list to a DataGridView control.
23.78.6.Data Binding DataGrid
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.