Hide a few (variable) columns in a GridView

Discussion in 'ASP .Net' started by =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=, Feb 13, 2007.

  1. Hi,

    I have a GridView with a fixed number of columns being returned from a SQL
    query and each having a simple template:

    <asp:TemplateField HeaderText="Allocations">
    <edititemtemplate>
    <asp:TextBox id="txt89" runat="server" Text='<%# Bind("89") %>' asp:TextBox>
    </edititemtemplate>
    </asp:TemplateField>

    Where 'Allocations' is the display name and '89' is the field name and these
    are the only differences from template to template.

    I need to hide some of the columns (a different set of columns depending on
    the query parameters) when the values for the columns are null.

    To make this easier I have a query I've fed into a dataset that gives the
    list of field IDs to show. The problem I have is to turn the name of the
    field (the 89) from the query into the index of the column which is required
    to do the hiding/showing. I can't use the HeaderText property of the
    templatefield as it is different to the fieldID. Any ideas?

    TIA,

    Rob
     
    =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=, Feb 13, 2007
    #1
    1. Advertising

  2. Hi Rob,

    it would be easy if you used BoundField. Unfotunatelly, there's no direct
    relationship between underlying data source (it could be something else than
    data table i.e. a businnes object) and gridview columns due to simple fact
    you may use many data source fields in template as you want. The only way to
    resolve the problem would be to find cell index that includes binding
    expression containing "89" (Eval("89")) - that's literally what GridView does
    internally - but trust me, you don't want to do it :)(if you have
    Disassembler or Reflector see it for youself : GridView.ExtractRowValues(),
    then TemplateField.ExtractValuesFromCell(), and finally all methods in
    BindableTemplateBuilder class).


    --
    Milosz


    "Robert Chapman" wrote:

    > Hi,
    >
    > I have a GridView with a fixed number of columns being returned from a SQL
    > query and each having a simple template:
    >
    > <asp:TemplateField HeaderText="Allocations">
    > <edititemtemplate>
    > <asp:TextBox id="txt89" runat="server" Text='<%# Bind("89") %>' asp:TextBox>
    > </edititemtemplate>
    > </asp:TemplateField>
    >
    > Where 'Allocations' is the display name and '89' is the field name and these
    > are the only differences from template to template.
    >
    > I need to hide some of the columns (a different set of columns depending on
    > the query parameters) when the values for the columns are null.
    >
    > To make this easier I have a query I've fed into a dataset that gives the
    > list of field IDs to show. The problem I have is to turn the name of the
    > field (the 89) from the query into the index of the column which is required
    > to do the hiding/showing. I can't use the HeaderText property of the
    > templatefield as it is different to the fieldID. Any ideas?
    >
    > TIA,
    >
    > Rob
    >
     
    =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=, Feb 14, 2007
    #2
    1. Advertising

  3. Hi Milosz,

    The solution I've gone with is to maintain a database table with the matched
    field name and the column number. This is then loaded into a hashtable and
    used as a lookup table. This works because while the columns to hide are
    different every time it is still the same set of 280 potential columns in
    total.

    Regards,

    Rob

    "Milosz Skalecki [MCAD]" wrote:

    > Hi Rob,
    >
    > it would be easy if you used BoundField. Unfotunatelly, there's no direct
    > relationship between underlying data source (it could be something else than
    > data table i.e. a businnes object) and gridview columns due to simple fact
    > you may use many data source fields in template as you want. The only way to
    > resolve the problem would be to find cell index that includes binding
    > expression containing "89" (Eval("89")) - that's literally what GridView does
    > internally - but trust me, you don't want to do it :)(if you have
    > Disassembler or Reflector see it for youself : GridView.ExtractRowValues(),
    > then TemplateField.ExtractValuesFromCell(), and finally all methods in
    > BindableTemplateBuilder class).
    >
    >
    > --
    > Milosz
    >
    >
    > "Robert Chapman" wrote:
    >
    > > Hi,
    > >
    > > I have a GridView with a fixed number of columns being returned from a SQL
    > > query and each having a simple template:
    > >
    > > <asp:TemplateField HeaderText="Allocations">
    > > <edititemtemplate>
    > > <asp:TextBox id="txt89" runat="server" Text='<%# Bind("89") %>' asp:TextBox>
    > > </edititemtemplate>
    > > </asp:TemplateField>
    > >
    > > Where 'Allocations' is the display name and '89' is the field name and these
    > > are the only differences from template to template.
    > >
    > > I need to hide some of the columns (a different set of columns depending on
    > > the query parameters) when the values for the columns are null.
    > >
    > > To make this easier I have a query I've fed into a dataset that gives the
    > > list of field IDs to show. The problem I have is to turn the name of the
    > > field (the 89) from the query into the index of the column which is required
    > > to do the hiding/showing. I can't use the HeaderText property of the
    > > templatefield as it is different to the fieldID. Any ideas?
    > >
    > > TIA,
    > >
    > > Rob
    > >
     
    =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?=, Feb 15, 2007
    #3
  4. On Feb 15, 9:33 pm, Robert Chapman
    <> wrote:
    > Hi Milosz,
    >
    > The solution I've gone with is to maintain a database table with the matched
    > field name and the column number. This is then loaded into a hashtable and
    > used as a lookup table. This works because while the columns to hide are
    > different every time it is still the same set of 280 potential columns in
    > total.
    >
    > Regards,
    >
    > Rob
    >
    >
    >
    > "Milosz Skalecki [MCAD]" wrote:
    > > Hi Rob,

    >
    > > it would be easy if you used BoundField. Unfotunatelly, there's no direct
    > > relationship between underlying data source (it could be something else than
    > > data table i.e. a businnes object) and gridview columns due to simple fact
    > > you may use many data source fields in template as you want. The only way to
    > > resolve the problem would be to find cell index that includes binding
    > > expression containing "89" (Eval("89")) - that's literally what GridView does
    > > internally - but trust me, you don't want to do it :)(if you have
    > > Disassembler or Reflector see it for youself : GridView.ExtractRowValues(),
    > > then TemplateField.ExtractValuesFromCell(), and finally all methods in
    > > BindableTemplateBuilder class).

    >
    > > --
    > > Milosz

    >
    > > "Robert Chapman" wrote:

    >
    > > > Hi,

    >
    > > > I have a GridView with a fixed number of columns being returned from a SQL
    > > > query and each having a simple template:

    >
    > > > <asp:TemplateField HeaderText="Allocations">
    > > > <edititemtemplate>
    > > > <asp:TextBox id="txt89" runat="server" Text='<%# Bind("89") %>' asp:TextBox>
    > > > </edititemtemplate>
    > > > </asp:TemplateField>

    >
    > > > Where 'Allocations' is the display name and '89' is the field name and these
    > > > are the only differences from template to template.

    >
    > > > I need to hide some of the columns (a different set of columns depending on
    > > > the query parameters) when the values for the columns are null.

    >
    > > > To make this easier I have a query I've fed into a dataset that gives the
    > > > list of field IDs to show. The problem I have is to turn the name of the
    > > > field (the 89) from the query into the index of the column which is required
    > > > to do the hiding/showing. I can't use the HeaderText property of the
    > > > templatefield as it is different to the fieldID. Any ideas?

    >
    > > > TIA,

    >
    > > > Rob- Hide quoted text -

    >
    > - Show quoted text -


    What about this?

    protected void GridView1_RowDataBound(object sender,
    GridViewRowEventArgs e)
    {
    e.Row.Cells[1].Visible = false; // 1 - is your column
    }

    .....

    <asp:GridView ID="GridView1" runat="server"
    OnItemDataBound="GridView1_RowDataBound"
     
    Alexey Smirnov, Feb 15, 2007
    #4
    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. Jason
    Replies:
    3
    Views:
    17,068
    Jan Hyde
    May 8, 2006
  2. =?Utf-8?B?Q1NoYXJwZ3V5?=

    hide columns in export of gridview

    =?Utf-8?B?Q1NoYXJwZ3V5?=, Sep 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    486
    =?Utf-8?B?Q1NoYXJwZ3V5?=
    Sep 7, 2006
  3. Phil Sandler

    Hide Columns in GridView?

    Phil Sandler, Dec 21, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    895
    Eliyahu Goldin
    Dec 22, 2006
  4. Murali
    Replies:
    2
    Views:
    575
    Jerry Coffin
    Mar 9, 2006
  5. David C

    Hide GridView Columns

    David C, Feb 29, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    561
    David C
    Feb 29, 2008
Loading...

Share This Page