强大的SPGridView

利用SPGridView显示自定义数据源

Step1:

新建一个页面,写入如下内容:

<%@ Page MasterPageFile="~/_layouts/application.master"   Language="C#" AutoEventWireup="true"  CodeFile="SPGridViewTest.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
    Namespace="Microsoft.SharePoint.WebControls" TagPrefix="cc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PlaceHolderMain" runat="server">
        <cc1:SPGridView ID="SPGridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" PageSize="3" >
            <AlternatingRowStyle CssClass="ms-alternating" />
            <SelectedRowStyle CssClass="ms-selectednav" Font-Bold="True" />
        </cc1:SPGridView>
        <div style="text-align:center">
    <cc1:SPGridViewPager ID="SPGridViewPager1" runat="server" GridViewId="SPGridView1">    
    </cc1:SPGridViewPager></div>
 </asp:Content>

注意:
SPGridView不支持自动生成列,所以 一定要设置AutoGenerateColums为false。
SPGridView直接启用了分页,并且,页面放入一个SPGrieViewPager,来实现分页。
Step2:
在CodeFile里写一个提供测试数据的函数:

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

public DataTable GetDataTable()
{
    DataTable tblData = new DataTable();
    tblData.Columns.Add("Title");
    tblData.Columns.Add("Author");
    tblData.Columns.Add("Rating");

    tblData.Rows.Add("Obsession", "Robards, Karen", 2);
tblData.Rows.Add(
"Vanished", "Robards, Karen", 3); tblData.Rows.Add("Magician: Apprentice", "Feist, Raymong E.", 4); tblData.Rows.Add("Magician: Master", "Feist, Raymong E.", 5); tblData.Rows.Add("Silverthorn", "Feist, Raymong E.", 4); tblData.Rows.Add("Lord Foul's Bane", "Donaldson, Stephen R.", 3); tblData.Rows.Add("The Illearth War", "Donaldson, Stephen R.", 4); return tblData;
}

Step3:
在Page_Load里写代码,加入ObjectDataSource控件,用这个控件来调用上面的GetDateTable函数。

ObjectDataSource odsDataSource = new ObjectDataSource();
odsDataSource.ID = "ExampleSource";
odsDataSource.TypeName = this.GetType().FullName + "," + this.GetType().Assembly.FullName;
odsDataSource.SelectMethod = "GetDataTable";
Controls.Add(odsDataSource);

有人可能要奇怪了,为什么要用ObjectDataSource来调用GetDataTable,直接给SPGridView的DataSource属性赋值不行吗?
答案是:可以的。但是,那样的话,你就不的不再写一些代码来处理SPGridView的过滤和排序事件 。而ObjectDataSource是可以自动来处理这些事件的。

Step4:

继续在Page_Load写代码,设置SPGridView的过滤格式和过滤属性,如果不这样设置的话,过滤功能不会起效(这个设置可是我起早摸黑,用reflector看它的代码才发现的...)。

   //{0}表示过滤值,{1}表示过滤字段值
   SPGridView1.FilteredDataSourcePropertyFormat = "{1}='{0}'";
   SPGridView1.FilteredDataSourcePropertyName = "FilterExpression" ;    
   SPGridView1.EnableViewState = false;

Step5:

继续在Page_Load写代码,添加字段,并启用排序 :

        //添加字段
        SPBoundField col = new SPBoundField();
        col.DataField = "Title";
        col.SortExpression = "Title";
        col.HeaderText = "Title";
        SPGridView1.Columns.Add(col);

        col = new SPBoundField();
        col.DataField = "Author";
        col.SortExpression = "Author";
        col.HeaderText = "Author";
        SPGridView1.Columns.Add(col);

        col = new SPBoundField();
        col.DataField = "Rating";
        col.SortExpression = "Rating";
        col.HeaderText = "Rating";
        SPGridView1.Columns.Add(col);
        SPGridView1.AllowSorting = true;

Step6:设置过滤字段:

SPGridView1.AllowFiltering = true;

SPGridView1.FilterDataFields = ",Author,Rating";

注意 :开始的列如果不启用过滤需要加 “,”。

Step7:继续在Page_Load写代码,设置分组:

        //设置分组
        SPGridView1.AllowGrouping = true;
        SPGridView1.AllowGroupCollapse = true;
        SPGridView1.GroupField = "Title";
        SPGridView1.GroupDescriptionField = "Title";
        SPGridView1.GroupFieldDisplayName = "分组:";
原文地址:https://www.cnblogs.com/masahiro/p/10128139.html