Hi itsbaala,
Please use Repeater control instead of GridView Control.
Because a GridView can only display your data in a table format, but a Repeater can show your data in any way you want to display.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:Repeater ID="rptCustomers" runat="server">
<ItemTemplate>
<div>
<span>CustomerId:</span>
<asp:Label Text='<%# Eval("CustomerId") %>' runat="server" />
</div>
<div>
<span>Name:</span>
<asp:Label Text='<%# Eval("Name") %>' runat="server" />
</div>
<div>
<span>Country:</span>
<asp:Label Text='<%# Eval("Country") %>' runat="server" />
</div>
<hr />
</ItemTemplate>
</asp:Repeater>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindRepeater();
}
}
private void BindRepeater()
{
String conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerID, Name, Country FROM Customers", con))
{
cmd.CommandType = CommandType.Text;
{
con.Open();
this.rptCustomers.DataSource = cmd.ExecuteReader();
this.rptCustomers.DataBind();
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindRepeater()
End If
End Sub
Private Sub BindRepeater()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerID, Name, Country FROM Customers", con)
cmd.CommandType = CommandType.Text
If True Then
con.Open()
Me.rptCustomers.DataSource = cmd.ExecuteReader()
Me.rptCustomers.DataBind()
con.Close()
End If
End Using
End Using
End Sub
Screenshot