Using SPGridView to Display Your Data in WSS 3.0

来源:百度文库 编辑:神马文学网 时间:2024/04/29 12:02:02
Using SPGridView to Display Your Data in WSS 3.0

The other day I went to visit one of my colleagues in the building I currently work at. Was talking to him and noticed that he was using the SPGridView control to render his data, so thought of using it instead of using the ASP.NET data grid and then spend a lot of time trying to make look like SharePoint!

So as usual, the first thing I do; tried to Google it, the disappointment was that I only found two good posts on the web (One by Paolo and the other is by Bob). That being said; I had to take the challenge and create my own version of the SPGridView. Plus the SPGridView gives you the ability to create list item menus that are like the one that is available with the out of the box list views.

So let us get started!

Features available with my version of SPGridView:

  • Sorting.
  • Grouping.
  • Filtering.
  • Paging.

Going through the blog posts, I found out that SPGridView had some trouble remembering user selection when two operations are applied on the SPGridView at the same time (for example: sorting and filtering, paging and sorting … etc.) and that the developer has to manually store some information in the View State object to make it remember things.

So imagine, every time you want to use the SPGridView, you will have to remember all the details of how to make SPGridView remember its state? Plus more!

First idea came to my mind was to create a new class that inherits the SPGridView (I was lucky it was not sealedJ) and encapsulate all the "remembering" abilities inside of it, this way we leave the developer away from the details of how to handle it.

I am not going to go through the details of what all things mean since Paolo and Bob did a wonderful job explaining it all.

Click here to get the code for the SWSSPGridView that I have implemented.

Now, to test that class, I need to create a ExampleObjectDataSource and a WebPart that will hold the SWSSPGridView.

Click here for the code for the ExampleObjectDataSource.

Click here for the code for the WebPart that will hold our SWSSPGridView.

Deploy the WebPart to your SharePoint environment and add it to a page, you should get the following display.

And now our SWSSPGridView is aware of the user selection and keeps track of it without making the developer worry about how to do it!