I started working on SPGridView for one my new Requirement and as you would predict apparently not a straight forward if you are planing to use the feature you are seeing implemented in a share point list. I am still curious about different rendering they are doing using SPGRIDview for list . The different style rendering Table , Preview mode and still allow filtering and sorting .
Anyway coming back to topic SPGridView, Here are good starting points for anyway whose trying to display random list of data in a Grid.
http://blogs.msdn.com/powlo/archive/2007/02/25/displaying-custom-data-through-sharepoint-lists-using-spgridview-and-spmenufield.aspx
http://www.sharepointblogs.com/bobsbonanza/archive/2007/07/02/filtering-with-spgridview.aspx
I am working on custom data from list which needs to be displayed in a grid format, different status coloring and displaying the existing functionalities like Filtering and Sorting like in Share point ( ajax way ).
I started my development using articles above to display Grid and filtering article introduced me to Ajax capabilties for SPGridView which is much easier easier from using AjaxPro.net dll way, which btw I have worked with in SPS 2003.
Here you are using ObjectDataSource which is a control added to page's Controls along with your SPGridView. The filtering works fine. Although I couldn't find any easier way of working with multiple level (cascaded ) filtering I can live with it for now.
Here are few additional issues or work around I would like to mention,
1>HTML BoundColumns ( for displaying any control or text within) This is totally GRidView (.net 2.0 ) or SPGridView :
GridView lets you use bound columns and for custom Data Table like I am developing I find it is easier to use Bound Column with html rendering. This way you can add any HTML tags in your data and it will render it accordingly. It easier because you dont mess with the control and add pre determined Img Column or anything depending on the data it will render the columns accordingly. IF i want an Image to be displayed I can add an <img> tag in my data field and it will display the img in the Grid.
eg:
SPGridView spGdVw = new SPGridView();
BoundField col = new BoundField();
col.HtmlEncode =false;
col.DataField ="Custom";
spGDVw.Columns.Add(col);
// Your custom datatable will have a column called Custom;
foreach(DataRow dr in dt.Rows)
{
dr["Custom"] = string.Format("<a href ='{0}'> <img src='{1}' /> </a> ","hyperlink url", "img url");
}
It will display the linked Img in the column you specified.
2. Handling non Static data or properties with Ajaxial Calls of SPGridView
I was working on a web part and had custom properties for configuration, like list I am supposed to read data from etc. SPGridView works fine so easy to implement when you follow the articles above correctly. You are trying to create real world solution and now what. Filtering works fine ?? Actually once you get to implementing it, You notice only the default values of your Custom Properties are being read instead of the assigned ones and this is not problem with SPGridView it is the Ajax implementation, there is no context of current Instance. It has been a problem with AjaxPro.net dll web methods too. So there is no blaming of SPgridview.
I actually wanted the properties to be read, So what were my options ??
1> Use static variables to assign the custom properties values on intiailization and on Ajax call back use the values. But this doesnt solve the problem because there is just one instance of the static variable for the Class and this means if I have 2 instances of my web part on different pages, then I will be using the static variable with different propety values and at some point they would break the code. So I chose my 2nd approach
2> Get the object instance of the web part from the current page and Reassign properties, from current page. I could have used view state too. I just wanted to make sure I am reading right ones.
Here is a code snippet for the Reading in the values from custom properties to use them in your call back method.
SpWeb spWeb = SPContext.Current.Web ;
SPFile spFile = spWeb.GetFile(spWeb.Url + "/default.aspx");
"Your Web Part instance "
TestWebPart TestWp;
SPLimitedWebPartManager parts = spFile.GetLimitedWebPartManager(Personalization.Shared);
foreach(WebPart wp in parts)
{
if(!(wp.Hidden)&&!(wp.IsClosed)&& wp is TestWebPart )
{ TestWp = wp as TestWebPart;
break;}
}
//Reassign your properties;
sCustomProp = TestWp.CustomProperty................
...
What it does is get instance of the web part from current page and Read its properties again; Yes! but stil ldoesnt solve the entire problem here. What if there are 2 instances on same web part on same page. I have no clue may be an XML file for reading properties stored in SPList. for each instance of web part.