Column count / Access GridView Column after DataBind

Discussion in 'ASP .Net' started by Finn Stampe Mikkelsen, Aug 19, 2010.

  1. Hi

    I have below GridView, which i in codebehind page databind with a dynamicly
    created datatable.

    This all works fine, but after the databind i still only have a column count
    of 1 although the datatable adds another 4 columns to the gridview. This
    represents a problem, since i would like to have the last column properties
    change on the fly and i cannot access the properties of any column besides
    the on definded below. None of the databound columns created by the DataBind
    method can be accessed..

    How can i access these columns??

    /Finn

    <asp:GridView ID="UdlaanGrd" runat="server" CellPadding="4"
    ForeColor="#333333"
    GridLines="None" onrowcommand="UdlaanGrd_RowCommand" >

    <Columns>
    <asp:ButtonField ButtonType="Button"
    HeaderText="Fjern Vare" Text="Fjern"
    CommandName="Fjern" >
    <HeaderStyle HorizontalAlign="Center" />
    <ItemStyle HorizontalAlign="Center" />
    </asp:ButtonField>
    </Columns>
    <AlternatingRowStyle BackColor="White" />

    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True"
    ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True"
    ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White"
    HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True"
    ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

    --
    Der er 10 slags mennesker - Dem som forstår binær og dem som ikke gør.
    There are 10 kinds of people. Those who understand binary and those who
    don't.
    Es gibt 10 Arten von Menschen. Die, die Binär verstehen, bzw. die, die es
    nicht tuhen.
    Finn Stampe Mikkelsen, Aug 19, 2010
    #1
    1. Advertising

  2. Finn Stampe Mikkelsen

    Rajeev Gopal Guest

    Finn,
    If you mention the columns in the GridView (not allowing AutoGenerate
    of Columns), then you can get access to the column count. As of now
    the grid is aware of only one column and that is of Button.

    Hope you got it.

    Thanks,
    Rajeev
    http://rajeevgopal.blogspot.com
    Rajeev Gopal, Aug 20, 2010
    #2
    1. Advertising

  3. Subject: Re: Column count / Access GridView Column after DataBind

    > Finn,
    > If you mention the columns in the GridView (not allowing AutoGenerate
    > of Columns), then you can get access to the column count. As of now
    > the grid is aware of only one column and that is of Button.
    >
    > Hope you got it.


    Hi

    Yeah, well then it is not dynamic and i need to autogenerate the individual
    columns..

    Funny enough, the individual rows have knowledge of the correct number of
    cells, including the one i want to make invisible.. I find it curious, that
    i can make individual cells invisible and access them by index, but i can't
    access the whole columns by index...

    /Finn
    >
    > Thanks,
    > Rajeev
    > http://rajeevgopal.blogspot.com
    Finn Stampe Mikkelsen, Aug 20, 2010
    #3
  4. Finn Stampe Mikkelsen

    Rajeev Gopal Guest

    Can you try this:

    sampleGridView.Rows(0).Cells.Count?

    You could place this in Prerender to get the count.
    Rajeev Gopal, Aug 20, 2010
    #4
  5. "Rajeev Gopal" <> skrev i meddelelsen
    news:...
    > Can you try this:
    >
    > sampleGridView.Rows(0).Cells.Count?
    >
    > You could place this in Prerender to get the count.


    I know the count of cells and it's correct and as expected in regard to the
    SqlDataSource.Select command... What i need is a way to dynamicly determin
    if i wan't to show a specific column of the data bound from the
    SqlDataSource.

    If for some reason i dont want to see the 3rd column, i want to do a
    ..visible = false; to the property of that column. I cannot do that, but i
    can do it the individuel rows 3rd cell (Cells[3]). This leaves the header
    row visible and indicates to the user that data is invisible.

    Whar i don't understand is, why the databound gridview, after the dataBind,
    cannot return a correct column.count but the individual rows of the same
    gridview can... I don't see the reason for this and think there must be some
    hack to do it...

    /Finn
    Finn Stampe Mikkelsen, Aug 20, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MC
    Replies:
    2
    Views:
    8,245
  2. mharness
    Replies:
    2
    Views:
    456
  3. Keith G Hicks
    Replies:
    3
    Views:
    752
    Keith G Hicks
    Feb 18, 2008
  4. Michael
    Replies:
    0
    Views:
    214
    Michael
    Dec 29, 2003
  5. herman
    Replies:
    0
    Views:
    163
    herman
    Mar 10, 2005
Loading...

Share This Page