Hide a few (variable) columns in a GridView

G

Guest

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
 
G

Guest

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).
 
G

Guest

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
 
G

Guest

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






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

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top