When we view the list view on SharePoint, the default Web Part (listviewwebpart) provides a rich and friendly function, which can be used for sorting, filtering, and other operations, each list entry has a friendly drop-down menu. In 2007, Sharepoint built a similar control spgridview to achieve similar results, so when we need to display some other data (such as from SQL Server ), bind data to the spgridview to achieve similar sorting and filtering effects, without writing a line of code related to sorting and filtering. The implementation of the drop-down menu can be done through several lines of simple code.
There are many articles on spgridview about its usage (for example, in jianyi, this article describes how to use objectdatasource to display a Sharepoint-style data view, and there is a relatively simple drop-down menu, you can also sort and filter ). However, the second half of the article, that is, how to display the Sharepoint list data, has a big problem. Based on my search results, few articles have mentioned this problem.
Some examples on the Internet directly use spdatasource for data binding when using the Sharepoint list as the data source. This item is also added in 2007. With a spgridview control, you can directly bind it to a Sharepoint list through the list attribute. There is no problem with the display, but there is a problem in sorting and filtering. I don't know if the authors of those articles have tried to bind the Sharepoint list. I cannot use the code-free method to filter the list (sorting seems OK ). During filtering, the general method is to specify the filtereddatasourcepropertyname attribute of spgridview as "filterexpression", and specify the filtereddatasourcepropertyformat attribute as "{1} = '{0 }'", however, this method does not work for binding the Sharepoint list whose data source is spdatasource, because spdatasource does not have the filterexpression attribute and does not have any filter-related attributes, I have not found any other method for filtering. From this point of view, I think spdatasouce is not a perfect data source implementation, and I don't know if it will be improved in 2009.
In fact, the solution to binding Sharepoint list data is also very simple, that is, first convert the list data into a able (through the getdatatable method of splistitemcollection), and then bind the objectdatasource as the data source to the spgridview, in this way, you can sort and filter without writing code (but there are some places that are not as good as using spdatasource, which will be put later ).
First, filtering. Almost all the articles on the Internet have mentioned this article: filtering with spgridview. This article focuses on two main points: first, when binding a spgridview, if you want to implement the filtering function, you must specify the datasourceid of the spgridview as the ID of the objectdatasource during binding, instead, you cannot directly specify datasource as objectdatasouce. Second, by default, spgridview filtering and sorting are performed simultaneously (the display is not logical ), in this paper, we provide a solution to save the filtering conditions by using viewstate. We will not go into details here.
Then let's talk about data binding. spgridview does not support Automatic Generation of binding columns, so autogeneratecolumn must be set to false. Generally, when writing Asp.net, we use the boundfield control to bind data columns. There is a better choice in Sharepoint, that is, spboundfield. Its usage is exactly the same as that of boundfield, But it processes the SharePoint data type (that is, the column type, this method provides better processing for hyperlinks, multi-choice, and query items. At the underlying level, it calls the getfieldvalueashtml method of the corresponding spfield (So, if you want this to display your custom field type, you 'd better overwrite getfieldvalueashtml when implementing the Custom field ). Of course, this "better processing" refers to using spdatasource as the data source. When we use objectdatasource to bind the datatable obtained by using the getdatatable method, the problem arises: multiple options cannot be normally displayed as multiple choices (but; # internal representation), hyperlinks cannot be displayed as links (displayed as "url, description"), people do not have links, and most intolerable is that multiple texts are not displayed in HTML format, this is because when getdatatable is used, Sharepoint has already performed a data conversion, that is, binding with spboundfield also causes problems. The solution is to implement a getdatatable ...... In fact, it is not troublesome.
Finally, spgridview inherits the gridview, so you can add the "select" function and directly add a commandfield column at the end. You can also use some trick methods to hide the selection button and select it when you click a row, for example:
Of course, since it inherits the gridview, you can also perform some other operations, such as adding a button or something. But another problem occurs again. When the spgridview is PostBack in the button column (or the drop-down menu), data binding fails. There are also solutions: spgridview, spmenufield, grouping, and PostBack.