Hey satabeach,
Please refer below sample.
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
public Form1()
{
InitializeComponent();
DataTable dt = BindGrid();
this.dataGridView1.DataSource = dt;
dataGridView1.Columns["Name"].HeaderText = "CustomerName";
}
private DataTable BindGrid()
{
using (DataTable dt = new DataTable())
{
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
return dt;
}
}
VB.Net
Public Sub New()
InitializeComponent()
Dim dt As DataTable = BindGrid()
Me.dataGridView1.DataSource = dt
Me.dataGridView1.Columns("Name").HeaderText = "CustomerName"
End Sub
Private Function BindGrid() As DataTable
Using dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
dt.Rows.Add(1, "John Hammond", "United States")
dt.Rows.Add(2, "Mudassar Khan", "India")
dt.Rows.Add(3, "Suzanne Mathews", "France")
dt.Rows.Add(4, "Robert Schidner", "Russia")
Return dt
End Using
End Function
Screenshot