Showing posts with label template field in gridview. Show all posts
Showing posts with label template field in gridview. Show all posts

Friday, 12 April 2013

Gridview in Asp.net- Part 1

Written By:- Isha Malhotra(malhotra.isha3388@gmail.com)
If you find this article useful then leave your comment
Gridview - Part 1

Gridview is used to show the data on your Screen from database. In this article I will explain how to work with gridview. Following are some functionality which we can perform on gridview.

Show Data in Gridview

To show the data on gridview first add gridview on your form and select the data from database by using the following connectivity:-
Default.aspx.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Create connection. kindly change the conn_string according to your system

            string conn_string = "Data Source=Isha-PC;Initial Catalog=TechAltum;Integrated Security=True";
            SqlConnection con = new SqlConnection(conn_string);

            //create command object
            SqlCommand cmd = new SqlCommand();

            //pass connection and query to your command object
            cmd.Connection = con;
            cmd.CommandText = "Select * from Prod_rep";

            //create adaptor to fill data from database
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            //create datatable which holds the data
            DataTable dt = new DataTable();
            da.Fill(dt);

            //bind your data to gridview
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
}