Extended: GridView When empty data is displayed on the table header

Source: Internet
Author: User

July 14, 2015 16:50:06 Gridview by default, if the data is empty, the table is not displayed and is not displayed visually.

To extend for this issue:

usingSystem.Web.UI.WebControls; Public Static classgridviewextension{ Public Static voidBindemptydata ( ThisGridView g) {        if(G.rows.count = =0)        {            //setting of the table headerGridViewRow row =NewGridViewRow (-1, -1, Datacontrolrowtype.emptydatarow, datacontrolrowstate.normal); foreach(DataControlField Fieldinchg.columns) {TableCell cell=NewTableCell (); Cell. Text=field.                HeaderText; Cell. Width=field.                Headerstyle.width; Cell. Height=field.                Headerstyle.height; Cell. ForeColor=field.                Headerstyle.forecolor; Cell. Font.Size=field.                HeaderStyle.Font.Size; Cell. Font.Bold=field.                HeaderStyle.Font.Bold; Cell. Font.Name=field.                HeaderStyle.Font.Name; Cell. Font.strikeout=field.                HeaderStyle.Font.Strikeout; Cell. Font.underline=field.                HeaderStyle.Font.Underline; Cell. BackColor=field.                Headerstyle.backcolor; Cell. VerticalAlign=field.                Headerstyle.verticalalign; Cell. HorizontalAlign=field.                Headerstyle.horizontalalign; Cell. CssClass=field.                Headerstyle.cssclass; Cell. BorderColor=field.                Headerstyle.bordercolor; Cell. BorderStyle=field.                Headerstyle.borderstyle; Cell. BorderWidth=field.                Headerstyle.borderwidth; Row.            Cells.add (cell); } TableItemStyle Headstyle=G.headerstyle; TableItemStyle Emptystyle=G.emptydatarowstyle; Emptystyle.width=Headstyle.width; Emptystyle.height=Headstyle.height; Emptystyle.forecolor=Headstyle.forecolor; EmptyStyle.Font.Size=headStyle.Font.Size; EmptyStyle.Font.Bold=HeadStyle.Font.Bold; EmptyStyle.Font.Name=HeadStyle.Font.Name; EmptyStyle.Font.Strikeout=headStyle.Font.Strikeout; EmptyStyle.Font.Underline=HeadStyle.Font.Underline; Emptystyle.backcolor=Headstyle.backcolor; Emptystyle.verticalalign=headstyle.verticalalign; Emptystyle.horizontalalign=headstyle.horizontalalign; Emptystyle.cssclass=Headstyle.cssclass; Emptystyle.bordercolor=Headstyle.bordercolor; Emptystyle.borderstyle=Headstyle.borderstyle; Emptystyle.borderwidth=Headstyle.borderwidth; //settings for blank linesGridViewRow Row1 =NewGridViewRow (0, -1, Datacontrolrowtype.emptydatarow, datacontrolrowstate.normal); TableCell Cell1=NewTableCell (); Cell1. Text="there is no data to display"; Cell1. ForeColor=System.Drawing.Color.Purple; Cell1. BackColor=System.Drawing.Color.White; Row1.            Cells.add (CELL1); Cell1. ColumnSpan=G.columns.count; if(G.controls.count = =0) {G.page.response.write ("<script language= ' javascript ' >alert (' call this extension method after first calling the GridView Binddata () Method! '); </script>"); }            Else{g.controls[0].                Controls.clear (); g.controls[0]. Controls.addat (0, Row); g.controls[0]. Controls.addat (1, Row1); }        }    }}

The code is called in the following way:

    protected void Page_Load (object  sender, EventArgs e)    {        if (!  IsPostBack)        {            binddata ();        }    }     protected void Binddata ()    {        this. Grid_plantvarietyright.databind ();          This . Grid_plantvarietyright.bindemptydata ();          }

Display effects such as

This extension of the main reference http://www.cnblogs.com/muer/archive/2010/03/26/1697178.html a little extension, thank the elder brother.

Extended: GridView When empty data is displayed on the table header

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.