public class GridViewEX:GridView
{
public delegate void BindEventHandler();
DataSet _ds;
[Description("自定义的DataSet类型数据源"), Category("扩展")]
public virtual DataSet DataSetSource
{
get
{
return _ds;
}
set
{
_ds = value;
}
}
public event BindEventHandler Bind;
public virtual void OnBind()
{
if (Bind != null)
{
Bind();
if (DataSetSource != null)
{
DataView dv = DataSetSource.Tables[0].DefaultView;
dv.Sort = SortExpressionStr;
this.DataSource = dv;
this.DataBind();
}
}
}
[Description("排序表达式"), Category("扩展")]
protected virtual string SortExpressionStr
{
get
{
if (ViewState["SortExpression"] == null)
{
return null;
}
return ViewState["SortExpression"].ToString();
}
set
{
ViewState["SortExpression"] = value;
}
}
[Description("排序方向"), Category("扩展")]
protected virtual string SortDirectionStr
{
get
{
if (ViewState["SortDirection"] == null)
{
return "DESC";
}
if (ViewState["SortDirection"].ToString().ToLower() != "asc" && ViewState
["SortDirection"].ToString().ToLower() != "desc")
{
return "DESC";
}
return ViewState["SortDirection"].ToString();
}
set
{
ViewState["SortDirection"] = value;
}
}
public string SortExpressionEx
{
get
{
if (ViewState["SortExpressionEx"] == null)
{
return null;
}
return ViewState["SortExpressionEx"].ToString();
}
set
{
ViewState["SortExpressionEx"] = value;
}
}
protected override void OnSorting(GridViewSortEventArgs e)
{
SortExpressionEx = e.SortExpression;
SortExpressionStr = e.SortExpression + " " + SortDirectionStr;
OnBind();
if (SortDirectionStr.ToLower() == "asc")
{
SortDirectionStr = "DESC";
}
else
{
SortDirectionStr = "ASC";
}
}
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
try
{
OnBind();
}
catch
{
}
}
base.OnLoad(e);
}
}