Hi nabilabolo,
Refer below sample code.
Database
For this example I have used Customers table of Microsoft Northwind Database. You can download the database using the link below.
Download Northwind Database
HTML
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string[] countries = new[] { "Sweden", "Mexico", "UK" };
List<DetailModel> customersList = new List<DetailModel>();
foreach (string country in countries)
{
NorthwindEntities db = new NorthwindEntities();
List<DetailModel> customers = (from c in db.Customers
where c.Country == country
select new DetailModel
{
Name = c.ContactName,
City = c.City,
Country = c.Country
}).ToList();
foreach (DetailModel customer in customers)
{
customersList.Add(customer);
}
}
gvCustomers.DataSource = customersList;
gvCustomers.DataBind();
}
}
public class DetailModel
{
public string Name { get; set; }
public string City { 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 IsPostBack Then
Dim countries As String() = {"Sweden", "Mexico", "UK"}
Dim customersList As List(Of DetailModel) = New List(Of DetailModel)()
For Each country As String In countries
Dim db As NorthwindEntities = New NorthwindEntities()
Dim customers As List(Of DetailModel) = (From c In db.Customers
Where c.Country = country
Select New DetailModel With {
.Name = c.ContactName,
.City = c.City,
.Country = c.Country
}).ToList()
For Each customer As DetailModel In customers
customersList.Add(customer)
Next
Next
gvCustomers.DataSource = customersList
gvCustomers.DataBind()
End If
End Sub
Public Class DetailModel
Public Property Name As String
Public Property City As String
Public Property Country As String
End Class
Screenshot