SharePoint GridView的使用,DataSource的实现

SharePoint GridView使用可以通过两种方式来使用:
1. 直接给SPGridVew的DataSource属性赋值一个DataTable, 之后调用BindData方法。缺点是只使用SPGridView来显示数据,而排序,分页,过滤和其他的功能都不可用。
2. 通过SPGridView的DataSourceId绑定一个DataSource控件,之后就可以通过这个DataSource控件实现SPGridView的排序,分页,过滤等功能。

关于SPGridView的菜单项,排序分页和过滤的实现可以参考:http://www.cnblogs.com/ericfine/archive/2008/10/22/1316431.html
http://blog.csdn.net/ericfine/archive/2008/10/23/3130387.aspx

然而我的实现方式并和他一样,在上面链接的文章当中,SPGridView的排序可以使用,过滤也只能过滤一个条件,同时还要一个缺点,如果要显示的数据有上千甚至上万条的数据时,SPGridView还是将全部的数据导入到DataTable中,结果却只显示其中的10(SPGridView的PageSize)条,这样不但速度效率会非常的慢。

如下是我的实现方式:使用Microsoft.SharePoint.WebControlsDataTableDataSourceView和System.Web.UI.DataSourceControl控件。
WebControlsDataTableDataSourceView是SharePoint实现的一个DataSourceView的派生类。它的主要方法有

ContractedBlock.gif ExpandedBlockStart.gif Code
IEnumerable Select(DataSourceSelectArguments selectArguments)

我就不多说了,贴代码给大家直接看吧:

页面:

ContractedBlock.gif ExpandedBlockStart.gif Code
<Mine:MyDataSource runat="server" ID="DataSource" ViewName="FilterJob" ></Mine:MyDataSource>
                        
<SharePoint:SPGridView PagerSettings-Visible="true" AllowPaging="true" ID="ViewList" runat="server" AllowFiltering="true" FilteredDataSourcePropertyFormat="{1} = '{0}'" FilteredDataSourcePropertyName="FilterExpression"
                         FilterDataFields
=",JobId,PlanName,StartTime,EndTime,Status,Progress" DataSourceID="DataSource" AutoGenerateColumns="False" AllowSorting="True" BorderStyle="None" CssClass="ms-listviewtable" GridLines="None"  Width="100%">    
                            
<SelectedRowStyle CssClass="ms-selectednav" Font-Bold="True" />
                            
<AlternatingRowStyle CssClass="ms-alternating" />
          
</SharePoint:SPGridView>

由于为了实现国际化,Columns的添加在后台,我就不介绍了,上面的那两个链接。

自定义DataSource的实现:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
[ToolboxData("<{0}:MyDataSource runat=server></{0}:MyDataSource>")]
    
public class MyDataSource : DataSourceControl
ExpandedBlockStart.gifContractedBlock.gif    
{
        
private ACDataSourceView mView;
        
private string mViewName;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private static string[] mViewNames = new string[] "FrontEnd""ScanJob""ScanReport""FilterJob""FilterReport""ManualScan""ContentFilter""RealTimeReport" ,"Test"};

        
// Methods
        protected virtual ACDataSourceView CreateView()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int num = -1;
            
if (!String.IsNullOrEmpty(this.ViewName) && (num = GetViewNumber()) != -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
switch (num)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
case 0:
                        
return new ACFrontEndDataSourceView(thisthis.ViewName, this.Context);
                    
case 1:
                        
return new ACJobDataSourceView(thisthis.ViewName, this.Context);
                    
case 2:
                        
return new ACScanReportDataSourceView(thisthis.ViewName, this.Context);
                    
case 3:
                        
return new ACJobDataSourceView(thisthis.ViewName, this.Context, true);
                    
case 4:
                        
return new ACFilterReportDataSourceView(thisthis.ViewName, this.Context);
                    
case 5:
                        
return new ACPlanListView(thisthis.ViewName, this.Context, false);
                    
case 6:
                        
return new ACPlanListView(thisthis.ViewName, this.Context, true);
                    
case 7:
                        
return new ACScanReportDataSourceView(thisthis.ViewName, this.Context);
                    
case 8:
                        
return new ACTestDataSourceView(thisthis.ViewName, this.Context);
                    
default:
                        
break;
                }

            }

            
return null;
        }


        
private int GetViewNumber()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
for (int i = 0; i < mViewNames.Length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (mViewNames[i] == mViewName)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
return i;
                }

            }

            
return -1;
        }


        
protected override DataSourceView GetView(string viewName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return this.View;
        }


        
protected override ICollection GetViewNames()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return mViewNames;
        }


        
private void LoadCompleteEventHandler(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
this.SelectParameters.UpdateValues(this.Context, this);
        }


        
protected override void LoadViewState(object savedState)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Pair pair 
= (Pair)savedState;
            
if (savedState != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
base.LoadViewState(pair.First);
                
if (pair.Second != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.View.LoadViewState(pair.Second);
                }

            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
base.LoadViewState(null);
            }

        }


        
protected override void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.OnInit(e);
            
this.Page.LoadComplete += new EventHandler(this.LoadCompleteEventHandler);
        }


        
protected override object SaveViewState()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            Pair pair 
= new Pair();
            pair.First 
= base.SaveViewState();
            
if (this.mView != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                pair.Second 
= this.mView.SaveViewState();
            }

            
if (pair.First != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return pair;
            }

            
if (pair.Second != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return pair;
            }

            
return null;
        }


        
protected override void TrackViewState()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
base.TrackViewState();
            
if (this.mView != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.mView.TrackViewState();
            }

        }


        
// Properties
        [MergableProperty(false), DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerProperty)]
        
public ParameterCollection SelectParameters
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.View.SelectParameters;
            }

        }


        
protected ACDataSourceView View
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if (this.mView == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
this.mView = this.CreateView();
                    
if (base.IsTrackingViewState)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
this.mView.TrackViewState();
                    }

                }

                
return this.mView;
            }

        }


        
public string ViewName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.mViewName;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                mViewName 
= value;
            }

        }


        
public string FilterExpression
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.View.FilterExpression;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.View.FilterExpression = value;
            }

        }


        
public bool IsFilter
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return this.View.IsFilter;
            }

            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
this.View.IsFilter = value;
            }

        }

 DataSourceView的实现在后面的文章再介绍。

转载于:https://www.cnblogs.com/wangzhiyang/archive/2009/02/02/1382490.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值