ASP.NET: GridView Control

One of the reasons why I’m impressed with asp.net’s rich set of controls is its GridView control. This control displays your data in a table structure complete with delete, update and select features. It even has paging in case you have lots of records in your database. The best thing about this paging is that everything is done for you, the links, the paging number, the paging labels and others. If you click a page link, it will retrieve the next set of record(s) for you without changing any SQL query. For example, you can use this SQL query to retrieve all records of the table.

In MySQL, you would need to supply the LIMIT keyword in order to get a specific set of rows. With GridView’s paging feature enabled, everything is done for you. Neat huh. Took me like a day in getting this one to work though because I had problems with the update and delete function. Since I am using MySQL, turns out when you do parameters in your query commands, the @ sign does not work because it is only for MS SQL. You would have to change the @ to ?.

Take this sample asp.net code for a GridView control as an example. This assumes you have a working database connection to MySQL. If you do not know how to connect to a MySQL in asp.net, go here to use the custom MySQLProvider classes by Rakotomalala Andriniaina.

If you read e-books regarding asp.net, the queries used are for MS SQL. It’s been a long time since I dabbled with MS SQL, so when I read about it, I thought the @ sign for parameters were required for any database used. Took me some time to know that ? has to be used. See the DeleteCommand and UpdateCommand properties of the SqlDataSource tag.

How you design your GridView control is up to you. With styles, you can override how the table would look like. The other GridView like control that I had used before that looks like asp.net’s version is the datagrid taglib for Java. The downside with using the taglib is that if your SQL query returns lots of rows, it may not load fast and may use up resources in doing so because it gets all records at one time. Asp.net’s GridView control totally impressed me in more ways.

Related Posts Plugin for WordPress, Blogger...

6 comments

  1. echilon, i will get back to you tomorrow. i did not bring my laptop today. the paging links are automatically done by the component which what got me impressed with it.

    when you say changing the page, as in clicking on the next link gives you zero results?

    are you also using MySQL?

  2. The problem was I was adding columns dynamically, which throws a humungous spanner in the works because you have to add the columns and rebind on every postback.

  3. echilon, i see. then my code is fine. i have not tried adding columns dynamically. the code in this post is for page load only. i probably may write a post regarding that if i come across the same situation are what you are trying to do now

  4. When working with AJAX to create a better UI, I like using javascript controls instead of .Net server controls inside update panels.
    The Asp.Net GridView is great for normal server-side generated pages but it’s not right for client side UI.
    I needed a control that allows creating the grid on the client side and supports client side sorting and column drag & drop.

    I found this control which does the basic sortable table with drag and drop but it missed some key features.

  5. which control zk? you did not include the link. you are right though, the controls are pretty heavy for client side use. not all people have fast PCs as well.

Leave a Reply

Your email address will not be published. Required fields are marked *