views:

2125

answers:

2

I have an ASP.NET GridView that's bound to an ObjectDataSource (which is bound to a MySQL database). On this grid, I have 2 unbound ButtonField columns that I want to trigger server-side events. Hence I have added an eventhandler method to the GridView's RowCommand event.

In the code of said eventhandler, I need to somehow get hold of the underlying DataRow that was clicked on by the user. However, I can't seem to get this to work; if I use code like the following the selectedRow variable is always null:

protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
  CallDataRow selectedRow = (CallDataRow) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].DataItem;
}

I've Googled and found pages such as http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event, but nothing I've found has worked. I'm pretty sure I'm just missing something obvious, but I can't figure out what...

Here's the ASP.NET code if that helps:

  <asp:GridView ID="searchResultsGridView" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="PersonNo,CallDate" 
    Width="100%" AllowPaging="True" EnableSortingAndPagingCallbacks="True" 
    onrowcommand="searchResultsGridView_RowCommand" PageSize="20">
    <Columns>
      <asp:BoundField DataField="PersonNo" HeaderText="Account number" ReadOnly="True" 
        SortExpression="PersonNo" />
      <asp:BoundField DataField="AgentNo" HeaderText="Agent number" 
        SortExpression="AgentNo" />
      <asp:BoundField DataField="AgentName" HeaderText="Agent name" 
        SortExpression="AgentName" />
      <asp:BoundField DataField="TelNumber" HeaderText="Telephone number" 
        SortExpression="TelNumber" />
      <asp:BoundField DataField="CallDate" HeaderText="Call date/time" ReadOnly="True" 
        SortExpression="CallDate" />
      <asp:ButtonField CommandName="play" HeaderText="Audio" ShowHeader="True" 
        Text="Play" />
      <asp:ButtonField CommandName="download" Text="Download" />
    </Columns>
  </asp:GridView>
A: 

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = searchResultsGridView.Rows[index];

Gregoire
That gives me a GridViewRow with RowType == DataRow and DataItem == null. Since it's the DataItem that I require to get hold of the underlying DataRow, and the DataItem is null in this case, this isn't an answer.
Ian Kemp
+1  A: 

Finally got it to work by doing the following:

  1. Adding a TemplateField containing a bound HiddenField.

    <asp:TemplateField ShowHeader="False">
      <ItemTemplate>
        <asp:HiddenField ID="audioFileName" runat="server" Value='<%# Eval("AudioFileName") %>' />
      </ItemTemplate>
    </asp:TemplateField>
    
  2. Adding the following code in the RowCommand event handler:

    protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
      string audioFile = ((HiddenField) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].FindControl("audioFileName")).Value;
    }
    

It's a cludge and it's not particularly secure, but it works and that's all I need right now. Any better solutions are still welcome though...

Ian Kemp