You need to either handle the RowDataBound event:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[index].Visible = false;
}
or, you can set the AutoGenerateColumns property to false
and create either templated custom columns or DataBound Columns:
<Columns>
<asp:BoundField DataField="field1" HeaderText="Field1" />
<asp:BoundField DataField="field2" HeaderText="Field2" />
<asp:BoundField DataField="field3" HeaderText="Field3" />
...
</Columns>
The final option, is to modify your SQL statement to explicity SELECT
the fields you need instead of selecting *
:
SqlCommand PTCmd = new SqlCommand("Select field1, field2, field3 from tbl_Projects", PTConn);
However, if you need, for example, the ID, then the latter probably isn't an option. I think the first option would be the easiest and fastest to implement.