views:

404

answers:

5

GridView1.Columns.Count is always zero even SqlDataSource1.DataBind();

But Grid is ok

I can do

for (int i = 0; i < GridView1.HeaderRow.Cells.Count;i++)

I rename request headers here but

GridView1.Columns[i].Visible = false;

I can't use it because of GridView1.Columns.Count is 0.

So how can I hide them ?

+2  A: 

You have to perform the GridView1.Columns[i].Visible = false; after the grid has been databound.

ddc0660
There is no `GridView.OnDataBound` event.
Jan Jongboom
When ... ?it stay being Zero ever if I add button and make it there .
nCdy
The `GridView` does in fact has a `DataBound` event and that's where this code should go.
Eilon
protected void GridView1_DataBound(object sender, EventArgs e) { WebMsgBox.Show(GridView1.Columns.Count.ToString()); }That is 0 ...
nCdy
Ok. I was mistaken here. What I should've said: *You don't have access to the generated columns in your `DataBound` event.*
Jan Jongboom
+1  A: 

Try putting the e.Row.Cells[0].Visible = false; inside the RowCreated event of your grid.

protected void bla_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false; // hides the first column
}

This way it auto-hides the whole column.

You don't have access to the generated columns through grid.Columns[i] in your gridview's DataBound event.

Jan Jongboom
That doesn't sound like a good idea. RowCreated will be called several times each time the GridView renders.
Eilon
It's not that easy. There is no `DataBound` event on a `GridView`, therefore you'll have to make a hook earlier in the binding process, like at `RowCreated` or `RowDataBound`.
Jan Jongboom
there is still no counts :)
nCdy
Actually, GridView very much has a `DataBound` event.
Eilon
and what must I make there ...how can I cancel adding some column ?
nCdy
Check my edit .
Jan Jongboom
Yes :)It works , thank you.
nCdy
A: 

Note: This solution only works if your GridView columns are known ahead of time.

It sounds like you're using a GridView with AutoGenerateColumns=true, which is the default. I recommend setting AutoGenerateColumns=false and adding the columns manually:

<asp:GridView runat="server" ID="MyGridView"
    AutoGenerateColumns="false" DataSourceID="MySqlDataSource">
    <Columns>
        <asp:BoundField DataField="Column1" />
        <asp:BoundField DataField="Column2" />
        <asp:BoundField DataField="Column3" />
    </Columns>
</asp:GridView>

And only include a BoundField for each field that you want to be displayed. This will give you the most flexibility in terms of how the data gets displayed.

Eilon
thank you but my datasource provides different columns each time.and thank you for fast answers and correcting my question !
nCdy
and yes AutoGenerateColumns=trueso is there any ways to use dynamic columns with false ?
nCdy
+1  A: 

Hi there,

I was having the same problem - need my GridView control's AutogenerateColumns to be 'true', due to it being bound by a SQL datasource, and thus I needed to hide some columns which must not be displayed in the GridView control.

The way to accomplish this is to add some code to your GridView's '_RowDataBound' event, such as this (let's assume your GridView's ID is = 'MyGridView'):

protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Cells[<index_of_cell>].Visible = false;
    }
}

That'll do the trick just fine ;-)

Werner Moecke
hm... thank you , looking like that's better.
nCdy
but what is <index_of_cell> ???
nCdy
4​​​​​​​​​​​​​2
Jørn Schou-Rode
+1  A: 

@nCdy: index_of_cell should be replaced by an integer, corresponding to the index number of the cell that you wish to hide in the .Cells collection.

For example, suppose that your GridView presents the following columns:

CONTACT NAME | CONTACT NUMBER | CUSTOMERID | ADDRESS LINE 1 | POST CODE

And you want the CUSTOMERID column not to be displayed. Since collections indexes are 0-based, your CUSTOMERID column's index is..........? That's right, 2!! Very good. Now... guess what you should put in there, to replace 'index_of_cell'??

Werner Moecke
And I'm like urr hurr, and um and now I'm just like mascera and I'm good to go ! And um yeah and so I love you guys, a lot. I really like, rawrawrawr...
nCdy