继承DataGrid后,加了下面这二个属性[Description("获得或者设置数据源表"),Category("自定义类别"),Bindable(true),DefaultValue(null)]
public object DataSourceTable
{
get
{
if(this.ViewState["DataSourceTable"] == null)
{
return null;
}
else
{
return this.ViewState["DataSourceTable"];
}
}
set
{
this.ViewState["DataSourceTable"] = value;
this.DataSourceType = value.GetType().ToString();
}
}[Description("获得或者设置数据源表类型"),Category("自定义类别"),Bindable(true),DefaultValue(null)]
private string DataSourceType
{
get{return this.ViewState["DataSourceType"].ToString();}
set{this.ViewState["DataSourceType"] = value;}
}/// <summary>
/// 处理排序的事件处理函数
/// </summary>
/// <param name="e"></param>
protected override void OnSortCommand(DataGridSortCommandEventArgs e)
{
if(this.DataSourceTable == null)
{
throw new Exception("未设置数据源表");
}
else
{ this.SortField = e.SortExpression.ToLower();
string up = "<font style='FONT-SIZE: 12pt; COLOR: #009999; FONT-FAMILY: Webdings'>5</font>";
string down = "<font style='FONT-SIZE: 12pt; COLOR: #009999; FONT-FAMILY: Webdings'>6</font>";
string sort = "";
foreach(DataGridColumn column in this.Columns)
{
if(column.SortExpression == e.SortExpression)
{
string str = column.HeaderText;
if(str.IndexOf(up) + str.IndexOf(down) == -2)
{
column.HeaderText = column.HeaderText + up;
this.IsSortAscending = true;
sort = this.SortField + " Asc";
}
else if(str.IndexOf(up) != -1)
{
column.HeaderText = column.HeaderText.Replace(up,down);
this.IsSortAscending = false;
sort = this.SortField + " Desc";
}
else if(str.IndexOf(down) != -1)
{
column.HeaderText = column.HeaderText.Replace(down,up);
this.IsSortAscending = true;
sort = this.SortField + " Asc";
}
}
else
{
column.HeaderText = column.HeaderText.Replace(up,"").Replace(down,"").Trim();
}
} DataView dv = new DataView();
if(this.DataSourceType == typeof(DataSet).ToString())
{
DataSet ds = (DataSet)this.DataSourceTable;
if(ds.Tables.Count == 1)
{
dv.Table = ds.Tables[0];
}
else if(ds.Tables.Count > 1)
{
dv.Table = ds.Tables[this.DataMember];
}
else
{
throw new Exception("数据源不包括任何数据成员.");
}
}
else if(this.DataSourceType  == typeof(DataTable).ToString())
{
dv.Table = (DataTable)this.DataSourceTable;
}
else
{
throw new Exception("无法处理转换.");
}
dv.Sort = sort;
this.DataSource = dv;
this.DataBind();
dv.Dispose();
base.OnSortCommand(e);
}
}这二个属性用来保存数据源和数据源的类型.用下面方法调用=======================================================SqlDataAdapter da = new SqlDataAdapter("select top 10 OrderID,ShipName,ShipAddress from orders order by OrderID DESC","server=192.168.1.106;database=northwind;uid=sa;pwd=sa;");
DataSet ds = new DataSet();
da.Fill(ds);
da.Dispose();
da = new SqlDataAdapter("select top 10 OrderID,ShipName,ShipAddress from orders order by OrderID DESC","server=192.168.1.106;database=northwind;uid=sa;pwd=sa;");
da.Fill(ds,"table2");
da.Dispose();
this.PowerDataGrid1.DataSource = ds;
this.PowerDataGrid1.DataMember = ds.Tables[1].TableName;
this.PowerDataGrid1.DataSourceTable = ds;
this.PowerDataGrid1.DataBind();============================================================================
问题如下:
 Sort事件时,this.DataSourceType 的值是下确的.
         但 (DataSet)this.DataSourceTable却成了一个有架构没记录行的记录集.

解决方案 »

  1.   

    this.PowerDataGrid1.DataBind();后面加上
    ds.dispose();就会出错上述问题,
    如果不马上清空ds的话就正常????????????????我明明把ds保存到DataGrid的ViewState中了,怎么就不行了呢???
      

  2.   

    DataSet将数据保存在内存中,你一清空的话,数据就丢失了。
      

  3.   

    楼主的代码看不出什么问题,不会
    先问个问题给楼主:你为什么不做成System.Web.UI.UserControl,这样派生的话如何在aspx页面上使用?它会提示在
    未能从程序集 System.Web, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a 中加载类型 System.Web.UI.WebControls.PowerDataGrid
    你用<%@ Import Namespace="...." %>???
      

  4.   

    你的那个type是不是也要用视图保存?不然当我要获取DataSource的时候是靠那个type来判断的,不保存type的话怎么辨别是什么类型的datasource?type的变量在回发后就清掉了阿
      

  5.   

    另外你的DataGrid怎么绑定的?
    this.PowerDataGrid1.DataSourceTable = ds;??还是this.PowerDataGrid1.DataSource = ds才行?
    this.PowerDataGrid1.DataBind();