Remove columns on Gridview

D

David C

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
 
M

Mark Rae

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;
 
J

JDC

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...
 
D

David C

Mark,
Do I need to do this in one of the GridView events, and if so which one?
Thanks.

David
Mark Rae said:
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;
 
M

Mark Rae

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;
 
D

David C

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
Mark Rae said:
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;
 
M

Mark Rae

That now works, but I'll have to set the width of my other columns because
some are way too wide.

Easy enough...
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.

Have you tried stepping through the code...? Could you possibly be
referencing the columns of the grid somewhere else...?
 
D

David C

I fixed it but I'm not exactly sure why. When I traced the code, the system
was resetting the MultiView1.ActiveViewIndex to 0 if it was -1. Doesn't the
ActiveViewIndex hold it's value in postbacks? I put code in the click event
of the Edit button and now it seems to work ok, even though there is nothing
in the Cancel link button. The GridView is displayed only when the
ActiveViewIndex = 0.

David
 
M

Mark Rae

I fixed it but I'm not exactly sure why. When I traced the code, the
system was resetting the MultiView1.ActiveViewIndex to 0 if it was -1.
Doesn't the ActiveViewIndex hold it's value in postbacks? I put code in
the click event of the Edit button and now it seems to work ok, even though
there is nothing in the Cancel link button. The GridView is displayed only
when the ActiveViewIndex = 0.

Curious... I confess I'm not really familiar with <asp:MultiView>, as I have
a client-side JavaScript solution which I use for tabbing...
 
D

David C

So how do you know which view to display when you come back to the page.
I'm all open to alternatives that are easier. I currently have 8 different
views with a variety of 1-5 different formviews, gridviews, etc. Would you
share your concept? Thanks.

David
 
M

Mark Rae

So how do you know which view to display when you come back to the page.

I use a hidden field, though more often than not these days that is rendered
unnecessary by AJAZ.
Would you share your concept?

Sure - contact me privately.
 
V

vinay chowdhary

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top