在Net 中 DataGrid 虽然有排序的功能但并不支持双向的排序用到了看了些相关的帖子自己尝试了一种方法竟然也行得通主要是用DataGridAttributes 存了一个参数同时在onSortCommand中修改了DataGridColumn的SortExpression 代码如下
private void BindData()
{
DataTable dt = ;
if(dt != null)
{
DataView dv = dtDefaultView;
if(DataGridAttributes[SortBy] != null)
{
dvSort = DataGridAttributes[SortBy];
}
DataGridDataSource = dv;
DataGridDataBind();
}
}
private void DataGridSort(object source SystemWebUIWebControlsDataGridSortCommandEventArgs e)
{
DataGridAttributes[SortBy] = sortstr;
thisBindData();
//找到排序的列并修改把它的排序属性
DataGridColumn clm = null;
for(int i=;i<DataGridColumnsCount;i++)
{
if(DataGridColumns[i]SortExpression == eSortExpression )
{
clm = DataGridColumns[i];
break;
}
}
if(clm == null) return;
if(eSortExpressionToLower()IndexOf(desc) > )
{
clmSortExpression = eSortExpressionToLower()Replace(descasc);
}
else
{
if(eSortExpressionToLower()IndexOf(asc) > )
{
clmSortExpression = eSortExpressionToLower()Replace(ascdesc);
}
else
{
clmSortExpression = eSortExpressionToLower() + desc;
}
}
}