Solved

How to hide / show a datagrid column

Posted on 2011-03-12
3
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

729 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