473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

hiding columns in Gridview with paging enabled

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">

9 12014
sanjib65
102 100+
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
9,735 Expert Mod 8TB
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
ally
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
102 100+
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
ally
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
149 100+
whats the column count of your datatable?
Dec 7 '09 #7
ally
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
ally
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
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

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

Similar topics

0
by: ck388 | last post by:
For some reason when I enable the callback feature of the gridview I still get a page refresh, that is it seems like there is a postback that occurs, not a callback which is just supposed to update...
0
by: Samy | last post by:
Hi There, I have a grid view which has paging enabled in it. One of the colums is a check box and checkbox selections shows a subset of rows a user has selected from available rows. I need to...
1
by: davidjgonzalez | last post by:
I have a GridView that has paging enabled. Each item (as defined in an ItemTemplate) includes several controls which have operations i would like to Atlas-enable. Everything is working well except...
8
by: AG | last post by:
ASP.NET 2.0, VS 2005 I have a gridview with paging enabled, bound to an objectdatasource. Both were dropped on to the page and configured via the wizards. Basically working as expected. The...
1
by: DMC | last post by:
Hi, I have enabled paging in my gridview control and am now trying to work out how to programmatically access it. With my gridview i have an "Add" button which adds a new row to the gridview and...
9
by: =?Utf-8?B?c3Jpbml2YXM=?= | last post by:
Hi All, Can anybody please help me, how to access all rows in a grid view if the paging is enabled? If gridview contains 100 rows and pagecount=10, then in the gridview.rows collection, i'm...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
2
by: =?Utf-8?B?SmF5IFBvbmR5?= | last post by:
Based on wether a row is selected in a GridView I need to HIDE the last two columns of a gridview. I do NOT need to make the cells invisible I want to hide the entire column. When I set the...
2
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hello, I have an asp.net 2.0 gridview with the paging enabled. I don't want to display the footer, but it always displays with the page numbers displayed. I have set the grid's showfooter to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.