protected override void Render(HtmlTextWriter writer)
{
// Only add the sorting UI if the GridView is sorted
if (!string.IsNullOrEmpty(GridView1.SortExpression))
{
// Determine the index and HeaderText of the column that
// the data is sorted by
int sortColumnIndex = 1;
string sortColumnHeaderText = String.Empty;
for (int i = 0; (i <= GridView1.Columns.Count); i++)
{
if ((GridView1.Columns[i].SortExpression.CompareTo(GridView1.SortExpression) == 0))
{
sortColumnIndex = i;
sortColumnHeaderText = GridView1.Columns[i].HeaderText;
break;
}
}
// Reference the Table the GridView has been rendered into
Table gridTable = (Table)GridView1.Controls[0];
// Enumerate each TableRow, adding a sorting UI header if
// the sorted value has changed
string lastValue = string.Empty;
foreach (GridViewRow gvr in GridView1.Rows)
{
string currentValue = gvr.Cells[sortColumnIndex].Text;
if (lastValue.CompareTo(currentValue) != 0)
{
// there's been a change in value in the sorted column
int rowIndex = gridTable.Rows.GetRowIndex(gvr);
// Add a new sort header row
GridViewRow sortRow = new GridViewRow(rowIndex, rowIndex, DataControlRowType.DataRow, DataControlRowState.Normal);
TableCell sortCell = new TableCell();
sortCell.ColumnSpan = GridView1.Columns.Count;
sortCell.Text = string.Format("{0}: {1}", sortColumnHeaderText, currentValue);
sortCell.CssClass = "SortHeaderRowStyle";
// Add sortCell to sortRow, and sortRow to gridTable
sortRow.Cells.Add(sortCell);
gridTable.Controls.AddAt(rowIndex, sortRow);
// Update lastValue
lastValue = currentValue;
}
}
}
base.Render(writer);
}
Bookmarks