How to display the table header when the data source of the gridview is empty

Source: Internet
Author: User
Problem: The gridview control introduced in Asp.net 2.0 cannot be displayed when its data source is empty (gridview. datasource = NULL ).
Output header.

Solution:

Method 1: Use its emptytemplate to implement it. Write a static table in the template;
Disadvantage: It is troublesome. You need to set each gridview.

Method 2: if the data source is able, a blank row of datatable is always returned when no data exists;
If the data source is a collection class (arraylist, list <t>, etc.), when no data exists, an empty entity is generated and added to the Collection class.
Disadvantage: it is still troublesome.

Method 3:
It is also a method to introduce to you: extend the gridview to implement. inherit gridvie and rewrite the render method. When the data source is empty, perform the following operations:CodeRight:

/// <Summary>
/// Gridview extension control
/// @ Author: jianyi0115@163.com
/// </Summary>
Public class gridview: system. Web. UI. webcontrols. gridview
{
Private bool _ enableemptycontentrender =   True ;
// /<Summary>
// /Whether the title line is displayed when the data is empty
// /</Summary>
Public bool enableemptycontentrender
{
Set {_ enableemptycontentrender = Value ;}
Get { Return _ Enableemptycontentrender ;}
}

Private string _ emptydatacellcssclass;
// /<Summary>
// /When it is null, the Information Cell Style Class
// /</Summary>
Public String emptydatacellcssclass
{
Set {_ emptydatacellcssclass = Value ;}
Get { Return _ Emptydatacellcssclass ;}
}

// /<Summary>
// /Output content when it is null
// /</Summary>
// /<Param name = "Writer"> </param>
Protected virtual Void Renderemptycontent (htmltextwriter writer)
{
Table t =   New Table (); // create a table
T. cssclass =   This . Cssclass; // copy all property
T. gridlines =   This . Gridlines;
T. borderstyle =   This . Borderstyle;
T. borderwidth =   This . Borderwidth;
T. cellpadding =   This . Cellpadding;
T. cellspacing =   This . Cellspacing;

T. horizontalalign= This. Horizontalalign;

T. Width= This. Width;

T. copybaseattributes (This);

tablerow row = New tablerow ();
T. rows. add (ROW);

Foreach (datacontrolfield FIn This. Columns) // generate table Header
{
Tablecell Cell= NewTablecell ();

Cell. Text=F. headertext;

Cell. cssclass= "Tdheaderstyle1"; // Write the Header style to death

Row. cells. Add (cell );
}

Tablerow row2= NewTablerow ();
T. Rows. Add (row2 );

Tablecell msgcell= NewTablecell ();
Msgcell. cssclass= This. _ Emptydatacellcssclass;

If ( This . Emptydatatemplate ! =   Null ) // Second Row, use the template
{
This . Emptydatatemplate. instantiatein (msgcell );
}
Else // The second row, use Emptydatatext
{
Msgcell. Text =   This . Emptydatatext;
}

Msgcell. horizontalalign=Horizontalalign. Center;
Msgcell. columnspan= This. Columns. count;

Row2.cells. Add (msgcell );

T. rendercontrol (writer );
}

Protected override Void Render (htmltextwriter writer)
{
If (_ Enableemptycontentrender && ( This . Rows. Count =   0   |   This . Rows [ 0 ]. Rowtype = Datacontrolrowtype. emptydatarow ))
{
Renderemptycontent (writer );
}
Else
{
Base. Render (writer );
}
}

}
}

I love the improvement!

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.