Click here to Skip to main content
11,649,678 members (76,705 online)
Click here to Skip to main content

Displaying Data in Gridview

, 3 Jul 2010 CPOL 28.5K 418 7
Rate this:
Please Sign up or sign in to vote.
An article on how to display data in gridview

Difference between GridView and DataGrid

DataGrid GridView
Sorting, and in-place editing of data requires additional coding. It enables you to add sorting, paging and editing features without writing any code. We can enable these features by setting properties on the control.
The DataGridcontrol can declaratively bind a DataSourceControlcontrol, it can do so only for data selection. Sorting, paging, updates, and deletions must be written manually. The GridViewcontrol supports a DataSourceIDproperty that can be set to any object that implements the IDataSourceinterface and can, therefore, take advantage of the data source control's sorting, paging, updating, and deleting capabilities, for example, the SqlDataSourcecontrol Control.
The DataGridcontrol requires additional coding for paging. The GridViewcontrol automatically supports paging by setting the PagerSettingsproperty.
AllowCustomPagingproperty must be set to truefor custom paging. Custom paging support in the GridViewis supplied by the bound data source control.
It raises single events for operations such as sorting, updating. Gridview raises pre-operation and post-operation events such as sorted, sorting events for sort command, updating and updated events for Update command.

This article explains how to display data in the GridViewfrom the Database Table Emp. Let us first assume that we have a database Table Emp with columns EmpId, EmpName, EmpSalin the database CompanyDB.

Requirement

To display the Employeedetails from the Emptable in the GridView.

Step 1: Creating a new Website: GridViewDemo

  1. Open Visual Web Developer or Visual Studio 2008.
  2. On the File menu, click New Website. In the Dialog box that appears under Visual Studio Installed templates, select ASP.NET Website.
  3. Then type the location (Foldername) where you want to create your website such as D:\GridViewDemo in the second location box.
  4. Select the language you want to create a website in. Let us select C#. 
  5. Click OK. This will create a website with name GridViewDemo. This website by default contains a Webpage named Default.aspx.

Step 2: Adding GridViewControlto display data

  1. Open the Default.aspx page.
  2. Go to View Menu -> Select Toolbox -> From the Toolbox window -> Under Data section, double click on Gridviewor Drag and Drop the GridViewfrom the Toolbox onto the Default.aspx page.
  3. In Visual Studio /Visual WebDeveloper, switch to Design view. Right click the GridViewcontrol and then click on ShowSmartTag -> This displays a small button with an arrow (>) on the extreme right-top of the Gridview(SmartTag).
  4. Select the smart tag, this will open the Gridviewtasks menu, in the choose datasource list click the dropdown and select New Datasource. The Data Source configuration dialog box appears.
  5. Click Database->This specifies that you want to fetch data from database. In the Specify an ID for the data source textbox, a default data source control name appears (SqlDataSource1).
  6. Click Ok.

    image001.jpg

  7. The Configure Data Source Wizard appears, which displays a page on which you can choose a connection to the database.
  8. Click New connection. In the Add Connection Window, change the Data Source to Microsoft SQL Server.
  9. The servername should be database server name. If it is the localsystemon which you are trying to connect to the database, usually it is .\SQLEXPRESS where . (dot)here represents the current machine name. SQLEXPRESS is the database server instance name.
  10. Tip: How to find the Database server Instance name: (StartMenu->All Programs->Administrative Tools->Service->In the services window, search for service with the name SQL Server. Beside this, you will find Database Server instance name in parenthesis (generally it is SQL EXPRESS). This name is the Server name which has to be specified in the above Add connection window after “.\
  11. Let the Windows authentication be selected for log in and connecting to the database or if required you can type the username and password. Then select the database from which you want to display the data.
  12. The Emptable is present in CompanyDBdatabase, so I selected CompanyDBhere.

    image002.jpg

  13. Click Next.
  14. The wizard appears, displaying a page on which you can choose to store the connection string in the configuration file.
  15. Click Next again.
  16. Under Specify columns from a table or view, in the Name list, click Emp.

    image003.jpg

  17. Under Columns, select the EmpId, EmpName, EmpSalcheckboxes.
  18. This window also displays the SQL statement that you are creating in a box at the bottom of the window.
  19. Click Next and then Click Test Query to make sure that you are fetching the data you want.
  20. Click on finish.
  21. Press CTRL + F5 to execute the Default page.
  22. The Webpage (Default.aspx page) appears in the webbrowser which shows the Gridviewdisplaying all the datarows from the Emptable of CompanyDBdatabase.

Conclusion

In this article, we have seen how to fetch selected data from the SQLSERVERdatabase and display in the GridView.

History

  • 3rd July, 2010: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shining Legend
Software Developer
India India
I am a Software Developer in Dotnet technologies. I also provide online training in dotnet technologies.

You may also be interested in...

Comments and Discussions

 
General[My vote of 1] Good joke Pin
Sunasara Imdadhusen25-Mar-11 3:55
memberSunasara Imdadhusen25-Mar-11 3:55 
GeneralMy vote of 1 Pin
Member 32185757-Jul-10 9:20
memberMember 32185757-Jul-10 9:20 
GeneralMy vote of 1 Pin
alijoongolgoli5-Jul-10 17:34
memberalijoongolgoli5-Jul-10 17:34 
GeneralRe: My vote of 1 Pin
Brian Grant6-Jul-10 8:37
memberBrian Grant6-Jul-10 8:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150804.4 | Last Updated 3 Jul 2010
Article Copyright 2010 by Shining Legend
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid