pikk
pikk

Reputation: 855

Get GridView Row

I have a GridView which I bind to a SqlDataReader on Page_Load. It has a column with buttons and I am trying to get the row when a button is clicked with the following code:

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

EDIT : Pasting the .aspx page from the comments sectio

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" OnRowCommand="GridView1_RowCommand" DataKeyNames="id" GridLines="None"> <AlternatingRowStyle BackColor="White" /> 
    <Columns> 
        <asp:TemplateField> 
             <ItemTemplate> 
                  <asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' /> 
             </ItemTemplate> 
         </asp:TemplateField> 
     </Columns>
</aspx:GridView> 

I get the following error: 'System.FormatException: Input string was not in a correct format.' on line 'int index = Convert.ToInt32(e.CommandArgument);'.

Any ideas?

Upvotes: 3

Views: 8000

Answers (4)

Emaad Ali
Emaad Ali

Reputation: 1501

Check out this code

void ContactsGridView_RowCommand(Object sender,GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Add")
{
  // Convert the row index stored in the CommandArgument
  // property to an Integer.
  int index = Convert.ToInt32(e.CommandArgument);

  // Retrieve the row that contains the button clicked 
  // by the user from the Rows collection.
  GridViewRow row = ContactsGridView.Rows[index];

  // Create a new ListItem object for the contact in the row.     
  ListItem item = new ListItem();
  item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +
    Server.HtmlDecode(row.Cells[3].Text);

  // If the contact is not already in the ListBox, add the ListItem 
  // object to the Items collection of the ListBox control. 
  if (!ContactsListBox.Items.Contains(item))
  {
    ContactsListBox.Items.Add(item);
  }
 }
}    

Below html code for gridview

<asp:gridview id="ContactsGridView" 
          datasourceid="ContactsSource"
          allowpaging="true" 
          autogeneratecolumns="false"
          onrowcommand="ContactsGridView_RowCommand"
          runat="server">

          <columns>
            <asp:buttonfield buttontype="Link" 
              commandname="Add" 
              text="Add"/>
            <asp:boundfield datafield="ContactID" 
              headertext="Contact ID"/>
            <asp:boundfield datafield="FirstName" 
              headertext="First Name"/> 
            <asp:boundfield datafield="LastName" 
              headertext="Last Name"/>
          </columns>

        </asp:gridview>

Check link Gridview commands

Hope this answer helps you.

Upvotes: -1

Vinay
Vinay

Reputation: 1064

You have not added value to the command Argument. For your Button event in .aspx page

<asp:Button ID="btnChange" runat="server" Text="Change" CommandName="Test" CommandArgument = 1 Visible='<%# Convert.ToBoolean(Eval("Tested")) == true ? true : false %>' /> 

In the code behind i.e the RowCommand Event

if(e.CommandName == "Test")
{
int index = Convert.ToInt32(e.CommandArgument);
}

This will work only for value 1. To make it generic you can bind the command Argument to the value you want using one of the binding techniques for example : CommandArgument ='<%# Eval("ID") %>' (Assuming ID is present in the GridView)

Upvotes: 0

Kash
Kash

Reputation: 9039

You need to check which command in the GridView row has been clicked. Your markup should correspondingly map. See egs below. The e.CommandArgument you are getting may not correspond to your button click.

In CodeBehind:

    void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
    // If multiple buttons are used in a GridView control, use the CommandName property to determine which button was clicked.
    if(e.CommandName=="Add")
    {
    // Convert the row index stored in the CommandArgument property to an Integer.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the button clicked by the user from the Rows collection.
    GridViewRow row = CustomersGridView.Rows[index];

    // additional logic...
    }
    // additional logic...
}

In Markup:

Also please ensure you have set your CommandArgument attribute appropriately. Example below:

<asp:Button (...) CommandArgument="<%# Container.DataItemIndex %>" />

OR use a buttonfield

<asp:ButtonField ButtonType="button" CommandName="Add" Text="Add" />

Upvotes: 6

Ravi Gadag
Ravi Gadag

Reputation: 15881

Can u post the whole markup code,it would be helpful to solve. according to your question in gridview aspx code you have to use Command Name, and command Argument for the Button Control and it should bind to the one of the column of db. and use Row Command event of gridview. And also Try to use ItemTemplate to put Control Inside the gridview.

Click here for MSDN Documentation. Row Command in GridView

protected void Grid_RowCommand( object sender, GridViewCommandEventArgs e )
{
    int index = Convert.ToInt32( e.CommandArgument );
    your logic .......
}

Upvotes: 1

Related Questions