Previously I've posted a few Helper Classes . This post describes my GridViewHelper class.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
public class GridViewHelper
{
//Copied from on WebFormsHelper class http://geekswithblogs.net/mnf/articles/89859.aspx
public static int ColumnIndexBySortExpression(DataControlFieldCollection Columns, string SortExpression)
{
for (int i = 0; i < Columns.Count; i++)
{
if (Columns[i].SortExpression == SortExpression)
{
return i;
}
}
return -1;
}
public static TableCell CellBySortExpression(GridViewRow Item, string SortExpression)
{ //actually should return DataControlFieldCell(derived from TableCell)
GridView gridView = GetParentGridView(Item);
int nColumn = ColumnIndexBySortExpression(gridView.Columns, SortExpression);
if (nColumn < 0)
{
return null;
}
else
{
return Item.Cells[nColumn];
}
}
public static GridView GetParentGridView(GridViewRow row)
{
//NOTE row.parent doesn't return GridView
GridView gridView = (GridView)row.NamingContainer;
return gridView;
}
public static string GetFieldControlTextBySortExpression(GridViewRow Item, string SortExpression)
{
//can be different DataControlFields http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.datacontrolfield.aspx
//the function is tested for ButtonField ,
// for BoundField see GetColumnValueBySortExpression
DataControlFieldCell cell = (DataControlFieldCell)CellBySortExpression(Item, SortExpression);
string sRet = null;
if (cell != null)
{
if (cell.Controls.Count > 0)
{
Control cntl = cell.Controls[0];
if ((cntl is ITextControl))
{
sRet = ((ITextControl)cntl).Text;
}
else if (cntl is IButtonControl)
{ //I beleive IButtonControl should implement ITextControl interface to be consistent with others controls with Text property.
sRet = ((IButtonControl)cntl).Text;
}
};
}
return sRet;
}
public static DataControlField ColumnBySortExpression(DataControlFieldCollection Columns, string SortExpression)
{
DataControlField oRet = null;
int i = ColumnIndexBySortExpression(Columns, SortExpression);
if (i >= 0)
{
oRet = Columns[i];
}
return oRet;
}
//see also FieldControlTextBySortExpression
public static string GetColumnValueBySortExpression(GridViewRow gvRow, string SortExpression)
{
TableCell cell = CellBySortExpression(gvRow, SortExpression);
string sText = cell.Text;
return sText;
}
/// <summary>
/// finds CheckBox in CheckBoxField
/// </summary>
/// <param name="Item"></param>
/// <param name="SortExpression"></param>
/// <returns></returns>
public static CheckBox GetCheckBoxBySortExpression(GridViewRow gvRow, string SortExpression)
{
//see http://forums.asp.net/p/1023839/1867775.aspx#1867775
DataControlFieldCell cell = (DataControlFieldCell)CellBySortExpression(gvRow, SortExpression);
CheckBox chkBox = null;
if (cell != null)
{
if (cell.Controls.Count > 0)
{
chkBox = cell.Controls[0] as CheckBox;
}
}
return chkBox;
}
public static bool SetCellBySortExpressionVisible(GridViewRow gvRow, string SortExpression, bool visible)
{
//see http://forums.asp.net/p/1023839/1867775.aspx#1867775
DataControlFieldCell cell = (DataControlFieldCell)CellBySortExpression(gvRow, SortExpression);
bool bFound=false;
//CheckBox chkBox = null;
if (cell != null)
{
if (cell.Controls.Count > 0)
{
Control cntl = cell.Controls[0];
cntl.Visible = visible;
bFound = true;
};
}
return bFound;
}
public static void HyperLinkField_SetNullDisplayText(DataControlFieldCell cell, string nullDisplayText)
{
//TODO: implement derived from HyperLinkField class and set nullDisplayTex property
// TableCell cell = (TableCell) control;
Debug.Assert((cell.ContainingField is HyperLinkField));//
if (((cell.Controls.Count < 1) || !(cell.Controls[0] is HyperLink)))
{
throw new ApplicationException("HyperLinkField_WrongControlType"+ cell.ToString());
}
HyperLink link = (HyperLink)cell.Controls[0];
link.Visible = false;
Label label = new Label();
label.Text = nullDisplayText;
label.Visible = true;
cell.Controls.Add(label);
}
public static bool IsItemNormalOrAlterating(System.Web.UI.WebControls.GridViewRowEventArgs e)
{
bool bRet = (e.Row.RowType == DataControlRowType.DataRow);
return bRet;
}
//based on http://blogs.msdn.com/mattdotson/articles/541795.aspx
public static Table GetInnerTable(GridView gv)
{
if (gv.HasControls())
{
return (Table)gv.Controls[0];
}
return null;
}
}