Accessing data grid column

J

John

Hi

I have a hidden (visible=false) column on a gridview bound to a sql server
table. I am trying to access the field as;

For Each row As GridViewRow In grdEvents.Rows
If row.RowType = DataControlRowType.DataRow Then
bf = CType(row.Cells(row.Cells.Count - 1).FindControl("event id"),
BoundField)
End if
Next

but I get the 'System.Web.UI.Control' cannot be converted to
System.Web.UI.WebControls.BoundField' error. When I try to access it using
row.Cells(12).Text then it returns a blank value.

How can I access the relevant gridview column?

Thanks

Regards
 
M

Mark Rae

I have a hidden (visible=false) column on a gridview bound to a sql server
table. I am trying to access the field as;

For Each row As GridViewRow In grdEvents.Rows
If row.RowType = DataControlRowType.DataRow Then
bf = CType(row.Cells(row.Cells.Count - 1).FindControl("event id"),
BoundField)
End if
Next

but I get the 'System.Web.UI.Control' cannot be converted to
System.Web.UI.WebControls.BoundField' error.

Hmm - not precisely certain what you're trying to do here... Clearly, you
don't actually need to convert a column into a BoundField or anything else
just to interrogate its contents...
When I try to access it using row.Cells(12).Text then it returns a blank
value.

Sounds like you've discovered one of the new features of GridViews, whereby
hidden columns are not saved in ViewState by default...
How can I access the relevant gridview column?

Set the column Visible = True immediately before calling the GridView's
DataBind method, then set it Visible = False immediately afterwards...
 
J

John

Hi

I have used the 'Configure Data Source' to bind the gridview. Where would I
find the databind method in this case?

Thanks

Regards
 
K

KJ

Hello John,

I am guessing you used an ObjectDataSource control to act as a
datasource for the gridview? Check the following:

The DataSourceID property of the gridview is set to that of the
objectdatasource.
WHen you call DataBind() on the gridview, the SelectMethod of the
objectdatasource is implicitly called.
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top