Friday, May 12, 2006

How do I hide a column in my Datagrid if AutoGenerateColumns is set to True?

Question: How do I hide a column in my Datagrid if AutoGenerateColumns is set to True?

Answer: AutoGenerated columns do not appear in the Datagrid's Columns() collection, and so the usual method of hiding a Datagrid column will fail:

'Will NOT work for AutoGenerated columns:
Datagrid1.Columns(1).Visible = False

So the place to handle this is in the ItemDataBound event of the Datagrid:

<asp:DataGrid id="Datagrid1" runat="server" AutoGenerateColumns="True" OnItemDataBound="Datagrid1_OnItemDataBound"/>

Codebehind
Private Sub Datagrid1_OnItemDataBound(s As Object, e As DatagridItemEventArgs)
e.Item.Cells(1).Visible = False
End Sub

1 comment:

Anonymous said...

It is reserve, neither it is more, nor it is less

Common Software Design Patterns Explained with Examples

Common Software Design Patterns Explained with Examples Introduction to Software Design Patterns Definition and Purpose Software des...