gridview paging and hiding columns

I have dynamically bound data to my gridview and have enabled paging.

My usual method of hiding columns does not seem to work with paging

e.Row.Cells[1].Visible = false;

Therefore I would like to do what is discussed in the following link, but I am not so go with VB.

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_23681218.html

Is it possible for anybody to explain it to me in C#
Mr_ShawAsked:
Who is Participating?
 
tillgeffkenConnect With a Mentor Commented:
Ok, now i see where this is going. Set the AutoGenerateColumns property of the GridView to false and make sure you explicitly add all columns you need.
0
 
maXXXeEConnect With a Mentor Commented:
protected void  grvGesellschaften_RowDataBound(Object sender, GridViewRowEventArgs e)
{
HiddenField hdn=e.Row.FindControl("hdn");
lnkGesellschaft.NavigateUrl="default.aspx?id="+hdn.Value;
}
0
 
tillgeffkenCommented:
Is there a special reason why your not using

<Columns>
 <asp:TemplateField Visible="false">
 <ItemTemplate>Hidden Stuff</ItemTemplate>
 </</ItemTemplateField>
</Columns>

This works fine with paging.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
maXXXeECommented:
Explanation from the above EE link


Create a template column in the gridview and bind the id column
Then in rowdatabound get the value of the hidden field using the code

protected void  grvGesellschaften_RowDataBound(Object sender, GridViewRowEventArgs e)
{
HiddenField hdn=e.Row.FindControl("hdn");
lnkGesellschaft.NavigateUrl="default.aspx?id="+hdn.Value;
}
0
 
Mr_ShawAuthor Commented:
HI tillgeffken,
I am not using your method because I tried that and it did not work.

I am binding my data from dataset by using

Gridview1.DataSource = DataSet1();
Gridview1.DataBind();

Usin this method bind all the data,

I then tried to use

<asp:TemplateField Visible="False">
<ItemTemplate>
<asp:Label id="RowId" Text='<% #DataBinder.Eval(Container.DataItem, "RequestId") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>  

but it did not work.
0
 
tillgeffkenCommented:
Try <asp:Label id="RowId" Text='<%# Eval("RequestId")' %>
0
 
tillgeffkenCommented:
That came out wrong.

Try <asp:Label id="RowId" Text='<%# Eval("RequestId") %>' />
0
 
tillgeffkenCommented:
But thinking about it you're looking for a way to store a hidden value? Why not use a BoundField right away then?

<asp:BoundField DataField="RequestId" HeaderText="RequestId" ReadOnly="True" SortExpression="RequestId" Visible="False" />

0
 
Mr_ShawAuthor Commented:
Hi tillgeffken,

I just tried
<asp:Label id="RowId" Text='<%# Eval("RowId") %>' />
but I got an error. Lable is not a valid control inside a gridview.

I tried
<asp:BoundField DataField="RowID" HeaderText="RowID" Visible="false" />

But it just added an additional RowID column to what I already have through my inital databind
0
 
tillgeffkenCommented:
I'm afraid you're losing me. What's the name of the data field in your dataset that you're trying to make available in the gridview?
0
 
tillgeffkenCommented:
Hi tillgeffken,

I just tried
<asp:Label id="RowId" Text='<%# Eval("RowId") %>' />
but I got an error. Lable is not a valid control inside a gridview.

Of course you would have to put the label into a templatefield.
0
 
Mr_ShawAuthor Commented:
It is RowID

I gave you the wrong name to start with. Sorry.

Here is the deal.

I have created a dataset which is populated from a stored procedure.

I then take the dataset and bind it to gridview using the following code

Gridview1.DataSource = DataSet1();
Gridview1.DataBind();

My gridview allows paging and therefore my usal method of hiding columns does not work. My usual method is

e.Row.Cells[1].Visible = false;

I have tried using asp.net but that just adds and hides another RowID column leaving my original column still present.
0
 
tillgeffkenCommented:
I think i have to apologize to you. I don't know why but i was assuming you were developing a website but your original post clearly doesn't say website anywhere :)

My experience with windows gui applications is very limited so i'm not sure i can assist you in this matter.
0
 
Mr_ShawAuthor Commented:
i am developing a web site
0
 
tillgeffkenCommented:
Then the confusion is perfect now.

Post the relevant parts of your aspx and aspx.cs code please. (GridView and DataSet)
0
 
Mr_ShawAuthor Commented:
My dataset is created in a seperate method called select_referrals();

Referral_Gridview.DataSource = select_referrals.select_referrals();
Referral_Gridview.DataBind();

My asp code is

            <asp:GridView ID="Referral_Gridview" runat="server"
                onrowdatabound="Referral_Gridview_RowDataBound"
                onrowediting="Referral_Gridview_RowEditing" BackColor="White"
                BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
                ForeColor="Black" GridLines="Vertical" Width="424px" AllowPaging="true"
                onpageindexchanging="GridviewPaging">
                <Columns>
                    <asp:CommandField EditText="Review" ShowEditButton="True" />
                     <asp:BoundField DataField="RowID" HeaderText="RowID"/>
<asp:TemplateField Visible="false">
                                    <ItemTemplate>
                                          <asp:Label id="RowID" Text='<% #DataBinder.Eval(Container.DataItem, "RowID") %>' runat="server" />
                                    </ItemTemplate>
                              </asp:TemplateField>  

                     </Columns>
                <FooterStyle BackColor="#CCCCCC" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#CCCCCC" />
            </asp:GridView>
0
 
tillgeffkenCommented:
Ok, you have a visible boundfield with the RowId? What is it that you need in the hidden column and for what reason?
0
 
Mr_ShawAuthor Commented:
I only added the asp in becase my original method of hiding columns did not work with paging.

e.Row.Cells[1].Visible = false;

I need to hide the column for internal processing reasons.

0
 
tillgeffkenCommented:
I do not see why the below code should not generate a hidden field with the RowId as long as your dataset has a column with RowId. Does it break the paging where the paging works without the hidden field? Maybe your GridviewPaging() function is the culprit? Or maybe i just don't understand.

<asp:GridView ID="Referral_Gridview" runat="server"
                onrowdatabound="Referral_Gridview_RowDataBound"
                onrowediting="Referral_Gridview_RowEditing" BackColor="White"
                BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3"
                ForeColor="Black" GridLines="Vertical" Width="424px" AllowPaging="true"
                onpageindexchanging="GridviewPaging">
                <Columns>
                    <asp:BoundField DataField="RowId" HeaderText="RowId" ReadOnly="true" SortExpression="RowId" Visible="false" />
                    <asp:CommandField EditText="Review" ShowEditButton="True" />
                </Columns>
                <FooterStyle BackColor="#CCCCCC" />
                <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>

Open in new window

0
 
Mr_ShawAuthor Commented:
True that does generate a hidden column, but it does not remove the RowID column which created in the original databiind.

For example if I change the column in your example to visible=true i will have two rowid columns....
0
 
Mr_ShawAuthor Commented:
thanks.... I am sure you will be hearing more from me!!!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.