How to make the gridview control display the title when the bound data source does not have data

Source: Internet
Author: User

When a data source is bound to a DataGrid in the past, as long as the data source to be bound is not null but has a table structure, the title of the list can be displayed. This is not the case in the gridview. Even if the data source has a table structure, the title cannot be displayed as long as there is no row record.

I have used the following methods for your reference:

1. The simplest method is to use. If the returned data is null and there is no table structure, the corresponding table structure can be obtained from the database or temporary combination, for example:
Datatable dt = new datatable ("column ");
DT. Columns. Add (ID, typeof (system. int32 ));
DT. Columns. Add (name, typeof (system. String ));
If the returned data source has a table structure, but there is no row record, you do not need to do this step.
Then use
Datarow DR = DT. newrow ();
DT. Rows. Add (DR );
Then bind it to the gridview to display the title.
This method is simple, but there are some problems. If there are other controls in the list, such as the SELECT command, the blank lines are displayed, and the SELECT command is displayed. This will make the user mistakenly think that there is a record. Of course, you can check the status in rowdatabound to determine whether to clear all controls.

2. Use the emptydatatext attribute to prompt that the user has no records, but the title cannot be displayed.
When the data source bound to the gridview control does not contain any records, empty data rows are displayed in the control. Use the emptydatatext attribute to specify the text to be displayed in an empty data row.

3. Use the emptydatatemplate attribute. It can contain controls and HTML tags.
If both the emptydatatext and emptydatatemplate attributes are set, the emptydatatemplate attribute takes precedence.

4. createchildcontrols (ienumerable, bool) method:
This method creates the control to be displayed based on the data source and related settings. It is mainly a table and returns the number of created controls,
if no data exists in the data source, the gridview displays
information based on the attributes emptydatatemplate and emptydatatext. If neither of these attributes is set, the gridview does not display the task content.
In the DataGrid, if the data source does not have data, a table header is displayed. The gridview does not display the table header. For some
users, it may be a bit unaccustomed.
if you want to display the representation when no data is available, you can override this method. In this method, you can use the createcolumns method
to obtain the columns to be displayed, when you call the createrow method to create a row, call the intializerow method to initialize the row that creates
, and add the row to the table. One thing to note here is, if the gridview does not display anything,
there is no control in its controls. We need to create a table object by ourselves, and add it to the controls
set

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.