Showing posts with label SPGridView. Show all posts
Showing posts with label SPGridView. Show all posts

Friday 29 October 2010

SPGridView explained

Overview:  The SPGridView is similar to the asp GridView.  The SPGridView is derived from the aspGridView and has additional functionality.  The SPGridView does not support the property AutoGenerateColumns, you must specify each SPBoundField.

Example: SPBoundFields such as "BoundField" are used to display datasource columns.
 
Example: To format a bound column use the property "DataFormatString", e.g. colDate.DateFormatString = "{0:d}";, this will display the short date only from a DateTime type.  If you need more advanced formating such as for a multi choice field, create a new class that derives from BoundField and use it in the SPGridView.

Tip: Don't use DataKeyName on the SPDataGrid, it causes the error "Unable to cast object of type 'System.Int32' to type 'System.String'.".
Read More:
http://msdn.microsoft.com/en-us/library/bb466219(office.12).aspx

SPDataGrid error 'Unable to cast object of type int32 - SPDataGrid.NewPageIndex property and DataKeyNames

Problem: SPDataGrid can't use DataKeyNames, it results in the error "Unable to cast object of type 'System.Int32' to type 'System.String'." when paging or filtering.


Hypothesis: This looks like a bug in the implementation of the SPGridView.  The property DataKeyNames causes the error.

Resolution: To pass values using buttons or check boxes inside you SPGridView rather use the row index to get the row and read the Id or data you need from the row.

More info:
http://www.sharepointoverflow.com/questions/4481/using-code-compiled-for-moss-2007-in-2010

Monday 25 October 2010

Using SPGridView and Linq to display filterable lists

Problem: Display a SharePoint list and make it filterable. 

Initial Hypothesis: Create new page add the new Xslt List View Web Part (XLVWP) to the page. Customise the Xslt using SharePoint Designer (SPD).  The web part can now be exported if required.  Add filter web parts to filter the data for end users.

Resolution:  This gives a comprehensive solution for internal users but clients needing a specific design or advanced querying will need a different solution.  You can customise the xslt for the list and paging and sorting are built in.  However in my instance I needed drop down lists that could query against multi select choice columns. 

Problem Redefined: Using the SPGridView control display user's data based on selection criteria.

Hypothesis:
  1. Create a list that contains a choice column, the column should allow multiple choices.
  2. Ensure your generate a Linq to SharePoint proxy
  3. Create a Visual Web Part project that uses a visual web part to display the list results and the results are filtered by a drop down lists that works against the SharePoint list.
Resolution:
Filter the SPDataGrip using LINQ to SharePoint as shown below:
Step 1: Create the "Customers" list.
Stet 2 : Add the new column based on a multiple choice field type.

Shown above is my custom list "Customer", it has two columns namely: "Title" of type string and "Industry" of type choice (multi)
Step 3: Add a few random records.
Step 4: Create a new Visual Studio project, I chose a "Visual Web Part Project".
Step 5: Add the generate SPMetal code to your new project.  I used the SPMetal template provided by CKSDev but as long as you have added the SPMetal for the new list it will work.
Step 6:  Open the user control ascx and add a SPGridfiew and a drop down list.
Step 7:  In the code behind add the Linq query to retrieve the Customers list.
Different approach for using LINQ to SharePoint for this step is:

Step 8: Deploy you code and ensure it is working.