Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Showing and hiding grid columns

A grid shows every column it is given by default. Often you want to hide the ones that only matter to the code.

Two ways to control a column

  • Set the column visibility in the grid definition so it never renders.
  • Choose the columns yourself rather than letting the grid generate them from the data.
C#
productsGrid.AutoGenerateColumns = false;
productsGrid.Columns.Add(new BoundField
{
    DataField = "Name",
    HeaderText = "Product"
});

Why choose columns explicitly

Auto generated columns follow the order of the query, which changes when the query changes. Choosing columns keeps the display stable and hides internal identifiers from visitors.

Hide, do not forget. A hidden identifier is still in the data and still available to your code. That is exactly what you want for an edit or delete action.

Binding the data in the first place is covered in binding data with a data source control.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.