Sacramento .Net User Group

February 7, 2008

Accessing field values from the selected row of ASP.Net Gridview control

Filed under: Uncategorized — sacdotnet @ 4:30 am

This seems to be a common question in various forums

There is a clear distinction between accessing data in TemplateField and a BoundField

For BoundFields, it is straight forward:

string s = dgMyItems.SelectedRow.Cells[1].Text; // to access the second column of the selected row

For TemplateFields, use the following

string s = ((System.Web.UI.WebControls.Label)dgMyItems.SelectedRow.Cells[2].FindControl(“lblName”)).Text;

Assuming that you defined a label in the templatefield definition for the datagrid, with id =  lblName

for example,

<ItemTemplate>
  <asp:Label ID=”lblName” runat=”server” Text=’<%# Bind(“MyItemName”) %>’></asp:Label>
</ItemTemplate>

— Repost from Manoj’s blog

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.