By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,683 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,683 IT Pros & Developers. It's quick & easy.

hiding columns in Gridview with paging enabled

P: 7
Hi,

I am programmatically 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.Columns[1].Visible = false;
//gvAllReports.Columns[6].Visible = false;

Please guide me wat should I do
Dec 5 '09 #1

✓ answered by sanjib65

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:

Expand|Select|Wrap|Line Numbers
  1. <asp:BoundField DataField="LastName" HeaderText="LastName" 
  2.                                 SortExpression="LastName" Visible="false" />
  3.  
Likewise you can make Enable Paging true:

Expand|Select|Wrap|Line Numbers
  1.   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  2.                         DataKeyNames="EID" DataSourceID="SqlDataSource1" Width="293px" AllowPaging="true">

Share this Question
Share on Google+
9 Replies


sanjib65
100+
P: 102
Actually hiding columns can be done in Edit Template section. Not programmatically, the .NET Framework has assisted to do this.
You can select the textBox and in Properties make the Visble property false.
Dec 5 '09 #2

Frinavale
Expert Mod 5K+
P: 9,731
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
Dec 5 '09 #3

P: 7
Thnaks Frinny for help.
Here is the code.
Expand|Select|Wrap|Line Numbers
  1.   gvAllReports.DataSource = dt; //dt is DataTable
  2.         gvAllReports.DataBind();
  3.         gvAllReports.Columns[1].Visible = false;
  4.         gvAllReports.Columns[6].Visible = false;
  5.  
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
Dec 7 '09 #4

sanjib65
100+
P: 102
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:

Expand|Select|Wrap|Line Numbers
  1. <asp:BoundField DataField="LastName" HeaderText="LastName" 
  2.                                 SortExpression="LastName" Visible="false" />
  3.  
Likewise you can make Enable Paging true:

Expand|Select|Wrap|Line Numbers
  1.   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
  2.                         DataKeyNames="EID" DataSourceID="SqlDataSource1" Width="293px" AllowPaging="true">
Dec 7 '09 #5

P: 7
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
Dec 7 '09 #6

ssnaik84
100+
P: 149
whats the column count of your datatable?
Dec 7 '09 #7

P: 7
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.DataBind().
I don't understand why the column count is showing 0.
Do I have to add that code to some other event?
Dec 7 '09 #8

P: 7
Hi all,
I got the solution.
I dynamically added bound fields.

Expand|Select|Wrap|Line Numbers
  1. foreach (DataColumn dc in dt.Columns) //dt is source datatable
  2.         {
  3.             BoundField bf = new BoundField();
  4.             bf.DataField = dc.ColumnName;
  5.             bf.HeaderText = dc.ColumnName;
  6.             gvAllReports.Columns.Add(bf); //gvAllReports is GridView
  7.         }
  8.  
  9.         gvAllReports.DataSource = dt;
  10.         gvAllReports.DataBind();
  11.                gvAllReports.Columns[1].Visible = false;
  12.         gvAllReports.Columns[6].Visible = false;
  13.  
Now its working properly. :) Thanks for your help

-Ally
Dec 7 '09 #9

P: 1
Try this
just calculate the count
it works

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.Cells.Count>2)
{
e.Row.Cells[2].Visible = false;
}
}
Mar 22 '18 #10

Post your reply

Sign in to post your reply or Sign up for a free account.