GridView, and what it is eaten with

    In this topic, I will talk about one great ASP.NET GridView control. Many newcomers, I think, often encounter problems in using this control. I also had problems. I note that it makes little sense to use this control alone. It shows all its power when used with any of the data sources: SqlDataSource, LinqDataSource, ObjectDataSource and others.
    In my examples, I will use ObjectDataSource.


    So, here is a simple example:

        DataSourceID="OpHistoryListDS"
        AllowPaging="True"
        PageSize="15"
        CssClass="data" AutoGenerateColumns="False"
        onload="OperationHistoryList_Load"
        EnableViewState="False">
        
            
            
            
        




    * This source code was highlighted with Source Code Highlighter.


    What is interesting for us here? First, required is the runat attribute with the value “server”. The second is DataSourceID . The value is the identifier of the data source located on the page. One important note - GridView can only be used inside a server form -. A couple of the following attributes, AllowPaging & PageSize are set for paging. GridView supports paging. However, if you use ObjectDataSource, then you have to organize paging manually. In my project, I use ORM, written in our company, to enable paging, I have to redraw the sql query to the database. With SqlDataSource it is quite possible to twist your hands. CssClass - speaks for itself. AutoGenerateColumns = ”false” - you decide. Which columns to show (see the nested Columns element). At first glance, nothing complicated. Below is the ObjectDataSource configured for this GridView

        SelectMethod="SelectByMaster"
        TypeName="OperationHistory"
        EnableViewState="false">
        
            
            
            
        



    * This source code was highlighted with Source Code Highlighter.


    For now, I’ll not explain why. We will touch on this later when we try to organize paging using the GridView & ObjectDataSource.

    GridView supports templates. Template templates can be set for paging, as well as for the case when the source returned empty data to us (or empty, or did not return anything, to whom it will be more clear).
    In addition, wide style settings are possible for all the constituent elements of the GridView - header, pager, row, cell, etc.

    Well, for now I’ll finish introducing GridView. Later I will talk about paging, setting from codebehind, writing data selection methods for ObjectDataSource, and much, much more.

    I will try to answer all the questions. Ask.

    UPD: tomorrow about paging, I hope the level will be higher than msdn :)
    For today I didn’t calculate my strength a bit, so I wrote a little

    Also popular now: