How to Show Grid View in ASP. NET


Grid View in ASP.NET

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

    Example:

    _img1.jpg

    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.

    _img2.jpg

    Now choose the data source and click on ok button.

    _img3.jpg

    Now click on New Connection and then click on

    _img4.jpg

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

    _img5.jpg

    _img6.jpg

    Now Click next

    _img7.jpg

    Further Click next

    _img8.jpg

    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 = your PasswordRecovery"); 
    con.Open();
    Response.Write("connect");
    }

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


Similar Articles