Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

gridview paging and hiding columns

Posted on 2009-04-30
21
Medium Priority
?
639 Views
Last Modified: 2013-12-17
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#
0
Comment
Question by:Mr_Shaw
  • 11
  • 8
  • 2
21 Comments
 
LVL 7

Assisted Solution

by:maXXXeE
maXXXeE earned 80 total points
ID: 24268795
protected void  grvGesellschaften_RowDataBound(Object sender, GridViewRowEventArgs e)
{
HiddenField hdn=e.Row.FindControl("hdn");
lnkGesellschaft.NavigateUrl="default.aspx?id="+hdn.Value;
}
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24268811
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
 
LVL 7

Expert Comment

by:maXXXeE
ID: 24268812
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:Mr_Shaw
ID: 24268864
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
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24268907
Try <asp:Label id="RowId" Text='<%# Eval("RequestId")' %>
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24268916
That came out wrong.

Try <asp:Label id="RowId" Text='<%# Eval("RequestId") %>' />
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24268932
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
 

Author Comment

by:Mr_Shaw
ID: 24268942
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
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24268979
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
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24268997
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
 

Author Comment

by:Mr_Shaw
ID: 24269020
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
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24269060
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
 

Author Comment

by:Mr_Shaw
ID: 24269292
i am developing a web site
0
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24269315
Then the confusion is perfect now.

Post the relevant parts of your aspx and aspx.cs code please. (GridView and DataSet)
0
 

Author Comment

by:Mr_Shaw
ID: 24269381
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
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24269459
Ok, you have a visible boundfield with the RowId? What is it that you need in the hidden column and for what reason?
0
 

Author Comment

by:Mr_Shaw
ID: 24269494
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
 
LVL 9

Expert Comment

by:tillgeffken
ID: 24269606
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
 

Author Comment

by:Mr_Shaw
ID: 24269674
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
 
LVL 9

Accepted Solution

by:
tillgeffken earned 1920 total points
ID: 24269774
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
 

Author Closing Comment

by:Mr_Shaw
ID: 31576371
thanks.... I am sure you will be hearing more from me!!!!!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 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