I have an ASP.NET GridViewrelated to ObjectDataSource(which is bound to a MySQL database). In this grid, I have 2 unrelated columns ButtonFieldthat I want to trigger events on the server side. So I added the eventhandler method to the event GridView RowCommand.
In the code of the mentioned event handler, I need to somehow grab hold of the base DataRowone that the user clicked on. However, I cannot get this to work; if I use the following code, then the variable is selectedRowalways null:
protected void searchResultsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
CallDataRow selectedRow = (CallDataRow) searchResultsGridView.Rows[Convert.ToInt32(e.CommandArgument)].DataItem;
}
I have Googled and found pages like http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event but nothing what I found did not work. I am pretty sure that I am just missing something obvious, but I cannot understand that ...
Here is the ASP.NET code if this 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>
source
share