• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2698
  • 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" >
                     <Columns>
                        <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" />
                     </Columns>
                  </asp:GridView>

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

            this.grdNarrations.DataSource = page.TxNarrations;
            this.grdNarrations.DataBind();

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;  
                    System.Diagnostics.Debug.WriteLine(tx.OptionCode);
                }

                // 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;

            }

        }        

0
dhenderson12
Asked:
dhenderson12
  • 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.
0
 
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.
0
 
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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