在网上基本上没找到好的解决办法,最终还是觉得研究templateField 源码进行修改.
研究发现templateField 和bouldField 都是继承于DataControlField ,其实排序功能在DataControlField里面,所以排序功能是没问题,问题在于templateField 在重写InitializeCell时将cell里面的控件给替换了,可以参考下面代码
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
ITemplate headerTemplate = null;
switch (cellType)
{
case DataControlCellType.Header:
headerTemplate = this.headerTemplate;
break;
case DataControlCellType.Footer:
headerTemplate = this.footerTemplate;
break;
case DataControlCellType.DataCell:
headerTemplate = this.itemTemplate;
if ((rowState & DataControlRowState.Edit) == DataControlRowState.Normal)
{
if ((rowState & DataControlRowState.Insert) != DataControlRowState.Normal)
{
if (this.insertItemTemplate != null)
{
headerTemplate = this.insertItemTemplate;
}
else if (this.editItemTemplate != null)
{
headerTemplate = this.editItemTemplate;
}
}
else if (((rowState & DataControlRowState.Alternate) != DataControlRowState.Normal) && (this.alternatingItemTemplate != null))
{
headerTemplate = this.alternatingItemTemplate;
}
break;
}
if (this.editItemTemplate != null)
{
headerTemplate = this.editItemTemplate;
}
break;
}
if (headerTemplate != null)
{
cell.Text = string.Empty;
headerTemplate.InstantiateIn(cell);
}
else if (cellType == DataControlCellType.DataCell)
{
cell.Text = " ";
}
public override void InitializeCell(DataControlFieldCell cell, DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
ITemplate headerTemplate = null;
switch (cellType)
{
case DataControlCellType.Header:
headerTemplate = this.headerTemplate;
break;
case DataControlCellType.Footer:
headerTemplate = this.footerTemplate;
break;
case DataControlCellType.DataCell:
headerTemplate = this.itemTemplate;
if ((rowState & DataControlRowState.Edit) == DataControlRowState.Normal)
{
if ((rowState & DataControlRowState.Insert) != DataControlRowState.Normal)
{
if (this.insertItemTemplate != null)
{
headerTemplate = this.insertItemTemplate;
}
else if (this.editItemTemplate != null)
{
headerTemplate = this.editItemTemplate;
}
}
else if (((rowState & DataControlRowState.Alternate) != DataControlRowState.Normal) && (this.alternatingItemTemplate != null))
{
headerTemplate = this.alternatingItemTemplate;
}
break;
}
if (this.editItemTemplate != null)
{
headerTemplate = this.editItemTemplate;
}
break;
}
if (headerTemplate != null )
{
cell.Text = string.Empty;
headerTemplate.InstantiateIn(cell);
if (cellType == DataControlCellType.Header)
{
this.HeaderText = cell.Text;
base.InitializeCell(cell, cellType, rowState, rowIndex);
}
}
else if (cellType == DataControlCellType.DataCell)
{
cell.Text = " ";
}
}
进测试可以正常运行.
这是针对那些需要在templateField里需要使用HeaderTemplate的,如果不使用就不必修改代码,当然也就要在 templateField里使用HeaderText属性赋值,很多人就是没有附值,而使sort功能失效的其原因是DataControlField里面(if (headerText.Length == 0) { headerText = " "; })给屏蔽了.