【发布时间】:2014-05-19 22:24:32
【问题描述】:
我想对网格视图的列进行排序。谁能给我解释一下过程。我需要什么方法以及如何去做。我在互联网上查找了一些,但它们似乎不起作用。我终于和 http://msdn.microsoft.com/en-us/librar/system.web.ui.webcontrols.gridview.sortexpression%28v=vs.110%29.aspx 当我点击箭头时它什么也没做。当我点击列名时,我得到: System.Web.HttpException: The GridView 'GridView1' 触发的事件排序未处理。
在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
另外,如何将图像添加到所有列?
protected void GridView1_RowCreated(Object sender, GridViewRowEventArgs e)
{
// Use the RowType property to determine whether the
// row being created is the header row.
if (e.Row.RowType == DataControlRowType.Header)
{
// Call the GetSortColumnIndex helper method to determine
// the index of the column being sorted.
int sortColumnIndex = GetSortColumnIndex();
if (sortColumnIndex != -1)
{
// Call the AddSortImage helper method to add
// a sort direction image to the appropriate
// column header.
AddSortImage(sortColumnIndex, e.Row);
}
}
}
private int GetSortColumnIndex(int p)
{
throw new NotImplementedException();
}
// This is a helper method used to determine the index of the
// column being sorted. If no column is being sorted, -1 is returned.
protected int GetSortColumnIndex()
{
// Iterate through the Columns collection to determine the index
// of the column being sorted.
foreach (DataControlField field in GridView1.Columns)
{
if (field.SortExpression == GridView1.SortExpression)
{
return GridView1.Columns.IndexOf(field);
}
}
return -1;
}
// This is a helper method used to add a sort direction
// image to the header of the column being sorted.
protected void AddSortImage(int columnIndex, GridViewRow headerRow)
{
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (GridView1.SortDirection == SortDirection.Ascending)
{
sortImage.ImageUrl = "~/images/arrowasc.png";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "~/images/arrowdesc.png";
sortImage.AlternateText = "Descending Order";
}
// Add the image to the appropriate header cell.
headerRow.Cells[2].Controls.Add(sortImage);
}
【问题讨论】: