How to add multiple SPGridView in one single custom webpart
I am writing this blog to provide a sample on how to develop a custom SharePoint webpart, which contains multiple SPGridView. While developing the sample, the challeges that I faced are how to add paging and have different datasources. However, the sample is ready now and can be used and customised to your need.
Add a new class library in VS 2008 and add reference to Microsoft.SharePoint.dll
using System;
using System.Data;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace SampleGridViewTest
{
[Guid("7586970D-0EE6-446b-875F-CFE71052C171")]
public class SampleGridViewClass : Microsoft.SharePoint.WebPartPages.WebPart
{
private SPGridView[] m_oGridView = new SPGridView[10];
private ObjectDataSource[] oDataSource = new ObjectDataSource[10];
public SampleGridViewClass()
{
}
protected override void CreateChildControls()
{
for (int k = 0; k < 10; k++)
{
m_oGridView[k] = new SPGridView();
m_oGridView[k].ID = "MyGridView" + k;
m_oGridView[k].AutoGenerateColumns = false;
m_oGridView[k].AlternatingRowStyle.BackColor = System.Drawing.Color.WhiteSmoke;
m_oGridView[k].AllowSorting = true;
m_oGridView[k].PageSize = 3;
m_oGridView[k].AllowPaging = true;
m_oGridView[k].PagerStyle.HorizontalAlign = HorizontalAlign.Center;
m_oGridView[k].AllowFiltering = true;
m_oGridView[k].FilterDataFields = "FirstName,LastName";
m_oGridView[k].FilteredDataSourcePropertyName = "FilterExpression" ;
m_oGridView[k].FilteredDataSourcePropertyFormat = "{1} LIKE '{0}'";
m_oGridView[k].RowDataBound += new GridViewRowEventHandler(m_oGridView_RowDataBound);
CreateGridViewColumns("FirstName", k);
CreateGridViewColumns("LastName", k);
oDataSource[k] = new ObjectDataSource();
string strTypeName = "SampleGridViewTest,";
strTypeName += Assembly.GetExecutingAssembly().FullName;
oDataSource[k].TypeName = String.Format("{0},{1}", this.GetType().FullName, Assembly.GetExecutingAssembly().FullName);
oDataSource[k].SelectMethod = "FillDataTable";
oDataSource[k].SelectParameters.Add("indx", k.ToString());
oDataSource[k].ID = "MyDataSource" + k;
HttpRequest oRequest = HttpContext.Current.Request;
if ((oRequest.Form["__CALLBACKID"] == null) || (oRequest.Form["__CALLBACKPARAM"] == null) || (!oRequest.Form["__CALLBACKID"].EndsWith("MyGridView" + k)))
{
if (null != ViewState["FilterExpression" + k])
{
oDataSource[k].FilterExpression = (string)ViewState["FilterExpression" + k];
}
}
Controls.Add(oDataSource[k]);
m_oGridView[k].DataSourceID = oDataSource[k].ID;
Controls.Add(m_oGridView[k]);
m_oGridView[k].PagerTemplate = null;
}
base.CreateChildControls();
}
protected override void OnPreRender(System.EventArgs e)
{
for (int k = 0; k < 10; k++)
{
m_oGridView[k].DataBind();
ViewState["FilterExpression" + k] = oDataSource[k].FilterExpression;
if (m_oGridView[k].HeaderRow != null)
{
// If the First control in the First Control(DataControlFieldHeaderCell-- "FirstName") of HeaderRow's controlcollection
// is Microsoft.SharePoint.WebControls.Menu, then update its ClientOnClickPreMenuOpen property
if (m_oGridView[k].HeaderRow.Controls[0].Controls[0].GetType().Equals(typeof(Microsoft.SharePoint.WebControls.Menu)))
{
((Microsoft.SharePoint.WebControls.Menu)m_oGridView[k].HeaderRow.Controls[0].Controls[0]).ClientOnClickPreMenuOpen = "SPGridView_FilterPreMenuOpen('" + m_oGridView[k].ClientID + "','" + m_oGridView[k].ClientID + "_SPGridViewFilterMenuTemplate', '%MENUCLIENTID%', 'FirstName', event); ";
}
// else, the second control in the First Control(DataControlFieldHeaderCell) of HeaderRow's controlcollection
// should be the menu (this because of addition of a Panel due to the call to SetGridViewFilterIcon as a First Control
else if (m_oGridView[k].HeaderRow.Controls[0].Controls[0].GetType().Equals(typeof(System.Web.UI.WebControls.Panel)))
{
if (m_oGridView[k].HeaderRow.Controls[0].Controls[1].GetType().Equals(typeof(Microsoft.SharePoint.WebControls.Menu)))
{
((Microsoft.SharePoint.WebControls.Menu)m_oGridView[k].HeaderRow.Controls[0].Controls[1]).ClientOnClickPreMenuOpen = "SPGridView_FilterPreMenuOpen('" + m_oGridView[k].ClientID + "','" + m_oGridView[k].ClientID + "_SPGridViewFilterMenuTemplate', '%MENUCLIENTID%', 'FirstName', event); ";
}
}
//same update for the Second Control (DataControlFieldHeaderCell--"LastName") of HeaderRow's controlcollection
if (m_oGridView[k].HeaderRow.Controls[1].Controls[0].GetType().Equals(typeof(Microsoft.SharePoint.WebControls.Menu)))
{
((Microsoft.SharePoint.WebControls.Menu)m_oGridView[k].HeaderRow.Controls[1].Controls[0]).ClientOnClickPreMenuOpen = "SPGridView_FilterPreMenuOpen('" + m_oGridView[k].ClientID + "','" + m_oGridView[k].ClientID + "_SPGridViewFilterMenuTemplate', '%MENUCLIENTID%', 'LastName', event); ";
}
else if (m_oGridView[k].HeaderRow.Controls[1].Controls[0].GetType().Equals(typeof(System.Web.UI.WebControls.Panel)))
{
if (m_oGridView[k].HeaderRow.Controls[1].Controls[1].GetType().Equals(typeof(Microsoft.SharePoint.WebControls.Menu)))
{
((Microsoft.SharePoint.WebControls.Menu)m_oGridView[k].HeaderRow.Controls[1].Controls[1]).ClientOnClickPreMenuOpen = "SPGridView_FilterPreMenuOpen('" + m_oGridView[k].ClientID + "','" + m_oGridView[k].ClientID + "_SPGridViewFilterMenuTemplate', '%MENUCLIENTID%', 'LastName', event); ";
}
}
}
}
base.OnPreRender(e);
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
for (int k = 0; k < 10; k++)
{
m_oGridView[k].RenderControl(writer);
m_oGridView[k].Dispose();
}
}
void m_oGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
SPGridView myGridView = (SPGridView)sender;
if ((null != sender) && (e.Row.RowType == DataControlRowType.Header))
{
string strFilteredColumn = ((SPGridView)sender).FilterFieldName;
SetGridViewFilterIcon(myGridView, strFilteredColumn, e.Row);
}
}
public void SetGridViewFilterIcon(SPGridView oGridView, string strColumn, GridViewRow oRow)
{
if ((false == string.IsNullOrEmpty(strColumn)) && (null != oRow))
{
for (int iIndex = 0; iIndex < oGridView.Columns.Count; iIndex++)
{
DataControlField oField = oGridView.Columns[iIndex];
if (oField.HeaderText == strColumn)
{
Image oFilterIcon = new Image();
oFilterIcon.ImageUrl = "/_layouts/images/ewr093.gif";
oFilterIcon.ImageAlign = ImageAlign.Left;
oFilterIcon.Style[System.Web.UI.HtmlTextWriterStyle.MarginTop] = "2px";
oFilterIcon.ID = "FilterIcon" ;
Panel oPanel = new Panel();
oPanel.Controls.Add(oFilterIcon);
oRow.Cells[iIndex].Controls.Add(oPanel);
break;
}
}
}
}
public static DataTable FillDataTable(int indx)
{
DataTable oDataTable = new DataTable("Singers");
DataRow row = null;
oDataTable.Columns.Add("FirstName", typeof(string));
oDataTable.Columns.Add("LastName", typeof(string));
row = oDataTable.NewRow();
row["FirstName"] = "Jennifer" + indx;
row["LastName"] = "Lopez" + indx;
oDataTable.Rows.Add(row);
row = oDataTable.NewRow();
row["FirstName"] = "Michael" + indx;
row["LastName"] = "Jackson" + indx;
oDataTable.Rows.Add(row);
row = oDataTable.NewRow();
row["FirstName"] = "Mark" + indx;
row["LastName"] = "Anthony" + indx;
oDataTable.Rows.Add(row);
row = oDataTable.NewRow();
row["FirstName"] = "George" + indx;
row["LastName"] = "Michael" + indx;
oDataTable.Rows.Add(row);
row = oDataTable.NewRow();
row["FirstName"] = "Elvis" + indx;
row["LastName"] = "Presley" + indx;
oDataTable.Rows.Add(row);
row = oDataTable.NewRow();
row["FirstName"] = "Elton" + indx;
row["LastName"] = "John" + indx;
oDataTable.Rows.Add(row);
return oDataTable;
}
private void CreateGridViewColumns(string strColumn,int idx )
{
BoundField oGridColumn = new BoundField();
oGridColumn.DataField = strColumn;
oGridColumn.HeaderText = strColumn;
oGridColumn.SortExpression = strColumn;
oGridColumn.Visible = true;
m_oGridView[idx].Columns.Add(oGridColumn);
}
}
}