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

Tuesday, May 22, 2012

Binding objectdatasource to SPGridView for Sorting

Share/Save/Bookmark

Today I have a simple requirement, to achieve filtering and sorting on a simple grid.
Asp.Net GridView do not have an option to filter by default but where as SPGridView provides as part of the control.
I have simple method, that will form the data into DataTable which will be set as datasource to SPGridView as shown in below code.



<SharePoint:SPGridView runat="server" ID="reportsList" AutoGenerateColumns="false"PageSize="1" AllowPaging="false"
AllowSorting="true"  AllowFiltering="false" FilterDataFields="Col1,Col2,Col3,Col4" >
    <Columns>
        <asp:TemplateField HeaderText="Col1" SortExpression="Col1">
            <ItemTemplate>
                <asp:HyperLink ID="Col1Link" runat="server" Text='<%# Eval("Col1") %>'NavigateUrl= '<%# Eval("Col5") %>'   />
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="Col2" SortExpression="Col2">
            <ItemTemplate>
                <asp:HyperLink ID="TabLink" runat="server" Text='<%# Eval("Col2") %>'NavigateUrl= '<%# Eval("Col6") %>'  />
            </ItemTemplate>
        </asp:TemplateField>
                                   
        <SharePoint:SPBoundField HeaderText="Col3" SortExpression="Col3" DataField="Col3" />
        <SharePoint:SPBoundField HeaderText="Col4" SortExpression="Col4" DataField="Col4" />
    </Columns>
    <EmptyDataTemplate>   No data.</EmptyDataTemplate>
</SharePoint:SPGridView>


above markup is also an example for binding hyperlink fields in spgridview.

protected override void CreateChildControls()
        {
            base.CreateChildControls();
            using (SPSite site = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb web = site.OpenWeb())
                {
                  
                    // bind the datasource to grid
                    reportsList.DataSource = GetMyDataTable();
                    reportsList.DataBind();
                  
                }
            }
           
        }
So easy and simple to see the grid populated with data on the page, but when I choose to sort a column I get the below error .
The GridView 'GridViewID' fired event Sorting which wasn't handled
Googling around I found that property, DataSourceID need to set instead of setting DataSource. Not sure why.
So now I have to rewrite the code and bind some type of datasource(objectdatasource/SPdatasources/…/).
As we know, ObjectDataSource is simple and easy I started with it and then lately realized that I have to create objects, I mean new type that will be used by ObjectDataSource as suggested by all other blogs.
But I do not want to write extra code but directly bind the existing datatable as objectdatasource for which blogs suggest to create type and write a method that will convert it to datatable which seem to be round way for me.
So I tried the below code and able to achieve sorting without a new Type/object definition. Still i could not get filtering work..at least it did not throw any error in log or on page.


//populate the datasource
                    ObjectDataSource  reportsDataSource = new ObjectDataSource();
                    reportsDataSource.ID = "reportsDataSource";
                    reportsDataSource.SelectMethod = "GetMyDataTable";
                    reportsDataSource.TypeName = this.GetType().AssemblyQualifiedName;
                    this.Controls.Add(reportsDataSource);
                    
                    // bind the datasource to grid
                    reportsList.DataSourceID = "MyDataSource";//  = 
                    reportsList.DataBind();


 Subscribe