When the data source of the GridView control introduced in asp.net 2.0 is empty (GridView. DataSource = null), the header cannot be displayed.

Source: Internet
Author: User
Solution:

Method 1: Use its EmptyTemplate to implement it. Write a static table in the template;

If your header is only html text, there is no control. When the header is displayed, you can copy the html of the header and put it in EmptyDataTemplate.

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:

This is also the method to introduce: extend the GridView to implement. Inherit GridVie and rewrite the Render method. When the data source is empty, perform the following operations:

/// <Summary>

/// GridView extension control

/// </Summary>

Public class GridView: System. Web. UI. WebControls. GridView

{

Private bool _ enableEmptyContentRender = true;

/// <Summary>

/// Whether the title line is displayed when the data is null

/// </Summary>

Public bool EnableEmptyContentRender

{

Set {_ enableEmptyContentRender = value ;}

Get {return _ enableEmptyContentRender ;}

}

Private string _ EmptyDataCellCssClass;

/// <Summary>

/// Information cell style class when it is null

/// </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 f in this. Columns) // generate table header

{

TableCell cell = new TableCell ();

Cell. Text = f. HeaderText;

Cell. CssClass = "TdHeaderStyle1"; // The Header style is left empty.

Row. Cells. Add (cell );

}

TableRow row2 = new TableRow ();

T. Rows. Add (row2 );

TableCell msgCell = new TableCell ();

MsgCell. CssClass = this. _ EmptyDataCellCssClass;

If (this. EmptyDataTemplate! = Null) // the second row, use the template

{

This. EmptyDataTemplate. InstantiateIn (msgCell );

}

Else // the second row, use the 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 checked CSDN. The solution is as follows:

Determine whether the datatable is empty before binding
If it is null, manually add a row of data
Da. Fill (ds, "data ");

If (ds. Tables ["data"]. Rows. Count = 0)
{
Ds. Tables ["data"]. Columns. Clear ();
Ds. Tables ["data"]. Columns. Add ("emp_id ");
Ds. Tables ["data"]. Columns. Add ("fname ");
Ds. Tables ["data"]. Columns. Add ("job_id ");
DataRow dr = ds. Tables ["data"]. NewRow ();
Dr ["emp_id"] = "No ";
Dr ["fname"] = "Record ";
Dr ["job_id"] = "Found ";
Ds. Tables ["data"]. Rows. Add (dr );
}
If (! IsPostBack)
{
This. GridView1.DataSource = ds. Tables ["data"];
This. GridView1.DataBind ();
}
Note that data emp_id fname job_id corresponds to your own !!!

Of course, it can also be implemented through the js client.

2. Edit the EmptyDataTemplate template of the GridView.
What you want to display when there is no data in it
For example, a table contains only the GridView header and an empty row.
In this way, the table is displayed when no data exists. It seems that a blank data GridView is displayed.
My level is limited. I just tried to use this method.
If you do not need the EmptyDataTemplate template, you can set the EmptyDataText attribute of the GridView to display only one sentence and one border

Related Article

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.