Hi Ramco,
Please refer below sample.
HTML
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.gvCustomers.DataSource = GetData();
this.gvCustomers.DataBind();
}
}
private DataTable GetData()
{
Customer[] customers = new Customer[]
{
new Customer{CustomerId = 1, Name = "John Hamoond",Country="United States"},
new Customer{CustomerId = 2, Name = "Mudassar Khan",Country="India"},
new Customer{CustomerId = 3, Name = "Suzanne Mathews",Country="France"},
new Customer{CustomerId = 4, Name = "Robert Schidner",Country="Russia"}
};
DataTable dt = new DataTable();
dt.Columns.Add("CustomerId");
dt.Columns.Add("Name");
dt.Columns.Add("Country");
IEnumerable<DataRow> query = from i in customers
select dt.Rows.Add(i.CustomerId, i.Name, i.Country);
return query.CopyToDataTable();
}
public class Customer
{
public int CustomerId { get; set; }
public string Name { get; set; }
public string Country { get; set; }
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.gvCustomers.DataSource = GetData()
Me.gvCustomers.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim customers As Customer() = New Customer() {New Customer With {
.CustomerId = 1,
.Name = "John Hamoond",
.Country = "United States"
}, New Customer With {
.CustomerId = 2,
.Name = "Mudassar Khan",
.Country = "India"
}, New Customer With {
.CustomerId = 3,
.Name = "Suzanne Mathews",
.Country = "France"
}, New Customer With {
.CustomerId = 4,
.Name = "Robert Schidner",
.Country = "Russia"
}}
Dim dt As DataTable = New DataTable()
dt.Columns.Add("CustomerId")
dt.Columns.Add("Name")
dt.Columns.Add("Country")
Dim query As IEnumerable(Of DataRow) = From i In customers Select dt.Rows.Add(i.CustomerId, i.Name, i.Country)
Return query.CopyToDataTable()
End Function
Public Class Customer
Public Property CustomerId As Integer
Public Property Name As String
Public Property Country As String
End Class
Screenshot