Jonathan Escobedo
Jonathan Escobedo

Reputation: 4053

Can't get values from rows/cells in GridView

I'm trying get values from a GridView using the following code:

foreach (GridViewRow row in this.dgvEstudios.Rows)
{
    var xy = row.Cells[1].Text;
}

Always get a an empty string ("") as the value returned from .Text, why does this happen? I have set EnableViewState to true

Upvotes: 2

Views: 8227

Answers (3)

Mark
Mark

Reputation: 41

If there are controls in each cell you will need to get the value like this:

Label lblEmailAddress = GridView.Rows[e.CommandArgument].FindControl("lblEmailAddress");
string Email = lblEmailAddress.Text;

in that case it it the control in the cell that has the value not the cell iteslf.

Upvotes: 4

shahkalpesh
shahkalpesh

Reputation: 33476

The cell might have controls inside it (e.g. LiteralControl or an HyperLink). This is what you should be looking for.

row.Cells[1].Controls collection, you should look for.

Upvotes: 2

Jeff Martin
Jeff Martin

Reputation: 11022

it could depend on many things.. Where is this code fired in relation to when the GridView is populated (Databind() called)?

Without any context, its hard to say what else it could be.

Upvotes: 1

Related Questions