Visual Studio .NET - How do you hide a datagrid column dynamically?
Asked By Burak Gunay
17-Nov-05 09:27 AM
Hi, I only want to display a datagrid column under certain conditions.
I wrote the code below, but i got back an "error creating control".
<asp:datagrid id="dtgDetails" AutoGenerateColumns="False" Runat="server">
<Columns>
<asp:BoundColumn Headertext="Compo" DataField="compo_desc"></asp:BoundColumn>
<% if boolGrid = 1 then %>
<asp:BoundColumn Headertext="Grade" DataField="grade_desc"></asp:BoundColumn>
<% end if %>
</Columns>
</asp:datagrid>
I defined boolGrid as a booleanvalue in the code behind.
Would appreciate your help.
Thanks,
Burak
Visible=False
Hi Burak,
To make a datagrid column hidden under certain conditions, you can set the Visible property of the datagrid column to False.
Hope this helps.
How about this
<asp:BoundColumn Headertext="Grade" DataField="grade_desc" Visible="<=%boolGrid %>"></asp:BoundColumn>
this is what I am trying to do
Ronald,
I have 2 tables with the same fields and 1 table with one additional field. I am writing a user control to query these tables and tie the dataset to the datagrid. I am passing in the table name to a stored procedure.
I want the extra datagrid column to only show up if I query the table with the extra field.
Let me work on hidign the column from the code behind and see how that goes.
Thanks,
Burak
i get an error
if i write this
<asp:BoundColumn Headertext="Grade" DataField="grade_desc"
Visible="<%=boolGrid %>"></asp:BoundColumn>
i get a control creation error.
if i write the folloiwng, i don't get any creation errors
<asp:BoundColumn Headertext="Grade" DataField="grade_desc"
Visible="<%#boolGrid %>"></asp:BoundColumn>
but when i run it, i get
'DataBinding' is not an event of 'System.Web.UI.WebControls.BoundColumn'
the problem is at databinding level
if the datagrid column is expecting a datavalue and if the dataset does not contain that value, then you get an error when you bind it.
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<asp:Label ID="lbl" Runat="server">
<%# Container.DataItem("grade_desc") %>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
which means i can only use the user control for the 2 tables with the same fields and not for the one table that has 1 extra field.
if you have any ideas let me know.
Alternative
Hi Burak,
One option that I can think of to avoid this problem is to return the missing column in the second table and just use NULL as its value. This should solve the problem with the data binding. Then after databinding, check if it's from the second table, then hide the column.
Hope this helps.
doing this worked for me

ronald,
i put this code in the grid
<asp:TemplateColumn HeaderText="Grade">
<ItemTemplate>
<asp:Label ID="lbl" Runat="server">
<% if boolGrid = "true" then %>
Container.DataItem("grade_desc")
<% else %>
Container.DataItem("compo_desc")
<% end if %>
</asp:Label>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
and then tweaked it in the code behind like this
Dim strCnn, strSql As String
strCnn = ConfigurationSettings.AppSettings("ConnectionString")
Dim cnn As New SqlConnection(strCnn)
Dim cmd As New SqlCommand
cmd.Connection = cnn
With cmd
.CommandType = CommandType.StoredProcedure
.Parameters.Add(New SqlParameter("@COMPO", SqlDbType.Char, 1)).Value = ""
End With
If boolGrid = False Then
cmd.Parameters.Add(New SqlParameter("@TABLE", SqlDbType.VarChar, 30)).Value = "Audit_Crs_grad"
cmd.CommandText = "Audit_IdenticalTablesSearch"
Else
cmd.Parameters.Add(New SqlParameter("@GRADE", SqlDbType.Char, 3)).Value = ""
cmd.CommandText = "Audit_TRV_ALLT_Search"
End If
Dim ds As New DataSet
Dim cmdAdapter As New SqlDataAdapter(cmd)
cmdAdapter.Fill(ds, "Results")
If boolGrid = False Then
dtgDetails.Columns(1).Visible = False
End If
dtgDetails.DataSource = ds.Tables("Results")
dtgDetails.DataBind()
and it worked for me.

Visual Studio .net .NET Framework Hi NG, ich habe vor längerer Zeit mit Visual Studio .Net 2003 gearbeitet und überlege momentan auf einen neueren Stand upzudaten. Ein Visual Studio .Net 2008 scheint es nicht zu geben. Habe zumindest mit googeln nichts gefunden. Was
Wise for Visual Studio.NET Wise for Visual Studio.NET By Peter A. Bromberg, Ph.D. To "Print This Page" Link Peter Bromberg Wise for Visual Studio .NET is a total and complete installation development system for creating and editing Windows® Installer
Visual Studio versioning . . . . how to tell? .NET Framework To my knowledge, Visual studio 6 was released in 1998, then Visual Studio .NET 2002 is VS 7, then Visual Studio .NET 2003 is VS 7.1, then Visual
visual studio.net 2003 and Access 2007 database .NET Framework Hi I am currently using Visual Studio.Net 2003 running on Windows Server 2000 operating system. I have used Visual Studio.net 2003 connecting to Access 2002 databases in the pass with great success. Now
Is Visual Studio self-hosting ? .NET Framework Does Microsoft use Visual Studio IDE, Visual Studio Debugger, Visual Studio Linker and Visual Studio compiler for developing Visual Studio ? Or is Visual Studio not