GridView - can't reference value of a non-visible cell??

R

Rob R. Ainscough

I'm trying to retrieve a value I placed in a grid view column that is set to
NOT visible. Now when I try to retrieve the value of the non visible column
using:

gv_Units is the GridView control

Dim gvRow As GridViewRow
For Each gvRow In gv_Units.Rows

Dim someValue as string = gvRow.Cells(6).Text

Next gvRow

If column 6 is not visible SomeValue = ""
If column 6 is visible SomeValue = "myvalue"

Is this a known bug with GridView? Please tell me this is NOT a "design
feature". Any workarounds?

Thanks, Rob.
 
P

Phillip Williams

Hi Rob,

Setting the visible property of a server control to false means that the
control does not render any HTML objects on the browser, therefore the empty
string value you are receiving in the code behind. If you wanted it to still
render but not be visible use the css attribute display =none instead, e.g.
<style>
..hidde {display:none;}
</style>
 
R

Rob R. Ainscough

Hi Phillip,

Not sure I understand in source view <style> </style> is not permitted
within the BoundField block. Did you mean setup a CSS Style with
Display:none and then set the gridview columns
ItemStyle/ControlSytle/FooterStyle/HeaderStyle to the CSS Style I create for
display none?

I also tried setting the DataKeyNames and adding a Primary Key to bound
table, still no go -- Visible is the only way I can get a value. The only
twist might be that my bound table is a Table I've created and populated via
code, it is not directly connected to a DataSource.

Rob
 
P

Phillip Williams

Hi Rob,

Rob R. Ainscough said:
Hi Phillip,

Not sure I understand in source view <style> </style> is not permitted
within the BoundField block. Did you mean setup a CSS Style with
Display:none and then set the gridview columns
ItemStyle/ControlSytle/FooterStyle/HeaderStyle to the CSS Style I create for
display none?

Yes.
 
R

Rob R. Ainscough

Phillip,

Ok, I got it -- thank you for pointing me in a direction I would never have
thought about going in.

My solution was:
1. Add a new CSS style to my .css called "NonVisibleText"

..NonVisibleText
{color: Black;
font-family: Verdana, Arial, Tahoma, Sans-Serif;
display:none;
}

2. From GridView set the column's ControlStyle.CssClass = NonVisibleText,
ItemStyle.CssClass = NonVisibleText, HeaderStyle.CssClass = NonVisibleText,
and FooterStyle.CssClass = NonVisibleText. Keep the Column Visible property
= True

My column is not visible but I can still reference the cells values.

FYI, setting the DataKeyNames will not work (even with a primarykey
defined).

Rob
 
C

Christopher Reed

As a further FYI, yes, this is a design feature. Ask the question: If it's
not visible, then why is there? In general, for DataGrids, one uses
nonvisible columns for data keys, though there can be other needs. For
GridViews, especially if you use the master-detail paradigm, the nonvisible
columns is really not needed since your DetailsView will contain all of your
fields in it. The only fields that you need to connect the GridView to the
DetailsView without having it as a visible column would be your primary key
fields.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top