Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2007

Tip: Looking for answers? Try searching our database.

Remove columns on Gridview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David C - 02 Apr 2007 14:27 GMT
I have a Gridview that has 10 columns.  I have a condition where I need to
hide 5 of them but I'm not sure how to hide a column.  I can hide a cell ok
in the row databound event but not sur how to hide whole columns (I assume
before databound).  Thanks.

David
Mark Rae - 02 Apr 2007 14:31 GMT
>I have a Gridview that has 10 columns.  I have a condition where I need to
>hide 5 of them but I'm not sure how to hide a column.  I can hide a cell ok
>in the row databound event but not sur how to hide whole columns (I assume
>before databound).  Thanks.

MyGridView.Columns[n].Visible = false;
David C - 02 Apr 2007 15:00 GMT
Mark,
Do I need to do this in one of the GridView events, and if so which one?
Thanks.

David

>>I have a Gridview that has 10 columns.  I have a condition where I need to
>>hide 5 of them but I'm not sure how to hide a column.  I can hide a cell
>>ok in the row databound event but not sur how to hide whole columns (I
>>assume before databound).  Thanks.
>
> MyGridView.Columns[n].Visible = false;
Mark Rae - 02 Apr 2007 15:08 GMT
> Do I need to do this in one of the GridView events, and if so which one?

I usually do this immediately after the DataBind() call e.g.

MyGridView.DataSource = <DataSource>;
MyGridView.DataBind();
MyGridView.Columns[1].Visible = false;
MyGridView.Columns[3].Visible = false;
MyGridView.Columns[n].Visible = false;
David C - 02 Apr 2007 15:32 GMT
That now works, but I'll have to set the width of my other columns because
some are way too wide.

Also, when I click the Edit link button on the line, the page does a
postback, but does not go into edit mode.  If I click it a 2nd time, it
does.  The same happens if I click the Cancel or Update link button.  Any
ideas why this is happening?  Thanks.

David

>> Do I need to do this in one of the GridView events, and if so which one?
>
[quoted text clipped - 5 lines]
> MyGridView.Columns[3].Visible = false;
> MyGridView.Columns[n].Visible = false;
JDC - 02 Apr 2007 14:45 GMT
Depends. If you want access to the data in the column, it's best to
hide it using CSS. Setting the "Visible" property causes the cells not
to be rendered at all.

In the code-behind:

protected void GridView1_RowCreated(object sender,
GridViewRowEventArgs e)
{
   if (e.Row.RowType != DataControlRowType.Pager)
   {
       // hide unwanted columns
       e.Row.Cells[0].CssClass = "hidden";
       e.Row.Cells[1].CssClass = "hidden";
   }
}

And the CSS class is simply:

.hidden
{
 display: none;
}

HTH...

> I have a Gridview that has 10 columns.  I have a condition where I need to
> hide 5 of them but I'm not sure how to hide a column.  I can hide a cell ok
> in the row databound event but not sur how to hide whole columns (I assume
> before databound).  Thanks.
>
> David
vinay chowdhary - 16 Oct 2007 09:27 GMT
As i was also facing same problem, and getting many where the same
solution to create hidden csss and then apply it dynamically. but i got
the solution we can do it without creating CSS also, we can do it like
this but the onlything we have to keep in mind that  we have to check
whether row.rowtype is DataControlRowType.Pager or not other wise it
will through an error like out of range....
Try it! its working fine my side...

If e.Row.RowType <> DataControlRowType.Pager Then
               e.Row.Cells(0).visible = false
               e.Row.Cells(1).CssClass = false            End If

thnkss....keep posting

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.