473,386 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Hide a few (variable) columns in a GridView

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

Feb 13 '07 #1
3 7481
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
Feb 14 '07 #2
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
Feb 15 '07 #3
On Feb 15, 9:33 pm, Robert Chapman
<RobertChap...@discussions.microsoft.comwrote:
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"

Feb 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason | last post by:
How do I hide a column in a GridView in ASP.NET 2.0 when all of the columns are autogenerated based on the datasource? I want to hide the first of three columns, but the following doesn't work: ...
9
by: ghostwolf | last post by:
Hi, I want to hide a column in the asp:GridView, say one of the column of asp:BoundField. But it is not allowed to put <divinside for setting it display:none. What can I do? Thanks in millions.
0
by: CSharpguy | last post by:
Ok, i got my gridview (sortable and non sorting) to export to excel. BUt now 3 of my gridviews I have a as:TemplateField which are links. So how can I hide those 3 columns when I export the...
3
by: kevinpublic | last post by:
I have an item list for ordered products on a data grid in VS 2003. It's an ASP page running VB behind it. All detail lines display as well as all shipping charges. On the edit screen, we allow...
2
by: Phil Sandler | last post by:
Quick question: Is it possible to use autogeneratecolumns = true and still hide specific columns based on the column name in the datasource? Failing that, can it be done by column index? ...
0
by: Riaaaa | last post by:
I have created the form for entering the company details with its general information in asp.net C# 2005. I have two buttons that form on the submit click event it should saw the newly / last...
0
by: roamnet | last post by:
hi i created database file with .mdf extention ,sql server as a source and use grid view to display data there're no problem in data retrieve and display,but i want to edit it or insert new...
4
by: Luqman | last post by:
I have populated the Child Accounts and Parent Accounts in a Grid View Control, I want to hide the Select Column of Parent Accounts, but not the Child Accounts, is it possible ? I am using VS...
3
by: mudassir368 | last post by:
--This is the html code <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.