In this topic, I will talk about one great ASP.NET GridView control. Many newbies, I think, often have problems using this control. I also had problems. I note that there is almost no 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, I will give a simple example:
< asp:GridView ID ="OperationHistoryList" runat ="server"
DataSourceID ="OpHistoryListDS"
AllowPaging ="True"
PageSize ="15"
CssClass ="data" AutoGenerateColumns ="False"
onload ="OperationHistoryList_Load"
EnableViewState ="False" >
< Columns >
< asp:BoundField DataField ="" />
< asp:BoundField DataField ="" />
< asp:BoundField DataField ="" />
</ Columns >
</ asp:GridView >
* This source code was highlighted with Source Code Highlighter .
')
What is interesting for us here? The first, mandatory, is the
runat attribute with the value “server”. The second is the
DataSourceID . The value is the identifier of the data source located on the page. One important note - GridView can be used only within the server form -
<front runat = ”server”> . A pair of the following attributes,
AllowPaging &
PageSize are set for paging. GridView supports paging. However, if you use ObjectDataSource, then you will 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 that you will not have to twist anything with your hands.
CssClass - speaks for itself.
AutoGenerateColumns = ”false” - you define it yourself. Which columns to show (see the embedded Columns element). At first glance, nothing complicated. Below is the ObjectDataSource configured for this GridView.
< asp:ObjectDataSource ID ="OpHistoryListDS" runat ="server"
SelectMethod ="SelectByMaster"
TypeName ="OperationHistory"
EnableViewState ="false" >
< SelectParameters >
< asp:Parameter Name ="maximumRows" Type ="Int32" />
< asp:Parameter Name ="startRowIndex" Type ="Int32" />
< asp:QueryStringParameter Name ="masterId" QueryStringField ="parent_id" Type ="String" />
</ SelectParameters >
</ asp:ObjectDataSource >
* This source code was highlighted with Source Code Highlighter .
For now, I’m not going to explain why. We will touch on this later when we try to organize paging with the help of GridView & ObjectDataSource.
GridView supports teamplayta. Templates can be set for paging, as well as for the case when the source returned us empty data (either empty, or returned nothing, as it will be clearer).
In addition, wide style settings are possible for all components of the GridView - header, pager, row, cell, etc.
Well and while I will finish to acquaint with GridView. Later I will talk about paging, setting up codebehind, writing data selection methods for the ObjectDataSource, and much more.
I will try to answer all the questions. Ask.
UPD: tomorrow about paging, I hope the level will be higher msdn :)
Today I didn’t calculate my strength a little, so I wrote little