Solved

How to hide / show a datagrid column

Posted on 2011-03-12
3
683 Views
Last Modified: 2012-05-11
I have a datagrid with a few columns. In my ItemDatabound event I would like to show the CollectionDate column based on certain condiition, how do I access the bound column in the item data bound event. The default visibility is set to false for this Collection Date cplumn
<Columns>     
                <asp:TemplateColumn HeaderText="Patient Name"  >
                    <ItemTemplate >
                        <asp:HyperLink ID="hlPatientName" runat="server" NavigateUrl='<%# Eval("PatientId","Patients.aspx?PatientId={0}" ) %>'
                            Text='<%# Eval("PatientName") %>'></asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateColumn>             
                <asp:BoundColumn HeaderText="Patient ID" DataField="PatientId" Visible="false">
                </asp:BoundColumn>  
                 <asp:BoundColumn HeaderText="OrderID" DataField="Accession" Visible="true">
                </asp:BoundColumn>                
                <asp:BoundColumn HeaderText="Physician Name" DataField="PhysicianName" Visible="true">
                </asp:BoundColumn>  
                 <asp:BoundColumn HeaderText="Collection Date" DataField="CollectionDate" DataFormatString="{0:MM/dd/yyyy}" Visible="false">
                </asp:BoundColumn>          
            </Columns>

Open in new window

0
Comment
Question by:countrymeister
  • 2
3 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35116195
Hi,

Do you want to hide the entire column based on the values of individual rows? or just not show the value of the column for some rows depending on the row values?
0
 
LVL 1

Author Comment

by:countrymeister
ID: 35116285
i want to hide the column
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
ID: 35116336
Ok, if you want to hide the entire column  you should not do it in the ItemDataBound event, since this event will fire for every row in you datagrid.

So you can define a variable in your page that based in the conditions you want to validate, it will be set to true or false to know if the columns shows up or not.

Public blnShowColumn As Boolean

In you ItemDataBoundEvent make your validations

IF Something = SomethingElse then
   blnShowColumn  = True
Else
   blnShowColumn = False
End if

And just after you Call the DataBind method, hide/show the column

YourDataGrid.Columns(thecolumnidex).Visible = blnShowColumn

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question