How to fill in blank rows when the page of Data bound by the gridview is less than one page

Source: Internet
Author: User

ASP. NET learning notes series:

    • A Method for displaying pager rows at all times in the gridview

ASP. NET 2.0 provides very powerful binding capabilities, while the new gridview control in ASP. NET 2.0 fully reflects and gives full play to the powerful functions of data binding. You can use the simpleCodeThis greatly improves the development efficiency.

However, when using pagination, if the bound data is less than one page, the gridview only displays the bound data. For example, set pagesize of the gridview to 10, and 10 data entries are displayed on each page. If only 6 data entries are returned for the current query, the gridview displays the 6 data entries, if more than 10 data records are returned for the current query, the gridview displays 10 data records. In this way, when different queries are executed, the gridview displays 10 data records at a time, sometimes 1, 2, 3 ,......, 9 data entries. The displayed area of the gridview changes with the number of bound data entries, which gives a poor interactive experience.

What should I do? If you can always display the same number of rows regardless of the number of bound data entries, that is, when the number of data entries bound to the gridview is less than one page, some method is used to add blank rows in the gridview so that the gridview can always display the number of rows set by pagesize.

So how to add blank lines? You can also use the databound event of the gridview to determine whether the row size of the gridview is the same as that set by pagesize by judging the row attribute of the gridview. The code and effect are as follows:

Public   Void Gridview_databound ( Object Sender, eventargs E)
{
If (Gridview. Rows. Count ! =   0   && Gridview. Rows. Count ! = Gridview. pagesize)
{
Control table = Gridview. controls [ 0 ];
If (Table ! =   Null )
{
For ( Int I =   0 ; I < Gridview. pagesize - Gridview. Rows. Count; I ++ )
{
Int Rowindex = Gridview. Rows. Count + I +   1 ;
Gridviewrow row =   New Gridviewrow (rowindex, - 1 , Datacontrolrowtype. separator, datacontrolrowstate. Normal );
Row. cssclass = (Rowindex %   2   =   0 ) ?   " Alternate " : " Item " ;
For ( Int J =   0 ; J < Gridview. Columns. Count; j ++ )
{
Tablecell Cell =   New Tablecell ();
Cell. Text =   " & Nbsp; " ;
  Row. Controls. Add (cell );
}
Table. Controls. addat (rowindex, row );
}
}
}
}

 

Note that:

1. The rowtype of the added gridviewrow needs to be set to seperator;

2. when setting the tablecell attribute of blank rows in mainland China, pay attention to whether the visible attribute of the corresponding column of the gridview is set to false, and whether the autogeneratecollomn of the gridview is set to true. If yes, in this case, some corresponding processing should be performed.

The last result is the gridview effect of the blank row.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.