Solved

How to hide / show a datagrid column

Posted on 2011-03-12
3
694 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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