A more elegant solution to display gridview header and footer when the data source is empty. (CITE)

Source: Internet
Author: User
Http://weblogs.asp.net/joewrobel/archive/2008/01/30/a-more-elegant-solution-to-display-gridview-header-and-footer-when-the-data-source-is-empty.aspx

So here is my solution. I'll cover the main points of interest and if you want to see more, I have
Uploaded all the source and example to the new msdn code Gallery.
Http://code.msdn.microsoft.com/AlwaysShowHeaderFoot

Start out by extending the gridview control and add the following structure.
The most important part here is to override the specified mdatabinding method.

As you can see here. I am intercepting the data and making sure there is at lease one row.
If there is, the control behaves normally.
On the line with the red arrow, I am checking if the binding source is a dataview.
If it is, I can just add a row here and be done with it.
If the binging source is not a dataview (or dataset), then I fire an event that will need to be handled.

The event "mustaddarow", as seen here, will provide access to the binding data and allow you to add a row.

Here is a snippet from a page where I handle the mustaddarow event.
In this case, I am binding a list of products to the gridview.
As you can see, I'm just adding a new product to the list. It doesn' t matter what data you add here
Because it will get hidden in back in the gridview.

And finally back in the gridview, I override the ondatabound method so I can hide that extra row.

So there you have it. This is my first blog post ever. It's exciting, but I have to admit, I'm a little scared.
I see there is a lot of commotion going on about the overwhelming number of useless blogs lately.
Hopefully someone will get some use out of this.

Thanks
-Joe

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.