In this article i will show you, How to use BoundField of GridView in ASP.NET C#. Actually, BoundField is the Default field of GridView Control, used for DataBinding purpose. In my previous article i have already use this field with SQL DataSource control. Check mentioned below link:
[Video Contain BoundField + SQLDataSource + ADO.NET]
Now, Learn, How to create BoundField manually also bind them using ADO.NET Technology with Database table. First to create a database table, insert some data into table. Prepare BoundField like this:
<asp:GridView ID="g1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Identification No" />
<asp:BoundField DataField="Name" HeaderText="Name of User" />
</Columns>
</asp:GridView>
<asp:GridView ID="g1" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Identification No" />
<asp:BoundField DataField="Name" HeaderText="Name of User" />
</Columns>
</asp:GridView>
Code Behind Code:
using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
public partial class BoundFieldExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindGrid();
}
}
private void bindGrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
con.Open();
SqlCommand cmd= new SqlCommand();
cmd.CommandText = "select * From [usertable]";
cmd.Connection =con;
SqlDataReader rd= cmd.ExecuteReader();
g1.DataSource =rd;
g1.DataBind();
}
}
using System;
using System.Web.UI;
using System.Data.SqlClient;
using System.Configuration;
public partial class BoundFieldExample : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
bindGrid();
}
}
private void bindGrid()
{
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
con.Open();
SqlCommand cmd= new SqlCommand();
cmd.CommandText = "select * From [usertable]";
cmd.Connection =con;
SqlDataReader rd= cmd.ExecuteReader();
g1.DataSource =rd;
g1.DataBind();
}
}
Code Generates the following output: