Reader Level:

How to Show Grid View in ASP. NET

By Dinesh Beniwal on Jul 13, 2010
In this article we will learn to show gridview in ASP.NET.

Grid View in ASP.NET

  1. Create a new website in visual studio 2005/2008
  3. Select a file location and file name and language.
  4. Create some text boxes with name in web form



    Now drag a grid view in web form and click the edit column property  show in figure and add the item inside the grid view .You can also change it format by using property Auto Format.


    Now choose the data source and click on ok button.


    Now click on New Connection and then click on


    Choose either Window or server authentication, server name and attach the database. Click on ok button



    Now Click next


    Further Click next


    Click next and test Query and click finish

     Now use the Defalut.aspx.cs page and add the following 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.SqlClient;
    public partial class _Default : System.Web.UI.Page

    SqlConnection con;
    SqlCommand cmd
    string str;
    SqlDataAdapter ad;

    protected void Page_Load(object sender,EventArgs e)
    con = new SqlConnection ("Data Source =server name;Initial Catalog=Database Name";User ID= User;Password = yourPasswordRecovery"); 

    protected void Button1_Click(object sender, EventArgs e)
    str = "select * from empinfo where name ='"+TextBox1.Text+"' ,address = '"+TextBox2.Text+"',email_id ='"+TextBox3.Text+"'";
    ad =new SqlDataAdapter (str,con);
    GridView1.DataSource =ds.tables [empinfo].defaultview;
    GridView1.DataMember ="empinfo";
    GridView1 .DataBind ();

Dinesh Beniwal
Dinesh Beniwal

I am working as VP Content Manager, responsible for content publishing, content development, and social relations. You can follow me on twitter @dbeniwal21