Solved

How to hide / show a datagrid column

Posted on 2011-03-12
3
678 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
Comment Utility
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
Comment Utility
i want to hide the column
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 250 total points
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now