I solved it by
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "javascript:window.location.href ='ViewImage.aspx?ID=" + e.Row.Cells[0].Text + "';");
}
}
Remember to put OnRowDataBound ="GridView1_RowDataBound" into your aspx source.