自己构造公共程序(3)--DataGrid小方法
using System;
using System.Web.UI.WebControls;
using System.Data;
namespace RoleCom
{
/// <summary>
/// DataGridTools 的摘要说明。
/// </summary>
public class DataGridTools
{
public DataGridTools()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 功能: 添加排序
/// 创建人: Applegreen
/// 更新时间: 2004-8-6
/// </summary>
public static void SortDataGrid(DataTable dt, DataGrid dg, DataGridSortCommandEventArgs e)
{
dg.CurrentPageIndex=0;
string sortKeyValue = dg.Attributes[e.SortExpression];
bool isASC = false;
if (sortKeyValue==null || sortKeyValue.Length<1)
{
dg.Attributes.Add(e.SortExpression,isASC.ToString());
}
else
{
isASC = !bool.Parse(sortKeyValue);
dg.Attributes[e.SortExpression] = isASC.ToString();
}
string sortString = buildSortString(e.SortExpression,isASC);
if (dt.ExtendedProperties["sortString"]==null)
{
dt.ExtendedProperties.Add("sortString",sortString);
}
else
{
dt.ExtendedProperties["sortString"]=sortString;
}
dg.Attributes.Add("currentSortExpression",e.SortExpression);
dg.Attributes.Add("currentIsAsc",isASC.ToString());
dg.Attributes.Add("currentSortString",sortString);
dg.DataSource = SortTable(dt,dt.ExtendedProperties["sortString"].ToString());
dg.DataBind();
}
/// <summary>
/// 功能: 添加分页
/// 创建人: Applegreen
/// 更新时间: 2004-8-6
/// </summary>
public static void DoDataGridPaging(DataGrid dataGrid,DataTable dataTable,DataGridPageChangedEventArgs e)
{
string sortString=dataGrid.Attributes["currentSortString"];
DataView dv=dataTable.DefaultView;
if (sortString==null || sortString.Length<1)
{
}
else
{
dv=SortTable(dataTable,sortString);
}
dataGrid.CurrentPageIndex=e.NewPageIndex;
dataGrid.DataSource=dv;
dataGrid.DataBind();
}
/// <summary>
/// 功能: 数据表排序
/// 创建人: Applegreen
/// 更新时间: 2004-8-6
/// </summary>
public static DataView SortTable(DataTable dt, string sortString)
{
DataView dv = new DataView(dt);
dv.Sort=sortString;
return dv;
}
/// <summary>
/// 功能: 排序字符串
/// 创建人: Applegreen
/// 更新时间: 2004-8-6
/// </summary>
public static string SortDataGrid(DataGrid dg,DataGridSortCommandEventArgs e)
{
string sortKeyValue = dg.Attributes[e.SortExpression];
bool isASC = false;
if (sortKeyValue==null || sortKeyValue.Length<1)
{
dg.Attributes.Add(e.SortExpression,isASC.ToString());
}
else
{
isASC = !bool.Parse(sortKeyValue);
dg.Attributes[e.SortExpression] = isASC.ToString();
}
if (isASC)
{
return e.SortExpression+",ASC";
}
dg.CurrentPageIndex = 0;
return e.SortExpression+",DESC";
}
/// <summary>
///功能: 排序字符串
///创建人: Applegreen
///更新时间: 2004-8-6
/// </summary>
public static string buildSortString(string sortByField, bool isASC)
{
sortByField = wrapFieldName(sortByField) + " ";
if (isASC)
{
sortByField += "ASC";
}
else
{
sortByField += "DESC";
}
return sortByField;
}
/// <summary>
/// 功能: 名字中有空格字段
/// 创建人: Applegreen
/// 更新时间: 2004-8-6
/// </summary>
public static string wrapFieldName(string FieldName)
{
if (FieldName==null)
{
throw new Exception("MyWrap->wrapFieldName: FieldName is null");
}
if (FieldName.Length<1)
{
throw new Exception("MyWrap->wrapFieldName: FieldName is blank");
}
if (FieldName.Substring(0,1)=="[")
{
if (FieldName.Substring(FieldName.Length-1)=="]")
{
return FieldName;
}
else
{
throw new Exception("MyWrap->wrapFieldName: FieldName syntax is error");
}
}
else
{
string tmp = FieldName.Replace("]","]]");
return "[" + tmp + "]";
}
}
}
}