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