private void BindData()
{
DataTable dt = .......;
if(dt != null)
{
DataView dv = dt.DefaultView;
if(DataGrid1.Attributes["SortBy"] != null)
{
dv.Sort = DataGrid1.Attributes["SortBy"];
}
DataGrid1.DataSource = dv;
DataGrid1.DataBind();
}
}
private void DataGridSort(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
DataGrid1.Attributes["SortBy"] = sortstr;
this.BindData();
//找到排序的列,并修改把它的排序属性
DataGridColumn clm = null;
for(int i=0;i<DataGrid1.Columns.Count;i++)
{
if(DataGrid1.Columns[i].SortExpression == e.SortExpression )
{
clm = DataGrid1.Columns[i];
break;
}
}
if(clm == null) return;
if(e.SortExpression.ToLower().IndexOf("desc") > 0)
{
clm.SortExpression = e.SortExpression.ToLower().Replace("desc","asc");
}
else
{
if(e.SortExpression.ToLower().IndexOf("asc") > 0)
{
clm.SortExpression = e.SortExpression.ToLower().Replace("asc","desc");
}
else
{
clm.SortExpression = e.SortExpression.ToLower() + " desc";
}
}
} |