hiding columns in Gridview with paging enabled

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ally
    New Member
    • Dec 2009
    • 7

    hiding columns in Gridview with paging enabled

    Hi,

    I am programmaticall y binding gridview to a datatable. I want to hide 2 columns. I can be done by adding code in RowCreated event of gridview like

    e.Row.Cells[0].Visible = false;
    e.Row.Cells[5].Visible = false;

    but after that paging is not working.
    I tried another way by using gridview's column.Visible property
    but its giving error
    Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index

    gvAllReports.Co lumns[1].Visible = false;
    //gvAllReports.Co lumns[6].Visible = false;

    Please guide me wat should I do
  • sanjib65
    New Member
    • Nov 2009
    • 102

    #2
    Hiding columns

    Actually hiding columns can be done in Edit Template section. Not programmaticall y, the .NET Framework has assisted to do this.
    You can select the textBox and in Properties make the Visble property false.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Setting the visibility columns or cells in a GridView does not have any effect on the paging.

      Please post the code that handles the GridView's paging so that we can see what's going wrong.


      -Frinny

      Comment

      • ally
        New Member
        • Dec 2009
        • 7

        #4
        Thnaks Frinny for help.
        Here is the code.
        Code:
          gvAllReports.DataSource = dt; //dt is DataTable
                gvAllReports.DataBind();
                gvAllReports.Columns[1].Visible = false;
                gvAllReports.Columns[6].Visible = false;
        it gives error "Index was out of range. Must be non-negative and less than the size of the collection.
        Parameter name: index" even if I checked and rechecked the indexes of the columns.

        -Ally

        Comment

        • sanjib65
          New Member
          • Nov 2009
          • 102

          #5
          Hiding Columns

          If you want to do that in code behind page it gives error!
          I'm requesting Frinny to explain as it's beyond my knowledge :)
          But it works if you go to the source view and change the boundfield properties. Like this:

          Code:
          <asp:BoundField DataField="LastName" HeaderText="LastName" 
                                          SortExpression="LastName" Visible="false" />
          Likewise you can make Enable Paging true:

          Code:
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
                                  DataKeyNames="EID" DataSourceID="SqlDataSource1" Width="293px" AllowPaging="true">

          Comment

          • ally
            New Member
            • Dec 2009
            • 7

            #6
            Hey Sanji,

            My datatable is getting bound at runtime. At design time there is no binding. Thats why I cannot add boundfield in source view.
            this is my problem.

            -Ally

            Comment

            • ssnaik84
              New Member
              • Aug 2009
              • 149

              #7
              whats the column count of your datatable?

              Comment

              • ally
                New Member
                • Dec 2009
                • 7

                #8
                Column count is 6. But I think thats not the problem because even I change column index to 1 or 0. Its giving me same error.
                One more thing I tried to do is debug and checked the line to check column count, its showing 0, even after the gridview.DataBi nd().
                I don't understand why the column count is showing 0.
                Do I have to add that code to some other event?

                Comment

                • ally
                  New Member
                  • Dec 2009
                  • 7

                  #9
                  Hi all,
                  I got the solution.
                  I dynamically added bound fields.

                  Code:
                  foreach (DataColumn dc in dt.Columns) //dt is source datatable
                          {
                              BoundField bf = new BoundField();
                              bf.DataField = dc.ColumnName;
                              bf.HeaderText = dc.ColumnName;
                              gvAllReports.Columns.Add(bf); //gvAllReports is GridView
                          }
                      
                          gvAllReports.DataSource = dt;
                          gvAllReports.DataBind();
                                 gvAllReports.Columns[1].Visible = false;
                          gvAllReports.Columns[6].Visible = false;
                  Now its working properly. :) Thanks for your help

                  -Ally

                  Comment

                  • rohit99452
                    New Member
                    • Mar 2018
                    • 1

                    #10
                    Try this
                    just calculate the count
                    it works

                    protected void GridView1_RowDa taBound(object sender, GridViewRowEven tArgs e)
                    {
                    if(e.Row.Cells. Count>2)
                    {
                    e.Row.Cells[2].Visible = false;
                    }
                    }

                    Comment

                    Working...