When displaying custom data through SPGridView you won’t get the same look and feel of the default SharePoint List View. SPMenuField provides a data bound drop-down menu to be used in place of BoundField, TemplateField, or similar controls.
The following describe steps required to add drop-down menu items to a SPGridView control in Web Part.
- Create a Web Part
- Add project references: Microsoft.SharePoint and System.Web
- Add the following namespaces to your class:
- Inherits your class with Microsoft.SharePoint.WebPartPages.WebPart
- Override CreateChildControls method, create the ObjectDataSource, SPGridView, SPGridViewPager and SPMenuField controls as shown below:
- Code above create three (3) drop-down menu items and a separator menu as shown below:
- Clicking on the “View Item” redirects to custom page in the _layouts folder. When clicking on the “Open WebSite”, it opens web site and “Send Email” menu open client email form.
- Please note SPMenuField has a property TokenNameAndValueFields which can be used to store token names and values in the following format:
- To consume SPMenuField token values, you must specify token name within the % sign as shown below:
- As shown in code example above, ObjectDataSource control require “SelectData” method. You need to create a public method SelectData which returns a System.Data.DataTable – a dummy data which has three (3) columns which are “Name”, “Email” and “WebSite”. See below:
- In addition, ObjectDataSource control implement event handler ObjectCreating – You also need to create instance containing the event data as shown below:
- Next, bind the data with the SPGridView control as shown below:
- Build and deploy to SharePoint site. That’s it – All Done. See below for screenshot:
Get sample code here: