A more elegant solution to display GridView header and footer when the data source is empty. (cite)
2008-02-01 23:54
639 查看
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.
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
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.
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
相关文章推荐
- How to display an empty row when the underlying dataset is empty
- Consuming the Object Data Source--Binding the Object Data Source to the GridView
- How to bind to data when the DataContext is not inherited【项目】
- 导入Theano警告解决:failed to import scipy.linalg.blas, and theano flag blas.ldflags is empty
- Simple Excel to connect to the database and display filtered data.
- Demo: Fill and Save PDF Form online(also can add more function,such as email and save data to database ),Adobe Reader is enough
- The source file is different from when the module was built. Would you like the debugger to use it anyway?
- The source file is different from when the module was built. Would you like the debugger to use it anyway?
- Microsoft SQL Server 2008 R2 XML处理 One solution is to increase the number of characters retrieved from the server for XML data.
- [WPF] How to bind to data when the DataContext is not inherited
- This function has none of Deterministic,no sql,or reads sql data in its declaration and binary logging is enabled(you *might* want to use the less safe log_bin_trust_function_creators variable
- Show of Gridview Header with Empty Data Source
- [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates
- PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the Eve
- No orientation specified, and the default is horizontal. This is a common source of bugs when ...
- 每日刷题(2015/6/23)You are given the source to an application which crashes when it is run.
- What is the difference between SET and SELECT when assigning values to variables, in T-SQL?
- GridView的 EmptyDataTemplate与 ShowHeaderWhenEmpty属性
- No orientation specified, and the default is horizontal. This is a common source of bugs when childr
- Solution to "The breakpoint will not currently be hit. The source code is different from the original version."