Hi,
Here I will code you how to bind the values
into Gridview from database by using bound field. Bound field allows grid view
to show the specified columns. It enables developers to restrict the columns to
the grid. Also enables columns to change the header text for columns.
Output Preview :
Database Structure:
Aspx
Code:
<html>
<head runat="server">
<title>
Fourthbottle </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="Grd_BoundFeild" AutoGenerateColumns="false" runat="server">
<Columns>
<asp:BoundField HeaderText="Mobile Brand" DataField="Brand" />
<asp:BoundField HeaderText="Mobile Model" DataField="Model" />
<asp:BoundField HeaderText="Mobile Color" DataField="color" />
</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_BOUNDFEILD : 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();
}
}
}
}