I want to sort the columns of the grid view. Can anyone explain me the process. What methods I need and how to do it. I looked on the internet found some but they seem not to work. I finally went with http://msdn.microsoft.com/en-us/librar/system.web.ui.webcontrols.gridview.sortexpression%28v=vs.110%29.aspx and when I click on the arrow it doesn't do anything. When I click on the column name I get: System.Web.HttpException: The GridView 'GridView1' fired event Sorting which wasn't handled.
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Also, how can I add the image to all columns?
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);
}