views:

632

answers:

3

I have a gridview that uses autogenerated columns, because the user can select the columns to return in a query. I want to hide the column with the identity. How do I hide the autogenerated column? Even in the databound event the columns count is zero.

A: 

I'd check the column was greater than zero, if so then I'd use the fact that the column collection can be referenced by column name as well as integer to set the identity column to hidden.

Lazarus
Auto generated columns are not included in the column collection.
SchwartzE
Do it in the DataBound event at which point I'm pretty sure the autogenerated columns will be in the columns collection.
Lazarus
A: 

Do you need it? The simplest thing would be to not include it in the select query.

If you need it and know the column position:

gridView.Columns[KnownColumnIndex].Visible = false;
HectorMac
I need the index to include as the datakey for selecting the row.
SchwartzE
+2  A: 

I discovered how to do this. You need to use the rowdatabound event and hide the cell when the row is bound.

Protected Sub ResultGrid_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles ResultGrid.RowDataBound
        e.Row.Cells(1).Visible = False
End Sub
SchwartzE
+1, Only thing to watch for is that you change it for appropriate RowTypes. For example if (e.Row.RowType != DataControlRowType.Pager) { e.Row.Cells[1].Visible = false; }
Martin Clarke