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
2009-09-15 15:11:30
Auto generated columns are not included in the column collection.
SchwartzE
2009-09-15 15:21:08
Do it in the DataBound event at which point I'm pretty sure the autogenerated columns will be in the columns collection.
Lazarus
2009-09-15 15:30:05
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
2009-09-15 15:16:25
+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
2009-09-15 15:23:26
+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
2010-01-15 09:48:51