剑花 烟雨

   江南  好 风景旧曾谙 日出江花红胜火 春来江水绿如蓝 能不忆东北

理想中的建筑师应该是一个诗人、数学家、精通历史、精通心理学、熟悉音乐、了解医学、还要具备天文学和计算的基本知识。
                                                                                                                         ------------------公元前25年,罗马建筑学家Vitruvius
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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 PerformDataBinding method.
 ClassStructure

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.
 image

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

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.
image

And finally back in the GridView, I override the OnDataBound method so I can hide that extra row.
image 

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

count hit
欢迎您到紫阁阳光坐客