我想对网格视图的列进行排序。任何人都可以解释我的过程。我需要什么方法以及如何做到这一点。我在互联网上找到了一些,但他们似乎没有工作。我终于去了 http://msdn.microsoft.com/en-us/librar/system.web.ui.webcontrols.gridview.sortexpression%28v=vs.110%29.aspx当我点击箭头时它没有做任何事情。当我点击列名时,我得到:System.Web.HttpException: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);
}
答案 0 :(得分:0)
以下是使用排序功能的网格视图的快速指南:
您的.aspx页面(包含两列的GridView):
<asp:GridView ID="gridExistingQuestions" runat="server" AutoGenerateColumns="False" GridLines="Vertical" EmptyDataText="No Question Found." AllowSorting="True" OnSorting="gridExistingInterviewQuestions_Sorting" > <Columns> <asp:TemplateField HeaderText="Question Title" SortExpression="Title"> <ItemTemplate> <asp:Label ID="lblQuestionTitle" runat="server" Text='dynamic title here'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Question Desc" SortExpression="Desc"> <ItemTemplate> <asp:Label ID="lblQuestionDesc" runat="server" Text='dynamic Desc here'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
你的aspx.cs页面
DataTable questionTable; DataView questionView; this.SetQuestionData(); //on page gridview sorting protected void gridExistingQuestions_Sorting(object sender, GridViewSortEventArgs e) { try { CurrentView = e.SortExpression + " " + this.ConvertSortDirectionToSql(e, "question"); if (questionView != null) { questionView.Sort = CurrentView; // bind your grid view here ... } else this.SetQuestionData(); } catch { // display error } } private string CurrentView { get { return (string)(ViewState["vsCurrentView"] ?? (object)null); } set { ViewState["vsCurrentView"] = value; } } //handling sorting function private string ConvertSortDirectionToSql(GridViewSortEventArgs e, string key) { ViewState[key + e.SortExpression] = ViewState[key + e.SortExpression] ?? "ASC"; ViewState[key + e.SortExpression] = (ViewState[key + e.SortExpression].ToString() == "ASC") ? "DESC" : "ASC"; return ViewState[key + e.SortExpression].ToString(); } private bool SetQuestionData() { questionTable = //get your data from db if (questionTable != null) { questionView = new DataView(questionTable); if (!string.IsNullOrEmpty(CurrentView)) QuestionView.Sort = CurrentView; // bind your grid view here return true; } return false; }
并且您也可以对页面上的所有其他网格视图使用ConvertSortDirectionToSql()方法。
希望它能为您提供线索并记住您需要在评论点绑定数据