Here is how I'm populating my GridView control. I'm doing this from the code-behind, and not from the .aspx front end. The following is an extremely abbreviated version of what I have:
private void UpdateGridView()
{
DataTable temptable = new DataTable();
DataColumn idcol = new DataColumn();
DataColumn titlecol = new DataColumn();
idcol.ColumnName = "ID";
titlecol.ColumnName = "Title";
temptable.Columns.Add(idcol);
temptable.Columns.Add(titlecol);
...(get data from the database, store it as variable "x")...
DataRow tempdr;
tempdr[idcol] = x.ID;
tempdr[titlecol] = x.Title;
temptable.Rows.Add(tempdr);
GridView1.DataSource = temptable;
GridView1.DataBind();
}
To handle paging the GridView's "AllowPaging" set to true, and I have the following event handler:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
UpdateGridView();
}
And this works great!
However, I also have the RowDataBound event handler:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false; //hide the ID
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = "location.href='newsindex.aspx?NewsArticleID=" + e.Row.Cells[0].Text + "'";
}
}
My goal here is to have the rows themselves be clickable, and lead to another page with a query string that equals that row's ID. I need the value in the ID column so that I can access it when creating the row, so that I can add the ID to the QueryString of the link. But I don't want the ID column to be visible, so I added in the line: e.Row.Cells[0].Visible = false;
Doing this breaks the paging functionality. The page numbers no longer show up. If I comment out this one line, it all works, but the ID is visible in the GridView.
1) Why? 2) What can I do to get the same functionality, but with the fewest changes possible?