Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asp.net c# gridview columns hide

Posted on 2012-03-28
5
Medium Priority
?
783 Views
Last Modified: 2012-04-10
Hello,
I have a *.aspx page where I allow users to search a database table via  stored procedure.
When the user clicks an asp.button called "find", it fires the stored procedure which populates a gridview control.  

I am attempting to hide two columns on this gridview and cannot seem to accomplish that.

My *.aspx code follows:
---------------
 <asp:SqlDataSource ID="ds_warrantsearchresults" runat="server" ConnectionString="<%$ ConnectionStrings:Jailinfo_JAUConnectionString1 %>"
            SelectCommand="sp_getAllWarrantsbyName" SelectCommandType="StoredProcedure">
            <SelectParameters>
                <asp:ControlParameter ControlID="FirstNameTextBox" Name="Firstname" PropertyName="Text"
                    Type="String" Size="50" ConvertEmptyStringToNull="false" />
                <asp:ControlParameter ControlID="LastNameTextBox" Name="Lastname" PropertyName="Text"
                    Type="String" ConvertEmptyStringToNull="false" />
                <asp:ControlParameter ControlID="SpnTextBox" Name="Spn" PropertyName="Text" Type="String"
                    ConvertEmptyStringToNull="false" />
                 <asp:ControlParameter ControlID="DobTextBox" Name="Dob" PropertyName="Text" Type="String"
                    ConvertEmptyStringToNull="false" />    
            </SelectParameters>
        </asp:SqlDataSource>
     
               
        <asp:GridView ID="gv_warrantsearchresults" runat="server" DataKeyNames="Spn" AllowSorting="True"
            DataSourceID="ds_warrantsearchresults" CellPadding="4" Font-Names="Arial" Font-Size="Small"
            ForeColor="#0033CC" GridLines="None" Width="580px" EmptyDataText="NO Matching Data Found"
            OnSelectedIndexChanged="gv_warrantsearchresults_SelectedIndexChanged" PageSize="15"
            ShowFooter="True" AllowPaging="true">
            <%-- <HeaderStyle CssClass="GridHeader" /> --%>
            <RowStyle BackColor="#EFF3FB" />
            <EmptyDataRowStyle Font-Names="Arial" Font-Size="Small" ForeColor="Red" />
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
            </Columns>
            <FooterStyle BackColor="White" Font-Bold="True" ForeColor="White" Font-Names="Arial"
                Font-Size="X-Small" />
            <PagerSettings Mode="NumericFirstLast" />
            <PagerStyle BackColor="#507CD1" ForeColor="#507CD1" HorizontalAlign="center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
            <EditRowStyle BackColor="#2461BF" />
            <AlternatingRowStyle BackColor="White" HorizontalAlign="Left" VerticalAlign="Top" />
        </asp:GridView>

-----------

The codebehind of the asp.button that fires the stored procedure follows:
-----------

protected void Button1_Click(object sender, EventArgs e)
        {
            pnl_gvWarrantSearchResults.Visible = true;
            //gv_warrantsearchresults.Visible = true;

            gv_warrantsearchresults.Columns[0].Visible = true;
            gv_warrantsearchresults.Columns[1].Visible = true;
            //gv_warrantsearchresults.Columns[2].Visible = true;
            //gv_warrantsearchresults.Columns[3].Visible = true;
            //gv_warrantsearchresults.Columns[4].Visible = false;
           
            pnl_gvSearchResults_Detailsview.Visible = true;
            dv_searchresults.Visible = true;

            pnl_wsdetail_imageview.Visible = true;

            //pnl_agreetoterms.Visible = true;
            //txt_AgreedToTerms.Text = "You Hereby Agree to Terms & Conditions";

            pnl_display_ddl.Visible = true;


        }

---------------

ANY HELP with this would be greatly appreciated.
0
Comment
Question by:Codeaddict7423
[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
5 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37777892
Here is sample code:-
foreach (GridViewRow gvr in GridView1.Rows)
	    {
//add aditional logic here
	        gvr.Cells[0].Visible = false;    
            }

Open in new window

see this as well:-
http://www.codeproject.com/Articles/43727/Show-Hide-GridView-Columns-in-ASP-NET
0
 
LVL 16

Expert Comment

by:MikeMCSD
ID: 37777937
I used this once and it worked :

  protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e)
  {

    if  ( condition)
    {
      e.Row.Cells[1].Visible = false;
      e.Row.Cells[2].Visible = false;
      e.Row.Cells[3].Visible = false;

   ..
0
 
LVL 13

Expert Comment

by:Rick
ID: 37778202
Use the gridview's rowdatabound event and change the  "visible" property to false:

      e.row.cells(0).visible = false;

Or change the display attribute to "none":

      e.row.cells(0).style.add("display", "none");
0
 
LVL 12

Assisted Solution

by:AshleyBryant
AshleyBryant earned 1000 total points
ID: 37778389
You can hide the columns with the code you have.  You just need to make sure they are hidden prior to the databinding.

gv_warrantsearchresults.Columns[0].Visible = false;
gv_warrantsearchresults.Columns[1].Visible = false;

Open in new window


You may want to create a private property on the page that tracks if the columns should be hidden or not and then look at that value on page_load to see if the columns should be hidden.
0
 
LVL 13

Accepted Solution

by:
Rahul Agarwal earned 1000 total points
ID: 37780345
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[index].Visible = false;

    }

  }
0

Featured Post

Technology Partners: 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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

636 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