protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridView gridView = (GridView)sender;
StringBuilder sb = new StringBuilder();
// For the header row add a link to each header
// cell which can call the HideCol javascript method
if (e.Row.RowType == DataControlRowType.Header)
{
// Loop through each cell of the row
for (int columnIndex = 0; columnIndex < e.Row.Cells.Count; columnIndex++)
{
// Build the hide column link
sb.Remove(0, sb.Length); // first empty the StringBuilder
sb.Append("javascript:HideCol('");
sb.Append(gridView.ClientID);
sb.Append("', ");
sb.Append(columnIndex);
sb.Append(", '");
sb.Append(columnNames[columnIndex]);
sb.Append("');");
// Build the "Hide Column" HyperLink control
HyperLink hideLink = new HyperLink();
hideLink.Text = "-";
hideLink.CssClass = "gvHideColLink";
hideLink.NavigateUrl = sb.ToString();
hideLink.Attributes.Add("title", "Hide Column");
// Add the "Hide Column" HyperLink to the header cell
e.Row.Cells[columnIndex].Controls.AddAt(0, hideLink);
// If there is column header text then
// add it back to the header cell as a label
if (e.Row.Cells[columnIndex].Text.Length > 0)
{
Label columnTextLabel = new Label();
columnTextLabel.Text = e.Row.Cells[columnIndex].Text;
e.Row.Cells[columnIndex].Controls.Add(columnTextLabel);
}
}
}
// Give each row an id
if (e.Row.RowType == DataControlRowType.Pager)
e.Row.Attributes.Add("id", gridView.ClientID + "_pager");
else
e.Row.Attributes.Add("id", gridView.ClientID + "_r" + e.Row.RowIndex.ToString());
}