How to let the GridView display the table header when there is no data [when no SqlDataSource control is used]

Source: Internet
Author: User

The original Published time: 2008-08-03--from my Baidu article [imported by moving tools]

To read the full text, click Http://blog.csdn.net/windok2004/archive/2007/10/28/1852554.aspx

We know that as long as the GridView bound DataTable has a row of records, the GridView will display the table header, so when the DataTable is empty

We add a blank line to display the table header.

Let's change the code to look like this:

DataTable dt = new DataTable ();

Dt. Columns.Add ("temple_id");

Dt. Columns.Add ("Temple_name");

Dt. Columns.Add ("location");

Dt. Columns.Add ("Build_date");

if (dt. Rows.Count = = 0)

{

Dt. Rows.Add (dt. NewRow ());

}

This. Gridviewemptydatatest.datasource = DT;

This. Gridviewemptydatatest.databind ();

Before each binding, if a blank line is added, the result of the binding is as follows:


Can see the table header can be displayed, but the blank line displayed without any data is also confusing, could you add a hint of information?

4

3
Add empty record prompt
We add some code after the data binding to the GridView to do a bit of processing, so that the display results more friendly. In

This. Gridviewemptydatatest.databind (); The following add code looks like this:

int columnCount = dt. Columns.count;

Gridviewemptydatatest.rows[0]. Cells.clear ();

Gridviewemptydatatest.rows[0]. Cells.add (New TableCell ());

Gridviewemptydatatest.rows[0]. Cells[0]. ColumnSpan = ColumnCount;

Gridviewemptydatatest.rows[0]. Cells[0]. Text = "no record";

Gridviewemptydatatest.rows[0]. Cells[0]. Style.add ("Text-align", "center");

The improved display results are shown in the following figure:


It appears that the results have reached our requirements, but the page is displayed again when there are other button actions on the page that cause the page to postback

The message does not appear as shown in the following figure:


That's not what we want.

4

4
Prevent
PostBack
When the page shows changes
To prevent the display from changing, we re-bind the GridView by adding the following code in the Page_Load event:

if (IsPostBack)

{

Reconstructs the GridView if the data is empty

if (GridViewEmptyDataTest.Rows.Count = = 1 && gridviewemptydatatest.rows

[0]. Cells[0]. Text = = "no Record")

{

int columnCount = GridViewEmptyDataTest.Columns.Count;

Gridviewemptydatatest.rows[0]. Cells.clear ();

Gridviewemptydatatest.rows[0]. Cells.add (New TableCell ());

Gridviewemptydatatest.rows[0]. Cells[0]. ColumnSpan = ColumnCount;

Gridviewemptydatatest.rows[0]. Cells[0]. Text = "no record";

Gridviewemptydatatest.rows[0]. Cells[0]. Style.add ("Text-align", "center");

}

}

Our controls can finally be displayed as we asked, but for code reuse, when there are multiple GridView in a project,

Avoid the same code, we need to encapsulate the code into classes, so that all GridView data binding can be easily implemented

Our requirements.


The wrapper code for the class is as follows:

Using System.Data;

Using System.Web.UI.WebControls;

<summary>

GridView data record is empty when the GridView header is displayed and a hint is not logged

</summary>

public class Gridviewcontrol

{

Information that is displayed when the GridView data is empty

private static string emptytext = "no record";

Public Gridviewcontrol ()

{

}

<summary>

Prevent postback after the GridView cannot be displayed

</summary>

<param name= "GridView" ></param>

public static void Resetgridview (GridView GridView)

{

Reconstructs the GridView if the data is empty

if (GridView. Rows.Count = = 1 && gridview. Rows[0]. Cells[0]. Text = = Emptytext)

{

int columnCount = GridView. Columns.count;

Gridview. Rows[0]. Cells.clear ();

Gridview. Rows[0]. Cells.add (New TableCell ());

Gridview. Rows[0]. Cells[0]. ColumnSpan = ColumnCount;

Gridview. Rows[0]. Cells[0]. Text = Emptytext;

Gridview. Rows[0]. Cells[0]. Style.add ("Text-align", "center");

}

}

<summary>

Bind data to GridView, Show table header when table data is empty

</summary>

<param name= "GridView" ></param>

<param name= "Table" ></param>

public static void Gridviewdatabind (GridView GridView, DataTable table)

{

Log empty to reconstruct GridView

if (table. Rows.Count = = 0)

{

Table = table. Clone ();

Table. Rows.Add (table. NewRow ());

Gridview. DataSource = table;

Gridview. DataBind ();

int columnCount = table. Columns.count;

Gridview. Rows[0]. Cells.clear ();

Gridview. Rows[0]. Cells.add (New TableCell ());

Gridview. Rows[0]. Cells[0]. ColumnSpan = ColumnCount;

Gridview. Rows[0]. Cells[0]. Text = Emptytext;

Gridview. Rows[0]. Cells[0]. Style.add ("Text-align", "center");

}

Else

{

Data is not directly bound to null

Gridview. DataSource = table;

Gridview. DataBind ();

}

Rebind deselect

Gridview. SelectedIndex =-1;

}

}

You can compile this class into a DLL and let the various places invoke it.

4. 6 Use examples
The use of this class is simple, that is, each time the data binding is called Gridviewdatabind, the first argument of this function is to bind

Fixed data for GridView the second parameter is a DataTable that contains data field columns, which may be empty, and if the data is not empty, the function

is automatically bound properly, otherwise the "no record" prompt is displayed.

The code for the button event above can be changed to resemble the following:

DataTable dt = new DataTable ();

Dt. Columns.Add ("temple_id");

Dt. Columns.Add ("Temple_name");

Dt. Columns.Add ("location");

Dt. Columns.Add ("Build_date");

Gridviewcontrol.gridviewdatabind (this. gridviewemptydatatest, DT);

Finally, in Page_Load, the Resetgridview function is called for all GridView on this page, as follows:

if (IsPostBack)

{

Gridviewcontrol.resetgridview (this. Gridviewemptydatatest);

}

Here to provide a Web site test source code, you can follow the data table described in 2 to establish the test database, and the table to add the corresponding data to test

Try, remember to change the connection string oh.

Test Source: Http://dl2.csdn.net/down4/20071008/08205401208.rar

How to let the GridView display the table header when there is no data [when no SqlDataSource control is used]

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.