Hi,
Here I have coded you how to bind the data into Gridview using template field. Template field allows to use controls inside the Gridview. Two main attributes of template field is header template and item template. Header template allows to add column header text and item template allows to add HTML or Asp controls in it.
Output Preview :
Database Structure :
Here I have coded you how to bind the data into Gridview using template field. Template field allows to use controls inside the Gridview. Two main attributes of template field is header template and item template. Header template allows to add column header text and item template allows to add HTML or Asp controls in it.
Output Preview :
Gridview Using Templatefeild |
Aspx
Code:
<html>
<head id="Head1" runat="server">
<title>
Fourthbottle </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Grd_BoundFeild" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("Brand")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("Model")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Color">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("color")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%#Eval("Price")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Label id="lblstatus" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
C# CODE:
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
using System.Data;
using
System.Data.SqlClient;
namespace GRD
{
public partial class GRD_tempfeild : System.Web.UI.Page
{
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
BindGrid();
}
}
protected
void BindGrid()
{
SqlConnection
con = new SqlConnection("your connection string ");
string
Query = "select * from Table1";
SqlDataAdapter
da = new SqlDataAdapter(Query,
con);
DataSet
DS = new DataSet();
try
{
con.Open();
da.Fill(DS);
Grd_BoundFeild.DataSource = DS;
Grd_BoundFeild.DataBind();
con.Close();
}
catch
(Exception es)
{
lblstatus.Text =
es.Message.ToString();
}
}
}
}