Using a BindingSource to bind a list to a DataGridView control. : DataBinding DataGrid « GUI Windows Forms « C# / CSharp Tutorial






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(800, 200);
        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