0

hey I'm using to buttons in a gridview each which I want to assign to specific action involves retrieving certain cell values from the gridview columns I tried using

 GridViewRow row = (GridViewRow (((Button)e.CommandSource).NamingContainer);

but it gives me Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'.

I also tried

int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];

it gives me the following error Specified argument was out of the range of valid values. Parameter name: index

note that when I tried to specify the commandParameters property of the ButtonField the compiler said that it's not a valid parameter for the ButtomField and I have 8 columns in the gridview so its not out of range

or alternatively could I use more than one select command button if so how to say which one is clicked??

please help me im desperate

ASP code:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="AcitivityId" DataSourceID="SqlDataSource1" Height="304px" Width="912px" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:BoundField DataField="AcitivityId" HeaderText="AcitivityId" InsertVisible="False" ReadOnly="True" SortExpression="AcitivityId" />
                <asp:BoundField DataField="ActiviityName" HeaderText="ActiviityName" SortExpression="ActiviityName" />
                <asp:BoundField DataField="ActivityLocation" HeaderText="ActivityLocation" SortExpression="ActivityLocation" />
                <asp:BoundField DataField="ActivityStartDate" HeaderText="ActivityStartDate" SortExpression="ActivityStartDate" />
                <asp:BoundField DataField="ActivityDueDate" HeaderText="ActivityDueDate" SortExpression="ActivityDueDate" />
                <asp:BoundField DataField="ActivityDescription" HeaderText="ActivityDescription" SortExpression="ActivityDescription" />
                <asp:ButtonField ButtonType="Button" CommandName="Avaliable" Text="Show avaliable buses" />
                <asp:ButtonField ButtonType="Button" CommandName="Assigned" Text="Show assigned buses "/>
            </Columns>
        </asp:GridView>
Scarnet
  • 738
  • 2
  • 11
  • 36
  • Does this work: `GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;`? – Tim Schmelter Apr 26 '13 at 23:09
  • nope it doesn't it gives me Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'. – Scarnet Apr 26 '13 at 23:28

1 Answers1

0

I use this code in a number of places on my site to get the row from an object triggering a GridViewRowCommand

protected void btnUpdate_Command(object sender, CommandEventArgs e)
{
    GridViewRow g = (GridViewRow)((Button)sender).NamingContainer;
    // other stuff
}

Hope this helps.

CoderMarkus
  • 1,118
  • 1
  • 10
  • 24
  • thanks for your help @TrueDevelopment but im still getting the same error Unable to cast object of type 'System.Web.UI.WebControls.GridView' to type 'System.Web.UI.WebControls.Button'. – Scarnet Apr 27 '13 at 07:08