?
Solved

How to hide / show a datagrid column

Posted on 2011-03-12
3
Medium Priority
?
730 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 1000 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

764 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