• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2927
  • Last Modified:

Get the DataItem for a GridView row

I have a GridView control whose datasource is a custom object.

Loading and displaying the gird view works perfectly.

 I have added a button fied that calls the RowCommand(object sender, GridViewCommandEventArgs e) event.  I am trying to get the dataitem that this row is based  on.

any help appreciated.

here is the asp source:
<asp:GridView ID="grdNarrations" runat="server" CellPadding="3"
                     AutoGenerateColumns="False" AllowPaging="False"
                     onrowcommand="grdNarrations_RowCommand" >
                        <asp:ButtonField ButtonType="Image" ImageUrl="~/images/EditCodeHS.bmp"
                                         CommandName="EditNarration" />
                        <asp:BoundField datafield="translationType" HeaderText="Type" />
                        <asp:BoundField datafield="languageCode" HeaderText="Language" />
                        <asp:BoundField datafield="content" HeaderText="Content" />

here is the code that sets the datasource for the gridview:

            this.grdNarrations.DataSource = page.TxNarrations;

page.TxNarrations is a List<Translation>
Translation is an object with serveral fields from a table;  the gridview is only displaying 2 of the
object properties.

here is the code when the button control in the gridview is pressed:

        protected void grdNarrations_RowCommand(object sender, GridViewCommandEventArgs e)
            if (e.CommandName == "EditNarration")
                // Retrieve the row index stored in the
                // CommandArgument property.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button
                // from the Rows collection.
                GridViewRow row = grdNarrations.Rows[index];
               // the gridview is based on a list of Translation objects, yet this returns
               // returns null.
                if (row.RowType == DataControlRowType.DataRow)
                    Translation tx = (Translation) row.DataItem;  

                // the below works just fine, returning data from a specific cell
                string content = row.Cells[3].Text;
                fb.Text = content;
                editDiv.Visible = true;
                viewDiv.Visible = false;



  • 2
1 Solution
Paul MacDonaldDirector, Information SystemsCommented:
Put an invisible column in your gridview that holds the ID from your dataset...
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" Visible="False" />

...then on the back end, grab that value (this is VB so you'll have to convert it)...
   Dim intActiveRow As Integer = Convert.ToInt32(e.CommandArgument)
    Dim dkRowData As DataKey = grdNarrations.DataKeys(intActiveRow)
    Dim strIDColumnValue As String = dkRowData.Values("ID").ToString

I can't claim this is the best/most elegant way, but it works for me.
dhenderson12Author Commented:
thx.  I can return any individual cell item.  the problem is that I need to return the underlying object that the particular row is based on , ie. the Translation object.
Paul MacDonaldDirector, Information SystemsCommented:
Can you not stuff a unique identifier into the Grid (like an ID) and use that to get back to the ListItem?  I don't work with Lists very much so I have limited experience there.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now