boutique replica bags up ideas

the best replique rolex and prices here.

julia highlight 99j hair color 10a quality straight human hair lace front wigs 8 - 24 inches pre plucked hairline 13x4 inches lace front brazilian wig onlinefor sale

Populate GridView from code behind in ASP.NET Example

Updated on     Kisan Patel

This tutorial will show you how to populate gridview from code behind in ASP.NET with example.

Default.aspx

<asp:GridView ID="GridView1" runat="server" />

In the above code, we have declared a GridView without any associated properties.

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GetData();
    }
}

private void GetData()
{
    DataTable table = new DataTable();
    // get the connection
    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    {
        // write the sql statement to execute
        string sql = "SELECT Id, FirstName, LastName, Email, IsActive FROM Students";
        // instantiate the command object to fire
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
            // get the adapter object and attach the command object to it
            using (SqlDataAdapter sdt = new SqlDataAdapter(cmd))
            {
                // fire Fill method to fetch the data and fill into DataTable
                sdt.Fill(table);
            }
        }
    }
    // specify the data source for the GridView
    GridView1.DataSource = table;
    // bind the data now
    GridView1.DataBind();
}

In the code behind, we have used ADO.NET objects to retrieve the data from the database to a DataTable and that DataTable (table variable) is being used as the DataSource for the GridView. then, we need to call the DataBind method of the GridView that actually binds the data to the GridView.

Demo

gridview-demo

Download Complete Source Code


ASP.NET

Leave a Reply