We're frequently asked about the easiest way to bind the WinForms data grid control to a data source. When you install VIBlend WinForms Controls the setup installs a folder with several fully functional demo projects in VB.NET and C#. You can learn a lot by browsing through this code base.
In any case here's a very simple data binding example where we create a List of objects and use it as a data source:
C# Example:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VIBlend.WinForms.Controls;
using VIBlend.WinForms.DataGridView;
using VIBlend.Utilities;
namespace DataBindingDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<Employee> list = new List<Employee>();
for (int i = 0; i < 100; i++)
list.Add(new Employee("Name " + i, "Title " + i, "Phone " + i, "CellPhone " + i, "Address " + i));
dataGrid.VIBlendTheme = VIBlend.Utilities.VIBLEND_THEME.OFFICEBLACK;
dataGrid.BoundFields.Add(new BoundField("Employee Name", "Name"));
dataGrid.BoundFields.Add(new BoundField("Employee Title", "Title"));
dataGrid.BoundFields.Add(new BoundField("Employee Phone", "Phone"));
dataGrid.BoundFields.Add(new BoundField("Employee Cell Phone", "CellPhone"));
dataGrid.BoundFields.Add(new BoundField("Employee Address", "Address"));
dataGrid.DataSource = list;
dataGrid.DataBind();
dataGrid.RowsHierarchy.AutoResize();
dataGrid.ColumnsHierarchy.AutoResize();
dataGrid.Refresh();
}
}
class Employee
{
public Employee(string Name, string Title, string Phone, string CellPhone, string Address)
{
this.Name = Name;
this.Title = Title;
this.Phone = Phone;
this.CellPhone = CellPhone;
this.Address = Address;
}
#region Private Members
private string _name;
private string _title;
private string _phone;
private string _cellphone;
private string _address;
#endregion
#region Properties
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
public string CellPhone
{
get { return _cellphone; }
set { _cellphone = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
#endregion
}
}
VB.NET Example:
Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports VIBlend.WinForms.Controls
Imports VIBlend.WinForms.DataGridView
Imports VIBlend.Utilities
Namespace DataBindingDemo
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim list As New List(Of Employee)()
For i As Integer = 0 To 99
list.Add(New Employee("Name " & i, "Title " & i, "Phone " & i, "CellPhone " & i, "Address " & i))
Next i
dataGrid.VIBlendTheme = VIBlend.Utilities.VIBLEND_THEME.OFFICEBLACK
dataGrid.BoundFields.Add(New BoundField("Employee Name", "Name"))
dataGrid.BoundFields.Add(New BoundField("Employee Title", "Title"))
dataGrid.BoundFields.Add(New BoundField("Employee Phone", "Phone"))
dataGrid.BoundFields.Add(New BoundField("Employee Cell Phone", "CellPhone"))
dataGrid.BoundFields.Add(New BoundField("Employee Address", "Address"))
dataGrid.DataSource = list
dataGrid.DataBind()
dataGrid.RowsHierarchy.AutoResize()
dataGrid.ColumnsHierarchy.AutoResize()
dataGrid.Refresh()
End Sub
End Class
Friend Class Employee
Public Sub New(ByVal Name As String, ByVal Title As String, ByVal Phone As String, ByVal CellPhone As String, ByVal Address As String)
Me.Name = Name
Me.Title = Title
Me.Phone = Phone
Me.CellPhone = CellPhone
Me.Address = Address
End Sub
#Region "Private Members"
Private _name As String
Private _title As String
Private _phone As String
Private _cellphone As String
Private _address As String
#End Region
#Region "Properties"
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Public Property Phone() As String
Get
Return _phone
End Get
Set(ByVal value As String)
_phone = value
End Set
End Property
Public Property CellPhone() As String
Get
Return _cellphone
End Get
Set(ByVal value As String)
_cellphone = value
End Set
End Property
Public Property Address() As String
Get
Return _address
End Get
Set(ByVal value As String)
_address = value
End Set
End Property
#End Region
End Class
End Namespace