Monday, June 2, 2008

Asp .Net GridView Editing and Paging the Wizard way

GridView is an ASP .Net 2.0 control with inbuilt editing, paging and sorting support.
In this blog I am going to demonstrate enabling paging, sorting and editing in a no code way ie. using Wizard.

Place a GridView on Asp .Net Page. From the smart tag select Choose Data Source and New Data Source.

Data Source Configuration Wizard wizard will appear. Select database and click Ok.

From choose your data connection section of Wizard select New Connection

The Add Connection dialog will appear.

In the server name you can specify a . (dot) if you want to connect to Sql Server in the same machine, .\sqlserver if you want to connect to Sql Server 2005 or any machine name if you want to connect to Sql Server on another machine.

From select or enter a data base select the database you want to connect to.
Click on Test connection.

If everything is ok Test connection succeeded message will appear.

Your will be taken back to the wizard. Click Next

Here if you want you can specify another name for the connectionstring which is an entry in web.config. Click Next.

Here you can specify the table of field. Make sure to check the advanced button.

In the Advanced Sql Generation Option Dialog select Generate INSERT , UPDATE And Delete Statements and Use Optimistic concurrency. Click Next and click Test Query. Click on Finish.

Now your grid is ready. Since we want to implement additional functionality, click on the smart tag and Select Enable Paging, Enable Sorting, Enable Editing and Enable Deleting.

You have to set EnableSortingAndPagingCallbacks to true of Ajax enabling the GridView.

Lets explore what is happening behind the scene.
Asp .Net is adding a new entry inside connectionStrings in web.config.

An SqlDataSource control is added to the form which points to the connectionString:NorthwindConnectionString. It also contains code for insertion updation and deletion.

GridView's DataSource id is set to the SqlDataSouce control.

Happy Programming

This blog was posting in response to a request by Madhu. If you have any .Net doubts i will try to blog on that.


  1. Sir,

    I found the following issue arising in this context

    I created a table called Students
    having fields:
    Name(nvarchar) and Code(nvarchar)

    I made the Code field as "allow null"

    I entered 5 rows(name,code) in which the fifth row did not have any value for code.

    while tring to delete/edit the fifth row it was noted that no action has been taken

    Is there any method (by using wizard)to solve this problem?

  2. when i clicked on edit button every column of gridview is resized.. can u tell me how to fix the size of columns