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.