Get the DataItem for a GridView row

Posted on 2011-10-28
Last Modified: 2012-05-12
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;



Question by:dhenderson12
    LVL 33

    Accepted Solution

    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.

    Author Comment

    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.
    LVL 33

    Expert Comment

    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.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now