Search

Sunday, March 6, 2016
0 comments

GridView BoundField Example in ASP.NET C#

11:26 PMSunday, March 6, 2016
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>

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();
    }
}

Code Generates the following output:

GridView BoundField Example in ASP.NET C#
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top